package dev.lions.unionflow.server.resource;

import dev.lions.unionflow.server.api.dto.suggestion.request.CreateSuggestionRequest;
import dev.lions.unionflow.server.api.dto.suggestion.response.SuggestionResponse;
import dev.lions.unionflow.server.service.SuggestionService;
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 lombok.extern.slf4j.Slf4j;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Resource REST pour la gestion des suggestions utilisateur
 * 
 * @author UnionFlow Team
 * @version 1.0
 */
@Path("/api/suggestions")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Tag(name = "Suggestions", description = "Gestion des suggestions utilisateur")
@Slf4j
@RolesAllowed({ "USER", "ADMIN", "ADMIN_ORGANISATION", "MEMBRE" })
public class SuggestionResource {

    @Inject
    SuggestionService suggestionService;

    @GET
    @Operation(summary = "Lister toutes les suggestions")
    @APIResponse(responseCode = "200", description = "Liste des suggestions récupérée avec succès")
    public Response listerSuggestions() {
        log.info("GET /api/suggestions");
        List<SuggestionResponse> suggestions = suggestionService.listerSuggestions();
        return Response.ok(suggestions).build();
    }

    @POST
    @Operation(summary = "Créer une nouvelle suggestion")
    @APIResponse(responseCode = "201", description = "Suggestion créée avec succès")
    public Response creerSuggestion(@Valid CreateSuggestionRequest request) {
        log.info("POST /api/suggestions");
        SuggestionResponse created = suggestionService.creerSuggestion(request);
        return Response.status(Response.Status.CREATED).entity(created).build();
    }

    @POST
    @Path("/{id}/voter")
    @Operation(summary = "Voter pour une suggestion")
    @APIResponse(responseCode = "200", description = "Vote enregistré avec succès")
    public Response voterPourSuggestion(@PathParam("id") UUID id, @QueryParam("utilisateurId") UUID utilisateurId) {
        log.info("POST /api/suggestions/{}/voter", id);
        suggestionService.voterPourSuggestion(id, utilisateurId);
        return Response.ok().build();
    }

    @GET
    @Path("/statistiques")
    @Operation(summary = "Obtenir les statistiques des suggestions")
    @APIResponse(responseCode = "200", description = "Statistiques récupérées avec succès")
    public Response obtenirStatistiques() {
        log.info("GET /api/suggestions/statistiques");
        Map<String, Object> stats = suggestionService.obtenirStatistiques();
        return Response.ok(stats).build();
    }
}
