package cokr.xit.ens.core.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.servers.Server;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import java.util.Arrays;
import java.util.List;

@Configuration
public class SpringDocConfig {

    @Bean
    public OpenAPI openAPI(@Value("${xit.swagger.conf.api.url}") String url
            , @Value("${xit.swagger.conf.api.version}") String appVersion
            , @Value("${spring.profiles.active}") String active) {
        Info info = new Info().title("ENS API-Active Profile: " + active)
                .version(appVersion)
                .description("전자고지 Web Application API 입니다.")
                .termsOfService("http://swagger.io/terms/")
                .contact(new Contact().name("xit").url("https://www.xit.co.kr/").email("xit@xit.co.kr"))
                .license(new License().name("Apache License Version 2.0").url("http://www.apache.org/licenses/LICENSE-2.0"));

        /*TODO : 등록시 url 뒤에 url에 ... (curl 실행시)*/
        List<Server> servers = Arrays.asList(new Server().url(url).description("ENS (" + active + ")"));

        /* 인증-user / password 방식 */
//        SecurityScheme securityScheme = new SecurityScheme()
//                .type(SecurityScheme.Type.HTTP).scheme("basic")
//                .in(SecurityScheme.In.HEADER).name("Authorization");
//        SecurityRequirement schemaRequirement = new SecurityRequirement().addList("basicAuth");

        /* 인증-Token 방식 */
//        SecurityScheme securityScheme = new SecurityScheme()
//                .type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT")
//                .in(SecurityScheme.In.HEADER).name("Authorization");
//        SecurityRequirement schemaRequirement = new SecurityRequirement().addList("bearerAuth");

        return new OpenAPI()
//                .components(new Components().addSecuritySchemes("basicAuth", securityScheme))
//                .components(new Components().addSecuritySchemes("bearerAuth", securityScheme))
//                .addSecurityItem(schemaRequirement)
//                .security(Collections.singletonList(schemaRequirement))
                .info(info)
                .servers(servers);
    }


    @Bean
    public GroupedOpenApi systemMng() {
        return GroupedOpenApi.builder()
                .group("SYS. 시스템관리")
                .pathsToMatch("/sys/mng/**")
                .build();
    }

    @Bean
    public GroupedOpenApi systemUtil() {
        return GroupedOpenApi.builder()
                .group("SYS. 시스템도구")
                .pathsToMatch("/sys/util/**")
                .build();
    }

    @Profile({"local", "local-test", "local-maria", "local-oracle", "prod-iup", "dev-oracle"})
    @Bean
    public GroupedOpenApi cmmnFtBillPaysApiDoc() {
        return GroupedOpenApi.builder()
                .group("CF. 부가기능-청구서 공통 서비스")
                .pathsToMatch("/bill/pay/**")
                .build();
    }
    @Profile({"local", "local-test", "local-maria", "local-oracle", "prod-iup", "dev-oracle"})
    @Bean
    public GroupedOpenApi cmmnFtBillKkopayApiDoc() {
        return GroupedOpenApi.builder()
                .group("CF. 부가기능-청구서(kakaopay)")
                .pathsToMatch("/bill/kko/**")
                .build();
    }
    @Profile({"local", "local-test", "local-maria", "local-oracle", "prod-iup", "dev-oracle"})
    @Bean
    public GroupedOpenApi cmmnFtBillNvpayApiDoc() {
        return GroupedOpenApi.builder()
                .group("CF. 부가기능-청구서(naver 간편결제)")
                .pathsToMatch("/bill/nv/ep/**")
                .build();
    }

    @Profile({"local", "local-test", "local-maria", "local-oracle", "prod-traffic", "prod-traffic-cheonan", "prod-iup"})
    @Bean
    public GroupedOpenApi cmmnFtNiceCiDiApiDoc() {
        return GroupedOpenApi.builder()
                .group("CF. 부가기능-CI변환(나이스 CiDi)")
                .pathsToMatch("/cmft/nice/ci/**")
                .build();
    }

