package dev.lions.unionflow.server.resource;

import dev.lions.unionflow.server.entity.RapportTrimestrielControleurInterne;
import dev.lions.unionflow.server.repository.RapportTrimestrielControleurInterneRepository;
import dev.lions.unionflow.server.security.OrganisationContextHolder;
import dev.lions.unionflow.server.service.reporting.RapportTrimestrielService;
import io.quarkus.security.Authenticated;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.POST;
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 du Reporting trimestriel ControleurInterne (P2-NEW-3).
 *
 * <p>Accès restreint au {@code CONTROLEUR_INTERNE} de l'organisation et au {@code SUPER_ADMIN}.
 * La sélection de l'organisation active passe par le filtre {@link OrganisationContextHolder}
 * (header {@code X-Active-Organisation-Id}) ou via paramètre {@code orgId} pour SUPER_ADMIN.
 */
@Path("/api/rapports/trimestriel")
@Produces(MediaType.APPLICATION_JSON)
@Authenticated
public class RapportTrimestrielResource {

  @Inject RapportTrimestrielService service;
  @Inject RapportTrimestrielControleurInterneRepository repository;
  @Inject OrganisationContextHolder orgContext;

  @GET
  @RolesAllowed({"CONTROLEUR_INTERNE", "PRESIDENT", "ADMIN_ORGANISATION", "SUPER_ADMIN"})
  public List<RapportTrimestrielControleurInterne> lister(
      @QueryParam("orgId") UUID orgId,
      @QueryParam("annee") Integer annee) {
    UUID effectiveOrg = orgId != null ? orgId : orgContext.getOrganisationId();
    int effectiveAnnee = annee != null ? annee : java.time.Year.now().getValue();
    return repository.listerParOrgAnnee(effectiveOrg, effectiveAnnee);
  }

  @POST
  @Path("/generer")
  @RolesAllowed({"CONTROLEUR_INTERNE", "SUPER_ADMIN"})
  public RapportTrimestrielControleurInterne generer(
      @QueryParam("orgId") UUID orgId,
      @QueryParam("annee") int annee,
      @QueryParam("trimestre") int trimestre) {
    UUID effectiveOrg = orgId != null ? orgId : orgContext.getOrganisationId();
    return service.genererRapport(effectiveOrg, annee, trimestre);
  }

  @POST
  @Path("/{id}/signer")
  @RolesAllowed({"CONTROLEUR_INTERNE", "SUPER_ADMIN"})
  public RapportTrimestrielControleurInterne signer(
      @PathParam("id") UUID id, @QueryParam("signataireId") UUID signataireId) {
    return service.signer(id, signataireId);
  }

  @POST
  @Path("/{id}/archiver")
  @RolesAllowed({"CONTROLEUR_INTERNE", "PRESIDENT", "SUPER_ADMIN"})
  public RapportTrimestrielControleurInterne archiver(@PathParam("id") UUID id) {
    return service.archiver(id);
  }

  @GET
  @Path("/{id}/pdf")
  @Produces("application/pdf")
  @RolesAllowed({"CONTROLEUR_INTERNE", "PRESIDENT", "ADMIN_ORGANISATION", "SUPER_ADMIN"})
  public Response telechargerPdf(@PathParam("id") UUID id) {
    RapportTrimestrielControleurInterne r = repository.findById(id);
    if (r == null || r.getPdfBytes() == null) {
      throw new NotFoundException("Rapport ou PDF introuvable : " + id);
    }
    String filename = String.format("rapport-trim-%d-T%d.pdf", r.getAnnee(), r.getTrimestre());
    return Response.ok(r.getPdfBytes())
        .header("Content-Disposition", "attachment; filename=\"" + filename + "\"")
        .build();
  }
}
