package com.gbcm.client.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import com.gbcm.server.api.dto.workshop.WorkshopDTO;
import com.gbcm.server.api.dto.common.PagedResponseDTO;
import com.gbcm.server.api.enums.WorkshopPackage;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;

@ApplicationScoped
public class WorkshopRestService {

    private static final Logger logger = LoggerFactory.getLogger(WorkshopRestService.class);

    @Inject
    @RestClient
    private WorkshopRestClient workshopRestClient;

    public PagedResponseDTO<WorkshopDTO> getWorkshops(int page, int size, String sort,
            Object status, WorkshopPackage workshopPackage,
            Long coachId, String search) {
        try {
            logger.info("Appel API pour récupérer les ateliers");
            // Cast status if needed, assuming String matching
            String statusStr = status != null ? status.toString() : null;
            return workshopRestClient.getWorkshops(page, size, sort, statusStr, workshopPackage, coachId, search);
        } catch (Exception e) {
            logger.error("Erreur lors de l'appel API getWorkshops: " + e.getMessage(), e);
            throw new RuntimeException("Impossible de récupérer les ateliers", e);
        }
    }

    @RegisterRestClient
    @Path("/workshops")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public interface WorkshopRestClient {

        @GET
        PagedResponseDTO<WorkshopDTO> getWorkshops(
                @QueryParam("page") int page,
                @QueryParam("size") int size,
                @QueryParam("sort") String sort,
                @QueryParam("status") String status,
                @QueryParam("workshopPackage") WorkshopPackage workshopPackage,
                @QueryParam("coachId") Long coachId,
                @QueryParam("search") String search);
    }
}
