package dev.lions.user.manager.client.service;

import dev.lions.user.manager.client.filter.AuthHeaderFactory;
import dev.lions.user.manager.dto.realm.RealmAssignmentDTO;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import java.util.List;

/**
 * REST Client pour le service de gestion des affectations de realms
 */
@Path("/api/realm-assignments")
@RegisterRestClient(configKey = "lions-user-manager-api")
@RegisterClientHeaders(AuthHeaderFactory.class)
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface RealmAssignmentServiceClient {

    // ==================== Consultation ====================

    @GET
    List<RealmAssignmentDTO> getAllAssignments();

    @GET
    @Path("/user/{userId}")
    List<RealmAssignmentDTO> getAssignmentsByUser(@PathParam("userId") String userId);

    @GET
    @Path("/realm/{realmName}")
    List<RealmAssignmentDTO> getAssignmentsByRealm(@PathParam("realmName") String realmName);

    @GET
    @Path("/{assignmentId}")
    RealmAssignmentDTO getAssignmentById(@PathParam("assignmentId") String assignmentId);

    // ==================== Vérification ====================

    @GET
    @Path("/check")
    CheckResponse canManageRealm(
        @QueryParam("userId") String userId,
        @QueryParam("realmName") String realmName
    );

    @GET
    @Path("/authorized-realms/{userId}")
    AuthorizedRealmsResponse getAuthorizedRealms(@PathParam("userId") String userId);

    // ==================== Modification ====================

    @POST
    RealmAssignmentDTO assignRealmToUser(RealmAssignmentDTO assignment);

    @DELETE
    @Path("/user/{userId}/realm/{realmName}")
    void revokeRealmFromUser(
        @PathParam("userId") String userId,
        @PathParam("realmName") String realmName
    );

    @DELETE
    @Path("/user/{userId}")
    void revokeAllRealmsFromUser(@PathParam("userId") String userId);

    @PUT
    @Path("/{assignmentId}/deactivate")
    void deactivateAssignment(@PathParam("assignmentId") String assignmentId);

    @PUT
    @Path("/{assignmentId}/activate")
    void activateAssignment(@PathParam("assignmentId") String assignmentId);

    @PUT
    @Path("/super-admin/{userId}")
    void setSuperAdmin(
        @PathParam("userId") String userId,
        @QueryParam("superAdmin") Boolean superAdmin
    );

    // ==================== Classes de réponse ====================

    /**
     * Réponse de vérification d'accès
     */
    class CheckResponse {
        public boolean canManage;
        public String userId;
        public String realmName;
    }

    /**
     * Réponse des realms autorisés
     */
    class AuthorizedRealmsResponse {
        public List<String> realms;
        public boolean isSuperAdmin;
    }
}
