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

import dev.lions.unionflow.server.api.dto.vote.CampagneVoteRequest;
import dev.lions.unionflow.server.api.dto.vote.CampagneVoteResponse;
import dev.lions.unionflow.server.api.dto.vote.CandidatDTO;
import dev.lions.unionflow.server.api.enums.vote.StatutVote;
import dev.lions.unionflow.server.service.vote.CampagneVoteService;

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/vote/campagnes")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@RequiresModule("VOTES")
public class CampagneVoteResource {

    @Inject
    CampagneVoteService campagneVoteService;

    @POST
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "VOTE_RESP" })
    public Response creerCampagne(@Valid CampagneVoteRequest request) {
        CampagneVoteResponse response = campagneVoteService.creerCampagne(request);
        return Response.status(Response.Status.CREATED).entity(response).build();
    }

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

    @GET
    @Path("/organisation/{organisationId}")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "VOTE_RESP" })
    public Response getCampagnesByOrganisation(@PathParam("organisationId") UUID organisationId) {
        List<CampagneVoteResponse> response = campagneVoteService.getCampagnesByOrganisation(organisationId);
        return Response.ok(response).build();
    }

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

    @POST
    @Path("/{id}/candidats")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "VOTE_RESP" })
    public Response ajouterCandidat(@PathParam("id") UUID id, @Valid CandidatDTO dto) {
        CandidatDTO response = campagneVoteService.ajouterCandidat(id, dto);
        return Response.status(Response.Status.CREATED).entity(response).build();
    }
}
