package dev.lions.unionflow.server.resource;

import dev.lions.unionflow.server.api.dto.config.request.UpdateConfigurationRequest;
import dev.lions.unionflow.server.api.dto.config.response.ConfigurationResponse;
import dev.lions.unionflow.server.service.ConfigurationService;
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;

/**
 * Resource REST pour la gestion de la configuration système
 * 
 * @author UnionFlow Team
 * @version 1.0
 */
@Path("/api/configuration")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Tag(name = "Configuration", description = "Gestion de la configuration système")
@Slf4j
@RolesAllowed({ "ADMIN", "SUPER_ADMIN" })
public class ConfigurationResource {

    @Inject
    ConfigurationService configurationService;

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

    @GET
    @Path("/{cle}")
    @Operation(summary = "Récupérer une configuration par clé")
    @APIResponse(responseCode = "200", description = "Configuration trouvée")
    public Response obtenirConfiguration(@PathParam("cle") String cle) {
        log.info("GET /api/configuration/{}", cle);
        ConfigurationResponse config = configurationService.obtenirConfiguration(cle);
        return Response.ok(config).build();
    }

    @PUT
    @Path("/{cle}")
    @Operation(summary = "Mettre à jour une configuration")
    @APIResponse(responseCode = "200", description = "Configuration mise à jour avec succès")
    public Response mettreAJourConfiguration(@PathParam("cle") String cle, @Valid UpdateConfigurationRequest request) {
        log.info("PUT /api/configuration/{}", cle);
        ConfigurationResponse updated = configurationService.mettreAJourConfiguration(cle, request);
        return Response.ok(updated).build();
    }
}
