package dev.lions.unionflow.server.resource;

import dev.lions.unionflow.server.service.centif.DosCentifService;
import dev.lions.unionflow.server.service.centif.DosCentifService.DosCentifData;
import dev.lions.unionflow.server.service.centif.GoAmlXmlService;
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 des Déclarations d'Opérations Suspectes (DOS) pour la CENTIF.
 *
 * <p><strong>Accès restreint</strong> : seul un {@code COMPLIANCE_OFFICER} peut générer une DOS
 * (rôle issu de l'Instruction BCEAO 001-03-2025). Les fichiers ne sont jamais persistés sur
 * disque — streaming download direct.
 *
 * <p>L'export est tracé dans {@code audit_trail_operations} (action_type {@code EXPORT}).
 *
 * @since 2026-04-25 (P0-NEW-16)
 */
@Path("/api/aml/dos")
@Authenticated
public class DosCentifResource {

  @Inject DosCentifService dosService;
  @Inject GoAmlXmlService goAmlService;

  /**
   * Génère la DOS au format Word (.docx).
   *
   * @param data corps JSON {@link DosCentifData}
   * @return fichier Word streamé
   */
  @POST
  @Path("/word")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces("application/vnd.openxmlformats-officedocument.wordprocessingml.document")
  @RolesAllowed({"COMPLIANCE_OFFICER", "SUPER_ADMIN"})
  public Response genererWord(DosCentifData data) throws IOException {
    byte[] bytes = dosService.genererDosWord(data);
    String filename = "DOS_" + data.numeroDosInterne() + ".docx";
    return Response.ok(bytes)
        .header("Content-Disposition", "attachment; filename=\"" + filename + "\"")
        .build();
  }

  /**
   * Génère le relevé d'opérations atypiques au format Excel (.xlsx).
   *
   * @param data corps JSON {@link DosCentifData}
   * @return fichier Excel streamé
   */
  @POST
  @Path("/excel")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  @RolesAllowed({"COMPLIANCE_OFFICER", "SUPER_ADMIN"})
  public Response genererExcel(DosCentifData data) throws IOException {
    byte[] bytes = dosService.genererReleveExcel(data);
    String filename = "Releve_Operations_" + data.numeroDosInterne() + ".xlsx";
    return Response.ok(bytes)
        .header("Content-Disposition", "attachment; filename=\"" + filename + "\"")
        .build();
  }

  /**
   * Génère la DOS au format goAML XML (standard ONUDC) — anticipation adoption CI.
   * @since P2-NEW-4
   */
  @POST
  @Path("/goaml")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_XML)
  @RolesAllowed({"COMPLIANCE_OFFICER", "SUPER_ADMIN"})
  public Response genererGoAml(DosCentifData data) throws IOException {
    byte[] bytes = goAmlService.genererXml(data);
    String filename = "DOS_goAML_" + data.numeroDosInterne() + ".xml";
    return Response.ok(bytes)
        .header("Content-Disposition", "attachment; filename=\"" + filename + "\"")
        .build();
  }
}
