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.coach.CoachDTO;
import com.gbcm.server.api.dto.coach.CreateCoachDTO;
import com.gbcm.server.api.dto.coach.UpdateCoachDTO;
import com.gbcm.server.api.dto.common.PagedResponseDTO;

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

@ApplicationScoped
public class CoachRestService {

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

    @Inject
    @RestClient
    private CoachRestClient coachRestClient;

    public PagedResponseDTO<CoachDTO> getCoaches(int page, int size, String sort,
            String status, String specialization, boolean availableOnly, String search) {
        try {
            logger.info("Appel API pour récupérer les coachs - page: {}, size: {}", page, size);
            // On utilise l'endpoint dev ou standard selon dispo, ici on assume standard
            // sans dev prefix pour test
            // Mais si Client utilise /dev, faisons pareil pour consistance si dispo.
            // CoachResource n'a pas forcement /dev. Checkons CoachResource.
            // Je vais utiliser l'interface RestClient definie plus bas.
            return coachRestClient.getCoaches(page, size, sort, status, specialization, availableOnly, search);
        } catch (Exception e) {
            logger.error("Erreur lors de l'appel API getCoaches: " + e.getMessage(), e);
            throw new RuntimeException("Impossible de récupérer les coachs", e);
        }
    }

    public CoachDTO getCoachById(Long id) {
        return coachRestClient.getCoachById(id);
    }

    // Autres méthodes create/update si besoin

    @RegisterRestClient
    @Path("/coaches") // Base path relative to quarkus.rest-client.url
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public interface CoachRestClient {

        @GET
        PagedResponseDTO<CoachDTO> getCoaches(
                @QueryParam("page") int page,
                @QueryParam("size") int size,
                @QueryParam("sort") String sort,
                @QueryParam("status") String status,
                @QueryParam("specialization") String specialization,
                @QueryParam("availableOnly") boolean availableOnly,
                @QueryParam("search") String search);

        @GET
        @Path("/{id}")
        CoachDTO getCoachById(@PathParam("id") Long id);
    }
}
