package fr.titionfire.ffsaf.rest;

import fr.titionfire.ffsaf.domain.service.CompetitionService;
import fr.titionfire.ffsaf.rest.data.*;
import fr.titionfire.ffsaf.utils.SecurityCtx;
import io.quarkus.security.Authenticated;
import io.smallrye.mutiny.Uni;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.microprofile.openapi.annotations.Operation;

import java.util.List;

@Path("api/competition/")
public class CompetitionEndpoints {

    @Inject
    CompetitionService service;

    @Inject
    SecurityCtx securityCtx;

    @GET
    @Path("{id}")
    @Authenticated
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<CompetitionData> getById(@PathParam("id") Long id, @QueryParam("light") boolean light) {
        if (light)
            return service.getById(securityCtx, id);
        else
            return service.getByIdAdmin(securityCtx, id);
    }

    @GET
    @Path("{id}/register/{source}")
    @Authenticated
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<List<SimpleRegisterComb>> getRegister(@PathParam("id") Long id, @PathParam("source") String source) {
        return service.getRegister(securityCtx, id, source);
    }

    @POST
    @Path("{id}/register/{source}")
    @Authenticated
    @Produces(MediaType.APPLICATION_JSON)
    @Operation(hidden = true)
    public Uni<SimpleRegisterComb> addRegisterComb(@PathParam("id") Long id, @PathParam("source") String source,
                                                   RegisterRequestData data) {
        return service.addRegisterComb(securityCtx, id, data, source);
    }

    @POST
    @Path("{id}/registers/{source}")
    @Authenticated
    @Produces(MediaType.APPLICATION_JSON)
    @Operation(hidden = true)
    public Uni<List<SimpleRegisterComb>> addRegistersComb(@PathParam("id") Long id, @PathParam("source") String source,
                                                          List<RegisterRequestData> data) {
        return service.addRegistersComb(securityCtx, id, data, source);
    }

    @DELETE
    @Path("{id}/register/{comb_id}/{source}")
    @Authenticated
    @Produces(MediaType.APPLICATION_JSON)
    @Operation(hidden = true)
    public Uni<Void> removeRegisterComb(@PathParam("id") Long id, @PathParam("comb_id") Long combId,
                                        @PathParam("source") String source, @QueryParam("ban") boolean ban) {
        return service.removeRegisterComb(securityCtx, id, combId, source, ban);
    }

    @GET
    @Path("{id}/safcaData")
    @Authenticated
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<SimpleCompetData> getSafcaData(@PathParam("id") Long id) {
        return service.getSafcaData(securityCtx, id);
    }

    @GET
    @Path("{id}/internalData")
    @Authenticated
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<SimpleCompetData> getInternalData(@PathParam("id") Long id) {
        return service.getInternalData(securityCtx, id);
    }

    @GET
    @Path("{id}/categories")
    @Authenticated
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<List<PresetData>> getPresetsForCompetition(@PathParam("id") Long id) {
        return service.getPresetsForCompetition(securityCtx, id);
    }

    @GET
    @Path("all")
    @Authenticated
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<List<CompetitionData>> getAll() {
        return service.getAll(securityCtx);
    }

    @POST
    @Authenticated
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<CompetitionData> addOrUpdate(CompetitionData data) {
        return service.addOrUpdate(securityCtx, data);
    }

    @POST
    @Path("/safcaData")
    @Authenticated
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<?> setSafcaData(SimpleCompetData data) {
        return service.setSafcaData(securityCtx, data);
    }

    @POST
    @Path("/internalData")
    @Authenticated
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<?> setInternalData(SimpleCompetData data) {
        return service.setInternalData(securityCtx, data);
    }

    @DELETE
    @Path("{id}")
    @Authenticated
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<?> delete(@PathParam("id") Long id) {
        return service.delete(securityCtx, id);
    }
}
