package dev.lions.unionflow.server.resource.mutuelle.credit;

import dev.lions.unionflow.server.api.dto.mutuelle.credit.DemandeCreditRequest;
import dev.lions.unionflow.server.api.dto.mutuelle.credit.DemandeCreditResponse;
import dev.lions.unionflow.server.api.enums.mutuelle.credit.StatutDemandeCredit;
import dev.lions.unionflow.server.service.mutuelle.credit.DemandeCreditService;

import dev.lions.unionflow.server.security.RequiresModule;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.validation.Valid;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

@Path("/api/v1/mutuelle/credits")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@RequiresModule("CREDIT")
public class DemandeCreditResource {

    @Inject
    DemandeCreditService demandeCreditService;

    @POST
    @RolesAllowed({ "MEMBRE", "USER" })
    public Response soumettreDemande(@Valid DemandeCreditRequest request) {
        DemandeCreditResponse response = demandeCreditService.soumettreDemande(request);
        return Response.status(Response.Status.CREATED).entity(response).build();
    }

    @GET
    @Path("/{id}")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "MUTUELLE_RESP", "MEMBRE", "USER" })
    public Response getDemandeById(@PathParam("id") UUID id) {
        DemandeCreditResponse response = demandeCreditService.getDemandeById(id);
        return Response.ok(response).build();
    }

    @GET
    @Path("/membre/{membreId}")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "MUTUELLE_RESP", "MEMBRE", "USER" })
    public Response getDemandesByMembre(@PathParam("membreId") UUID membreId) {
        List<DemandeCreditResponse> response = demandeCreditService.getDemandesByMembre(membreId);
        return Response.ok(response).build();
    }

    @PATCH
    @Path("/{id}/statut")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "MUTUELLE_RESP" })
    public Response changerStatut(
            @PathParam("id") UUID id,
            @QueryParam("statut") StatutDemandeCredit statut,
            @QueryParam("notes") String notes) {
        if (statut == null) {
            return Response.status(Response.Status.BAD_REQUEST).entity("Le statut est requis").build();
        }
        DemandeCreditResponse response = demandeCreditService.changerStatut(id, statut, notes);
        return Response.ok(response).build();
    }

    @POST
    @Path("/{id}/approbation")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "MUTUELLE_RESP" })
    public Response approuver(
            @PathParam("id") UUID id,
            @QueryParam("montant") BigDecimal montant,
            @QueryParam("duree") Integer duree,
            @QueryParam("taux") BigDecimal taux,
            @QueryParam("notes") String notes) {
        DemandeCreditResponse response = demandeCreditService.approuver(id, montant, duree, taux, notes);
        return Response.ok(response).build();
    }

    @POST
    @Path("/{id}/decaissement")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "MUTUELLE_RESP" })
    public Response decaisser(
            @PathParam("id") UUID id,
            @QueryParam("datePremiereEcheance") String datePremiereEcheance) {
        LocalDate date = LocalDate.parse(datePremiereEcheance);
        DemandeCreditResponse response = demandeCreditService.decaisser(id, date);
        return Response.ok(response).build();
    }
}
