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

import dev.lions.user.manager.dto.audit.AuditLogDTO;
import dev.lions.user.manager.enums.audit.TypeActionAudit;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * REST Client pour le service d'audit
 */
@Path("/api/audit")
@RegisterRestClient(configKey = "lions-user-manager-api")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface AuditServiceClient {

    @POST
    @Path("/search")
    List<AuditLogDTO> searchLogs(
        @QueryParam("acteur") String acteurUsername,
        @QueryParam("dateDebut") String dateDebut,
        @QueryParam("dateFin") String dateFin,
        @QueryParam("typeAction") TypeActionAudit typeAction,
        @QueryParam("ressourceType") String ressourceType,
        @QueryParam("succes") Boolean succes,
        @QueryParam("page") @DefaultValue("0") int page,
        @QueryParam("pageSize") @DefaultValue("50") int pageSize
    );

    @GET
    @Path("/acteur/{acteurUsername}")
    List<AuditLogDTO> getLogsByActeur(
        @PathParam("acteurUsername") String acteurUsername,
        @QueryParam("limit") @DefaultValue("100") int limit
    );

    @GET
    @Path("/realm/{realmName}")
    List<AuditLogDTO> getLogsByRealm(
        @PathParam("realmName") String realmName,
        @QueryParam("dateDebut") String dateDebut,
        @QueryParam("dateFin") String dateFin,
        @QueryParam("page") @DefaultValue("0") int page,
        @QueryParam("pageSize") @DefaultValue("50") int pageSize
    );

    @GET
    @Path("/ressource/{ressourceType}/{ressourceId}")
    List<AuditLogDTO> getLogsByRessource(
        @PathParam("ressourceType") String ressourceType,
        @PathParam("ressourceId") String ressourceId,
        @QueryParam("limit") @DefaultValue("100") int limit
    );

    @GET
    @Path("/action/{typeAction}")
    List<AuditLogDTO> getLogsByAction(
        @PathParam("typeAction") TypeActionAudit typeAction,
        @QueryParam("dateDebut") String dateDebut,
        @QueryParam("dateFin") String dateFin,
        @QueryParam("limit") @DefaultValue("100") int limit
    );

    @GET
    @Path("/statistics/actions")
    Map<TypeActionAudit, Long> getActionStatistics(
        @QueryParam("dateDebut") String dateDebut,
        @QueryParam("dateFin") String dateFin
    );

    @GET
    @Path("/statistics/users")
    Map<String, Long> getUserActivityStatistics(
        @QueryParam("dateDebut") String dateDebut,
        @QueryParam("dateFin") String dateFin
    );

    @GET
    @Path("/statistics/failures")
    Long getFailureCount(
        @QueryParam("dateDebut") String dateDebut,
        @QueryParam("dateFin") String dateFin
    );

    @GET
    @Path("/statistics/successes")
    Long getSuccessCount(
        @QueryParam("dateDebut") String dateDebut,
        @QueryParam("dateFin") String dateFin
    );

    @GET
    @Path("/export/csv")
    @Produces("text/csv")
    String exportLogsToCSV(
        @QueryParam("dateDebut") String dateDebut,
        @QueryParam("dateFin") String dateFin
    );
}

