package io.gmss.fiscad.configuration;

import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class SwaggerOAS3Config {

    private static final String[] RFU_METIER = {
            "/api/batiment/**", "/api/caracteristique-caracteristiqueBatiment/**", "/api/caracteristique-parcelle/**",
            "/api/caracteristique-unite-logement/**", "/api/enquete-batiment/**", "/api/enquete-unite-logement/**",
            "/api/unite-logement/**", "/api/donnees-impositions-tfu/**", "/api/impositions-tfu/**"
    };

    private static final String[] RFU_PARAMETRE = {
            "/api/campagne/**", "/api/caracteristique/**", "/api/equipe/**", "/api/type-caracteristique/**","/api/categorie-batiment/**","/api/barem-rfu/**",
            "/api/zone-rfu/**", "/api/barem-rfu-non-bati/**", "/api/exercice/**"
    };

    private static final String[] DECOUPAGE = {
            "/api/arrondissement/**", "/api/commune/**", "/api/departement/**", "/api/nationalite/**", "/api/quartier/**",
            "/api/secteur/**", "/api/secteur-decoupage/**"
    };

    private static final String[] INFOCAD_METIER = {
            //metier
            "/api/acteur-concerne/**", "/api/commentaire/**", "/api/enquete/**", "/api/tpe/**", "/api/upload/**","/api/parcelle-geom/**"
    };

    private static final String[] INFOCAD_PARAMETERS = {
            "/api/bloc/**", "/api/mode-acquisition/**", "/api/nature-domaine/**", "/api/position-representation/**",
            "/api/profession/**", "/api/situation-geographique/**", "/api/situation-matrimoniale/**", "/api/source-droit/**",
            "/api/structure/**", "/api/type-contestation/**", "/api/type-domaine/**", "/api/type-personne/**", "/api/type-piece/**",
            "/api/type-representation/**"
    };

    private static final String[] NOTIFICATION = {
            "/api/email/**"
    };

    private static final String[] RAPPORT = {
            "/api/rapport/**"
    };

    private static final String[] STATISTIQUE = {
            "/api/statistique/**"
    };

    private static final String[] SYNCHRONISATION = {
            "/api/synchronisation/**", "/api/restauration/**"
    };

    private static final String[] USER = {
            "/api/auth/**", "/api/demande-reinitialisation-mp/**", "/api/role/**", "/api/user/**"
    };


    @Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group("ALL")
                .pathsToMatch("/**")
                .build();
    }

    @Bean
    public GroupedOpenApi rfuMetierApi() {
        return GroupedOpenApi.builder()
                .group("RFU_METIER")
                .pathsToMatch(RFU_METIER)
                .build();
    }

    @Bean
    public GroupedOpenApi rfuParametreApi() {
        return GroupedOpenApi.builder()
                .group("RFU_PARAMETRE")
                .pathsToMatch(RFU_PARAMETRE)
                .build();
    }


    @Bean
    public GroupedOpenApi infocadMetierApi() {
        return GroupedOpenApi.builder()
                .group("INFOCAD_METIER")
                .pathsToMatch(INFOCAD_METIER)
                .build();
    }

    @Bean
    public GroupedOpenApi infocadParametreApi() {
        return GroupedOpenApi.builder()
                .group("INFOCAD_PARAMETRE")
                .pathsToMatch(INFOCAD_PARAMETERS)
                .build();
    }

    @Bean
    public GroupedOpenApi decoupageApi() {
        return GroupedOpenApi.builder()
                .group("DECOUPAGE")
                .pathsToMatch(DECOUPAGE)
                .build();
    }

    @Bean
    public GroupedOpenApi notificationApi() {
        return GroupedOpenApi.builder()
                .group("NOTIFICATION")
                .pathsToMatch(NOTIFICATION)
                .build();
    }

    @Bean
    public GroupedOpenApi rapportApi() {
        return GroupedOpenApi.builder()
                .group("RAPPORT")
                .pathsToMatch(RAPPORT)
                .build();
    }

    @Bean
    public GroupedOpenApi statistiqueApi() {
        return GroupedOpenApi.builder()
                .group("STATISTIQUE")
                .pathsToMatch(STATISTIQUE)
                .build();
    }

    @Bean
    public GroupedOpenApi synchronisationApi() {
        return GroupedOpenApi.builder()
                .group("SYNCHRONISATION")
                .pathsToMatch(SYNCHRONISATION)
                .build();
    }

    @Bean
    public GroupedOpenApi userApi() {
        return GroupedOpenApi.builder()
                .group("USER")
                .pathsToMatch(USER)
                .build();
    }


    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");

        source.registerCorsConfiguration("/v3/api-docs", config);
        return new CorsFilter(source);
    }
}