package dev.lions.unionflow.client.service;

import dev.lions.unionflow.server.api.dto.common.PagedResponse;
import dev.lions.unionflow.server.api.dto.organisation.response.OrganisationResponse;
import dev.lions.unionflow.server.api.dto.organisation.response.OrganisationSummaryResponse;
import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import java.util.List;
import java.util.UUID;

@RegisterRestClient(configKey = "unionflow-api")
@RegisterClientHeaders(dev.lions.unionflow.client.security.AuthHeaderFactory.class)
@Path("/api/organisations")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public interface OrganisationService {

    @GET
    PagedResponse<OrganisationSummaryResponse> listerToutes(
            @QueryParam("page") @DefaultValue("0") int page,
            @QueryParam("size") @DefaultValue("1000") int size);

    @GET
    @Path("/{id}")
    OrganisationResponse obtenirParId(@PathParam("id") UUID id);

    @GET
    @Path("/recherche")
    PagedResponse<OrganisationSummaryResponse> rechercher(
            @QueryParam("nom") String nom,
            @QueryParam("type") String type,
            @QueryParam("statut") String statut,
            @QueryParam("region") String region,
            @QueryParam("ville") String ville,
            @QueryParam("page") @DefaultValue("0") int page,
            @QueryParam("size") @DefaultValue("20") int size);

    @GET
    @Path("/type/{type}")
    List<OrganisationSummaryResponse> listerParType(@PathParam("type") String type);

    @GET
    @Path("/region/{region}")
    List<OrganisationSummaryResponse> listerParRegion(@PathParam("region") String region);

    @POST
    OrganisationResponse creer(OrganisationResponse organisation);

    @PUT
    @Path("/{id}")
    OrganisationResponse modifier(@PathParam("id") UUID id, OrganisationResponse organisation);

    @DELETE
    @Path("/{id}")
    void supprimer(@PathParam("id") UUID id);

    // Côté serveur: POST /{id}/activer
    @POST
    @Path("/{id}/activer")
    OrganisationResponse activer(@PathParam("id") UUID id);

    // Suspension: POST /{id}/suspendre (alias historique "désactiver")
    @POST
    @Path("/{id}/suspendre")
    OrganisationResponse suspendre(@PathParam("id") UUID id);

    @PUT
    @Path("/{id}/dissoudre")
    OrganisationResponse dissoudre(@PathParam("id") UUID id);

    @GET
    @Path("/statistiques")
    StatistiquesOrganisationDTO obtenirStatistiques();

    @GET
    @Path("/{id}/membres/count")
    Long compterMembres(@PathParam("id") UUID id);

    @GET
    @Path("/{id}/performance")
    PerformanceOrganisationDTO obtenirPerformance(@PathParam("id") UUID id);

    // Classes DTO internes
    class StatistiquesOrganisationDTO {
        public Long totalAssociations;
        public Long associationsActives;
        public Long associationsInactives;
        public Long associationsSuspendues;
        public Long associationsDissoutes;
        public Long nouvellesAssociations30Jours;
        public Double tauxActivite;
        public java.util.Map<String, Long> repartitionParType;
        public java.util.Map<String, Long> repartitionParRegion;

        // Constructeurs
        public StatistiquesOrganisationDTO() {
        }

        // Getters et setters
        public Long getTotalAssociations() {
            return totalAssociations;
        }

        public void setTotalAssociations(Long totalAssociations) {
            this.totalAssociations = totalAssociations;
        }

        public Long getAssociationsActives() {
            return associationsActives;
        }

        public void setAssociationsActives(Long associationsActives) {
            this.associationsActives = associationsActives;
        }

        public Long getAssociationsInactives() {
            return associationsInactives;
        }

        public void setAssociationsInactives(Long associationsInactives) {
            this.associationsInactives = associationsInactives;
        }

        public Long getAssociationsSuspendues() {
            return associationsSuspendues;
        }

        public void setAssociationsSuspendues(Long associationsSuspendues) {
            this.associationsSuspendues = associationsSuspendues;
        }

        public Long getAssociationsDissoutes() {
            return associationsDissoutes;
        }

        public void setAssociationsDissoutes(Long associationsDissoutes) {
            this.associationsDissoutes = associationsDissoutes;
        }

        public Long getNouvellesAssociations30Jours() {
            return nouvellesAssociations30Jours;
        }

        public void setNouvellesAssociations30Jours(Long nouvellesAssociations30Jours) {
            this.nouvellesAssociations30Jours = nouvellesAssociations30Jours;
        }

        public Double getTauxActivite() {
            return tauxActivite;
        }

        public void setTauxActivite(Double tauxActivite) {
            this.tauxActivite = tauxActivite;
        }

        public java.util.Map<String, Long> getRepartitionParType() {
            return repartitionParType;
        }

        public void setRepartitionParType(java.util.Map<String, Long> repartitionParType) {
            this.repartitionParType = repartitionParType;
        }

        public java.util.Map<String, Long> getRepartitionParRegion() {
            return repartitionParRegion;
        }

        public void setRepartitionParRegion(java.util.Map<String, Long> repartitionParRegion) {
            this.repartitionParRegion = repartitionParRegion;
        }
    }

    class PerformanceOrganisationDTO {
        public UUID organisationId;
        public String nom;
        public Integer scoreGlobal;
        public Integer scoreMembres;
        public Integer scoreActivites;
        public Integer scoreFinances;
        public String tendance;
        public java.time.LocalDateTime derniereMiseAJour;

        // Constructeurs
        public PerformanceOrganisationDTO() {
        }

        // Getters et setters
        public UUID getOrganisationId() {
            return organisationId;
        }

        public void setOrganisationId(UUID organisationId) {
            this.organisationId = organisationId;
        }

        public String getNom() {
            return nom;
        }

        public void setNom(String nom) {
            this.nom = nom;
        }

        public Integer getScoreGlobal() {
            return scoreGlobal;
        }

        public void setScoreGlobal(Integer scoreGlobal) {
            this.scoreGlobal = scoreGlobal;
        }

        public Integer getScoreMembres() {
            return scoreMembres;
        }

        public void setScoreMembres(Integer scoreMembres) {
            this.scoreMembres = scoreMembres;
        }

        public Integer getScoreActivites() {
            return scoreActivites;
        }

        public void setScoreActivites(Integer scoreActivites) {
            this.scoreActivites = scoreActivites;
        }

        public Integer getScoreFinances() {
            return scoreFinances;
        }

        public void setScoreFinances(Integer scoreFinances) {
            this.scoreFinances = scoreFinances;
        }

        public String getTendance() {
            return tendance;
        }

        public void setTendance(String tendance) {
            this.tendance = tendance;
        }

        public java.time.LocalDateTime getDerniereMiseAJour() {
            return derniereMiseAJour;
        }

        public void setDerniereMiseAJour(java.time.LocalDateTime derniereMiseAJour) {
            this.derniereMiseAJour = derniereMiseAJour;
        }
    }
}
