package dev.lions.unionflow.server.resource.mutuelle.epargne;

import dev.lions.unionflow.server.api.dto.mutuelle.epargne.TransactionEpargneRequest;
import dev.lions.unionflow.server.api.dto.mutuelle.epargne.TransactionEpargneResponse;
import dev.lions.unionflow.server.service.mutuelle.epargne.TransactionEpargneService;

import dev.lions.unionflow.server.security.RequiresModule;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.validation.Valid;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import io.quarkus.security.identity.SecurityIdentity;

import java.util.List;
import java.util.UUID;

@Path("/api/v1/epargne/transactions")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@RequiresModule("EPARGNE")
public class TransactionEpargneResource {

    @Inject
    TransactionEpargneService transactionEpargneService;

    @Inject
    SecurityIdentity securityIdentity;

    @POST
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "MUTUELLE_RESP", "MEMBRE", "USER" })
    public Response executerTransaction(
            @Valid TransactionEpargneRequest request,
            @QueryParam("historique") @DefaultValue("false") boolean historique) {
        boolean bypassSolde = historique && securityIdentity.hasRole("SUPER_ADMIN");
        TransactionEpargneResponse transaction = transactionEpargneService.executerTransaction(request, bypassSolde);
        return Response.status(Response.Status.CREATED).entity(transaction).build();
    }

    @POST
    @Path("/transfert")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "MUTUELLE_RESP", "MEMBRE", "USER" })
    public Response transferer(@Valid TransactionEpargneRequest request) {
        TransactionEpargneResponse transaction = transactionEpargneService.transferer(request);
        return Response.status(Response.Status.CREATED).entity(transaction).build();
    }

    @GET
    @Path("/compte/{compteId}")
    @RolesAllowed({ "ADMIN", "SUPER_ADMIN", "ADMIN_ORGANISATION", "MUTUELLE_RESP", "MEMBRE", "USER" })
    public Response getTransactionsByCompte(@PathParam("compteId") UUID compteId) {
        List<TransactionEpargneResponse> transactions = transactionEpargneService.getTransactionsByCompte(compteId);
        return Response.ok(transactions).build();
    }
}
