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.KeycloakService;
import dev.lions.unionflow.server.service.SuggestionService;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
import org.jboss.logging.Logger;

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

/**
 * API pour l'envoi de commentaires / feedback utilisateur.
 * Persiste via SuggestionService (categorie = FEEDBACK).
 */
@Path("/api/feedback")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Tag(name = "Feedback", description = "Commentaires et suggestions utilisateur")
@RolesAllowed({ "USER", "ADMIN", "ADMIN_ORGANISATION", "MEMBRE" })
public class FeedbackResource {

        private static final Logger LOG = Logger.getLogger(FeedbackResource.class);

        @Inject
        KeycloakService keycloakService;
        @Inject
        SuggestionService suggestionService;

        @POST
        @Operation(summary = "Envoyer un commentaire / feedback")
        public Response sendFeedback(FeedbackRequest request) {
                if (request == null || (request.message == null || request.message.isBlank())) {
                        return Response.status(Response.Status.BAD_REQUEST)
                                        .entity(Map.of("error", "Le message est obligatoire"))
                                        .build();
                }
                String subject = request.subject != null && !request.subject.isBlank()
                                ? request.subject
                                : "Commentaire utilisateur";
                String userId = keycloakService.getCurrentUserId();
                UUID userUuid = userId != null && !userId.isBlank()
                                ? UUID.fromString(userId)
                                : UUID.fromString("00000000-0000-0000-0000-000000000000");
                CreateSuggestionRequest dto = CreateSuggestionRequest.builder()
                                .utilisateurId(userUuid)
                                .utilisateurNom(keycloakService.getCurrentUserFullName())
                                .titre(subject)
                                .description(request.message)
                                .categorie("FEEDBACK")
                                .build();
                SuggestionResponse created = suggestionService.creerSuggestion(dto);
                LOG.infof("Feedback reçu: %s", subject);
                return Response.status(Response.Status.CREATED)
                                .entity(Map.of("id", created.getId().toString(), "success", true))
                                .build();
        }

        /** Corps de requête pour POST /api/feedback */
        public static class FeedbackRequest {
                public String subject;
                public String message;
        }
}
