package dev.lions.unionflow.server.resource;

import dev.lions.unionflow.server.api.dto.kyc.request.CreateBeneficiaireEffectifRequest;
import dev.lions.unionflow.server.api.dto.kyc.request.UpdateBeneficiaireEffectifRequest;
import dev.lions.unionflow.server.api.dto.kyc.response.BeneficiaireEffectifResponse;
import dev.lions.unionflow.server.service.kyc.BeneficiaireEffectifService;
import io.quarkus.security.Authenticated;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.validation.Valid;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.util.List;
import java.util.UUID;

/**
 * Endpoints REST des Bénéficiaires Effectifs (UBO) — Instr. BCEAO 003-03-2025.
 *
 * <p>Cette Resource ne fait QUE le mapping HTTP ↔ Service. Toute la logique
 * métier (validation pourcentages, audit trail, persistence) est dans
 * {@link BeneficiaireEffectifService}.
 *
 * @since 2026-04-25 (Sprint 10)
 */
@Path("/api/kyc/beneficiaires-effectifs")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Authenticated
public class BeneficiaireEffectifResource {

  @Inject BeneficiaireEffectifService service;

  @GET
  @RolesAllowed({"COMPLIANCE_OFFICER", "CONTROLEUR_INTERNE", "ADMIN_ORGANISATION", "SUPER_ADMIN"})
  public List<BeneficiaireEffectifResponse> lister(
      @QueryParam("kycDossierId") UUID kycDossierId,
      @QueryParam("organisationCibleId") UUID organisationCibleId,
      @QueryParam("pep") Boolean pep) {
    if (Boolean.TRUE.equals(pep)) return service.listerPep();
    if (kycDossierId != null) return service.listerParKycDossier(kycDossierId);
    if (organisationCibleId != null) return service.listerParOrganisationCible(organisationCibleId);
    return List.of();
  }

  @GET
  @Path("/{id}")
  @RolesAllowed({"COMPLIANCE_OFFICER", "CONTROLEUR_INTERNE", "ADMIN_ORGANISATION", "SUPER_ADMIN"})
  public BeneficiaireEffectifResponse trouverParId(@PathParam("id") UUID id) {
    return service.trouverParId(id);
  }

  @POST
  @RolesAllowed({"COMPLIANCE_OFFICER", "ADMIN_ORGANISATION", "SUPER_ADMIN"})
  public Response creer(@Valid CreateBeneficiaireEffectifRequest request) {
    BeneficiaireEffectifResponse created = service.creer(request);
    return Response.status(Response.Status.CREATED).entity(created).build();
  }

  @PUT
  @Path("/{id}")
  @RolesAllowed({"COMPLIANCE_OFFICER", "ADMIN_ORGANISATION", "SUPER_ADMIN"})
  public BeneficiaireEffectifResponse mettreAJour(
      @PathParam("id") UUID id, @Valid UpdateBeneficiaireEffectifRequest request) {
    return service.mettreAJour(id, request);
  }

  @DELETE
  @Path("/{id}")
  @RolesAllowed({"COMPLIANCE_OFFICER", "ADMIN_ORGANISATION", "SUPER_ADMIN"})
  public Response desactiver(@PathParam("id") UUID id) {
    service.desactiver(id);
    return Response.noContent().build();
  }
}
