package dev.lions.unionflow.server.resource;

import dev.lions.unionflow.server.service.airms.RapportAirmsService;
import dev.lions.unionflow.server.service.airms.RapportAirmsService.RapportAirmsData;
import io.quarkus.security.Authenticated;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.io.IOException;

/**
 * Endpoint de génération du rapport AIRMS triple PDF (technique/moral/financier).
 *
 * @since 2026-04-25 (P1-NEW-1)
 */
@Path("/api/airms/rapports")
@Authenticated
public class RapportAirmsResource {

  @Inject RapportAirmsService service;

  @POST
  @Path("/triple")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces("application/pdf")
  @RolesAllowed({"PRESIDENT", "TRESORIER", "ADMIN_ORGANISATION", "SUPER_ADMIN"})
  public Response genererTriple(RapportAirmsData data) throws IOException {
    byte[] pdf = service.genererRapportTriple(data);
    String filename = "Rapport_AIRMS_" + data.organisationDenomination().replaceAll("[^a-zA-Z0-9]", "_")
        + "_" + data.exerciceAnnee() + ".pdf";
    return Response.ok(pdf)
        .header("Content-Disposition", "attachment; filename=\"" + filename + "\"")
        .build();
  }
}