    @Bean
    public GroupedOpenApi cmmnFtMblPageApiDoc() {
        return GroupedOpenApi.builder()
                .group("CF. 부가기능-모바일페이지")
                .pathsToMatch(
                        "/cmft/mbl/page/**"
                )
                .build();
    }


    @Profile({"local", "local-test", "local-maria", "local-oracle", "prod-iup"})
    @Bean
    public GroupedOpenApi kkoAlimtalkApiDoc() {
        return GroupedOpenApi.builder()
                .group("CMPT. 전자고지-kakao 알림톡")
                .pathsToMatch("/kko/alimtalk/**")
                .build();
    }

    @Profile({"local", "local-test", "local-maria", "local-oracle", "prod-traffic", "prod-traffic-cheonan", "prod-iup", "dev-oracle"})
    @Bean
    public GroupedOpenApi kkoMydocApiDoc() {
        return GroupedOpenApi.builder()
                .group("CMPT. 전자고지-kakaopay 인증톡(내문서함)")
                .pathsToMatch("/kko/mydoc/**")
                .build();
    }

    @Profile({"local", "local-test", "local-maria", "local-oracle", "prod-iup", "dev-oracle"})
    @Bean
    public GroupedOpenApi nvSigntalkApiDoc() {
        return GroupedOpenApi.builder()
                .group("CMPT. 전자고지-naver 인증톡")
                .pathsToMatch("/nv/signtalk/**")
                .build();
    }

    @Profile({"local", "local-test", "local-maria", "local-oracle"})
    @Bean
    public GroupedOpenApi ktSigntalkApiDoc() {
        return GroupedOpenApi.builder()
                .group("CMPT. 전자고지-kt 인증톡")
                .pathsToMatch("/kt/signtalk/**")
                .build();
    }

    @Profile({"local", "local-test", "local-maria", "local-oracle", "prod-traffic-cheonan"})
    @Bean
    public GroupedOpenApi ktGibisApiDoc() {
        return GroupedOpenApi.builder()
                .group("CMPT. 전자고지-kt 인증톡(지비스)")
                .pathsToMatch("/kt/gibis/**")
                .build();
    }

    @Profile({"local", "local-test", "local-maria", "local-oracle", "prod-iup", "prod-traffic-cheonan", "dev-oracle"})
    @Bean
    public GroupedOpenApi intgrnNotiApiDoc() {
        return GroupedOpenApi.builder()
                .group("CMPT. 전자고지-통합고지")
                .pathsToMatch("/intgrn/noti/**")
                .build();
    }


    @Profile({"local", "local-test", "local-maria", "local-oracle", "prod-iup", "dev-oracle"})
    @Bean
    public GroupedOpenApi iupServiceApiDoc() {
        return GroupedOpenApi.builder()
                .group("BIZ. 이용시스템-민자고속도로(IUP)")
                .pathsToMatch("/iup/**")
                .build();
    }

    @Profile({"local", "local-test", "local-maria", "local-oracle", "prod-traffic"})
    @Bean
    public GroupedOpenApi trafficCcServiceApiDoc() {
        return GroupedOpenApi.builder()
                .group("BIZ. 이용시스템-주정차웹(TRAFFIC-춘천)")
                .pathsToMatch("/traffic/cc/**")
                .build();
    }

    @Profile({"local", "local-test", "local-maria", "local-oracle", "prod-traffic-cheonan"})
    @Bean
    public GroupedOpenApi trafficCaServiceApiDoc() {
        return GroupedOpenApi.builder()
                .group("BIZ. 이용시스템-주정차웹(TRAFFIC-천안)")
                .pathsToMatch("/traffic/ca/**")
                .build();
    }


//    @Bean
//    public GroupedOpenApi adminApiDoc() {
//        return GroupedOpenApi.builder()
//                .group("springshop-admin")
//                .pathsToMatch("/admin/**")
//                .addMethodFilter(method -> method.isAnnotationPresent(Admin.class))
//                .build();
//    }
}
