package dev.lions.unionflow.server.resource.tontine;

import dev.lions.unionflow.server.api.dto.tontine.TontineRequest;
import dev.lions.unionflow.server.api.dto.tontine.TontineResponse;
import dev.lions.unionflow.server.api.enums.tontine.StatutTontine;
import dev.lions.unionflow.server.service.tontine.TontineService;

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/tontines")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@RequiresModule("TONTINE")
public class TontineResource {

    @Inject
    TontineService tontineService;

    @POST
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "TONTINE_RESP" })
    public Response creerTontine(@Valid TontineRequest request) {
        TontineResponse response = tontineService.creerTontine(request);
        return Response.status(Response.Status.CREATED).entity(response).build();
    }

    @GET
    @Path("/{id}")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "TONTINE_RESP", "MEMBRE", "USER" })
    public Response getTontineById(@PathParam("id") UUID id) {
        TontineResponse response = tontineService.getTontineById(id);
        return Response.ok(response).build();
    }

    @GET
    @Path("/organisation/{organisationId}")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "TONTINE_RESP" })
    public Response getTontinesByOrganisation(@PathParam("organisationId") UUID organisationId) {
        List<TontineResponse> response = tontineService.getTontinesByOrganisation(organisationId);
        return Response.ok(response).build();
    }

    @PATCH
    @Path("/{id}/statut")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "TONTINE_RESP" })
    public Response changerStatut(@PathParam("id") UUID id, @QueryParam("statut") StatutTontine statut) {
        if (statut == null) {
            return Response.status(Response.Status.BAD_REQUEST).entity("Le statut est requis").build();
        }
        TontineResponse response = tontineService.changerStatut(id, statut);
        return Response.ok(response).build();
    }
}
