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

import dev.lions.user.manager.client.filter.AuthHeaderFactory;
import dev.lions.user.manager.dto.role.RoleAssignmentDTO;
import dev.lions.user.manager.dto.role.RoleDTO;
import dev.lions.user.manager.enums.role.TypeRole;
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 rôles
 */
@Path("/api/roles")
@RegisterRestClient(configKey = "lions-user-manager-api")
@RegisterClientHeaders(AuthHeaderFactory.class)
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface RoleServiceClient {

    // ==================== Realm Roles ====================

    @POST
    @Path("/realm")
    RoleDTO createRealmRole(
        RoleDTO role,
        @QueryParam("realm") String realmName
    );

    @GET
    @Path("/realm/{roleName}")
    RoleDTO getRealmRoleByName(
        @PathParam("roleName") String roleName,
        @QueryParam("realm") String realmName
    );

    @GET
    @Path("/realm")
    List<RoleDTO> getAllRealmRoles(
        @QueryParam("realm") String realmName
    );

    @PUT
    @Path("/realm/{roleName}")
    RoleDTO updateRealmRole(
        @PathParam("roleName") String roleName,
        RoleDTO role,
        @QueryParam("realm") String realmName
    );

    @DELETE
    @Path("/realm/{roleName}")
    void deleteRealmRole(
        @PathParam("roleName") String roleName,
        @QueryParam("realm") String realmName
    );

    // ==================== Client Roles ====================

    @POST
    @Path("/client/{clientId}")
    RoleDTO createClientRole(
        @PathParam("clientId") String clientId,
        RoleDTO role,
        @QueryParam("realm") String realmName
    );

    @GET
    @Path("/client/{clientId}")
    List<RoleDTO> getAllClientRoles(
        @PathParam("clientId") String clientId,
        @QueryParam("realm") String realmName
    );

    @GET
    @Path("/client/{clientId}/{roleName}")
    RoleDTO getClientRoleByName(
        @PathParam("clientId") String clientId,
        @PathParam("roleName") String roleName,
        @QueryParam("realm") String realmName
    );

    @DELETE
    @Path("/client/{clientId}/{roleName}")
    void deleteClientRole(
        @PathParam("clientId") String clientId,
        @PathParam("roleName") String roleName,
        @QueryParam("realm") String realmName
    );

    // ==================== Role Assignment ====================

    @POST
    @Path("/assign/realm/{userId}")
    void assignRealmRolesToUser(
        @PathParam("userId") String userId,
        @QueryParam("realm") String realmName,
        RoleAssignmentRequest request
    );

    @POST
    @Path("/revoke/realm/{userId}")
    void revokeRealmRolesFromUser(
        @PathParam("userId") String userId,
        @QueryParam("realm") String realmName,
        RoleAssignmentRequest request
    );

    @GET
    @Path("/user/realm/{userId}")
    List<RoleDTO> getUserRealmRoles(
        @PathParam("userId") String userId,
        @QueryParam("realm") String realmName
    );

    @GET
    @Path("/user/client/{clientId}/{userId}")
    List<RoleDTO> getUserClientRoles(
        @PathParam("clientId") String clientId,
        @PathParam("userId") String userId,
        @QueryParam("realm") String realmName
    );

    /**
     * DTO pour l'attribution/révocation de rôles
     */
    class RoleAssignmentRequest {
        public List<String> roleNames;
    }

    // ==================== Composite Roles ====================

    @GET
    @Path("/composite/{roleName}")
    List<RoleDTO> getCompositeRoles(
        @PathParam("roleName") String roleName,
        @QueryParam("realm") String realmName,
        @QueryParam("typeRole") TypeRole typeRole,
        @QueryParam("clientName") String clientName
    );

    @POST
    @Path("/composite/{roleName}/add")
    void addCompositeRoles(
        @PathParam("roleName") String roleName,
        @QueryParam("realm") String realmName,
        RoleAssignmentRequest request
    );
}

