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

import dev.lions.unionflow.server.api.dto.ong.ProjetOngDTO;
import dev.lions.unionflow.server.api.enums.ong.StatutProjetOng;
import dev.lions.unionflow.server.service.ong.ProjetOngService;

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/ong/projets")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@RequiresModule("PROJETS_ONG")
public class ProjetOngResource {

    @Inject
    ProjetOngService projetOngService;

    @POST
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "ONG_RESP" })
    public Response creerProjet(@Valid ProjetOngDTO dto) {
        ProjetOngDTO response = projetOngService.creerProjet(dto);
        return Response.status(Response.Status.CREATED).entity(response).build();
    }

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

    @GET
    @Path("/ong/{organisationId}")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "ONG_RESP" })
    public Response getProjetsByOng(@PathParam("organisationId") UUID organisationId) {
        List<ProjetOngDTO> response = projetOngService.getProjetsByOng(organisationId);
        return Response.ok(response).build();
    }

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