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

import dev.lions.unionflow.server.api.dto.mutuelle.epargne.CompteEpargneRequest;
import dev.lions.unionflow.server.api.dto.mutuelle.epargne.CompteEpargneResponse;
import dev.lions.unionflow.server.api.enums.mutuelle.epargne.StatutCompteEpargne;
import dev.lions.unionflow.server.service.mutuelle.epargne.CompteEpargneService;

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.util.List;
import java.util.UUID;

@Path("/api/v1/epargne/comptes")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@RequiresModule("EPARGNE")
public class CompteEpargneResource {

    @Inject
    CompteEpargneService compteEpargneService;

    @POST
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "MUTUELLE_RESP" })
    public Response creerCompte(@Valid CompteEpargneRequest request) {
        CompteEpargneResponse compte = compteEpargneService.creerCompte(request);
        return Response.status(Response.Status.CREATED).entity(compte).build();
    }

    @GET
    @Path("/{id}")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "MUTUELLE_RESP", "MEMBRE", "USER" })
    public Response getCompteById(@PathParam("id") UUID id) {
        CompteEpargneResponse compte = compteEpargneService.getCompteById(id);
        return Response.ok(compte).build();
    }

    @GET
    @Path("/mes-comptes")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "MUTUELLE_RESP", "MEMBRE", "USER" })
    public Response getMesComptes() {
        List<CompteEpargneResponse> comptes = compteEpargneService.getMesComptes();
        return Response.ok(comptes).build();
    }

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

    @GET
    @Path("/organisation/{organisationId}")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "MUTUELLE_RESP" })
    public Response getComptesByOrganisation(@PathParam("organisationId") UUID organisationId) {
        List<CompteEpargneResponse> comptes = compteEpargneService.getComptesByOrganisation(organisationId);
        return Response.ok(comptes).build();
    }

    @PATCH
    @Path("/{id}/statut")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "MUTUELLE_RESP" })
    public Response changerStatut(@PathParam("id") UUID id, @QueryParam("statut") StatutCompteEpargne statut) {
        if (statut == null) {
            return Response.status(Response.Status.BAD_REQUEST).entity("Le statut est requis").build();
        }
        CompteEpargneResponse compte = compteEpargneService.changerStatut(id, statut);
        return Response.ok(compte).build();
    }
}
