package com.iqser.red.service.peristence.v1.server;

import com.knecon.fforesight.swaggercommons.SpringDocProperties;
import io.swagger.v3.oas.annotations.Hidden;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RestController
@RequiredArgsConstructor
@ConditionalOnProperty(value = "fforesight.springdoc.enabled", havingValue = "true")
@RequestMapping("${fforesight.springdoc.base-path:}")
public class SpringDocRedirectController {

    @Value("${server.servlet.context-path:}")
    private String contextPath;

    private final SpringDocProperties springDocProperties;


    @Hidden
    @GetMapping({"/api", "/api/"})
    public void home(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.sendRedirect(contextPath + springDocProperties.getBasePath() + "/docs/swagger-ui");
    }

}