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

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

import java.util.List;

@RegisterRestClient(configKey = "user-api")
@RegisterClientHeaders(AuthHeaderFactory.class)
@Path("/api/users")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface UserRestClient {

        @GET
        UserSearchResultDTO searchUsers(
                        @QueryParam("realm") String realmName,
                        @QueryParam("search") String searchTerm,
                        @QueryParam("username") String username,
                        @QueryParam("email") String email,
                        @QueryParam("prenom") String prenom,
                        @QueryParam("nom") String nom,
                        @QueryParam("enabled") Boolean enabled,
                        @QueryParam("page") int page,
                        @QueryParam("pageSize") int pageSize);

        @GET
        @Path("/{id}")
        UserDTO getUserById(@PathParam("id") String id, @QueryParam("realm") String realmName);

        @POST
        Response createUser(@QueryParam("realm") String realmName, UserDTO user);

        @PUT
        @Path("/{id}")
        UserDTO updateUser(@PathParam("id") String id, @QueryParam("realm") String realmName, UserDTO user);

        @DELETE
        @Path("/{id}")
        void deleteUser(@PathParam("id") String id, @QueryParam("realm") String realmName,
                        @QueryParam("hard") boolean hardDelete);

        // Correction : @POST (était @PUT — mismatch avec le serveur → 405)
        @POST
        @Path("/{id}/activate")
        void activateUser(@PathParam("id") String id, @QueryParam("realm") String realmName);

        // Correction : @POST (était @PUT — mismatch avec le serveur → 405)
        @POST
        @Path("/{id}/deactivate")
        void deactivateUser(@PathParam("id") String id, @QueryParam("realm") String realmName,
                        @QueryParam("reason") String reason);

        @POST
        @Path("/{id}/reset-password")
        void resetPassword(@PathParam("id") String id, @QueryParam("realm") String realmName,
                        PasswordResetRequest request);

        // Correction : path send-verification-email (était send-verify-email → 404)
        @POST
        @Path("/{id}/send-verification-email")
        void sendVerificationEmail(@PathParam("id") String id, @QueryParam("realm") String realmName);

        // Ajout : correspondance avec UserResourceApi.logoutAllSessions()
        @POST
        @Path("/{id}/logout-sessions")
        SessionsRevokedDTO logoutAllSessions(@PathParam("id") String id, @QueryParam("realm") String realmName);

        // Ajout : correspondance avec UserResourceApi.getActiveSessions()
        @GET
        @Path("/{id}/sessions")
        List<String> getActiveSessions(@PathParam("id") String id, @QueryParam("realm") String realmName);

        @GET
        @Path("/export/csv")
        @Produces(MediaType.TEXT_PLAIN)
        String exportUsersToCSV(@QueryParam("realm") String realmName);

        // Inner class pour la réinitialisation de mot de passe
        class PasswordResetRequest {
                public String password;
                public boolean temporary;
        }
}
