package com.iqser.red.persistence.service.v1.external.api.impl.controller;

import static com.iqser.red.service.persistence.management.v1.processor.roles.ActionRoles.READ_DOSSIER_STATUS;
import static com.iqser.red.service.persistence.management.v1.processor.roles.ActionRoles.WRITE_DOSSIER_STATUS;

import java.util.List;
import java.util.stream.Collectors;

import jakarta.transaction.Transactional;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.iqser.red.service.persistence.management.v1.processor.exception.BadRequestException;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.DossierStatusPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.utils.ColorUtils;
import com.iqser.red.service.persistence.management.v1.processor.utils.DossierStatusMapper;
import com.knecon.fforesight.databasetenantcommons.providers.utils.MagicConverter;
import com.iqser.red.service.persistence.service.v1.api.external.resource.DossierStatusResource;
import com.iqser.red.service.persistence.service.v1.api.shared.model.DossierStatusRequest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.CreateOrUpdateDossierStatusRequest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.DossierStatusInfo;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequiredArgsConstructor
public class DossierStatusController implements DossierStatusResource {

    private final DossierStatusPersistenceService dossierStatusPersistenceService;


    @Override
    @PreAuthorize("hasAuthority('" + WRITE_DOSSIER_STATUS + "')")
    public DossierStatusInfo createOrUpdateDossierStatus(@RequestBody DossierStatusRequest dossierStatusRequest) {

        if (dossierStatusRequest.getDossierTemplateId() == null) {
            throw new BadRequestException("Dossier Template must be set for creation");
        }
        if (dossierStatusRequest.getName() == null || dossierStatusRequest.getName().isEmpty()) {
            throw new BadRequestException("Dossier status name must be set");
        }
        //validate color
        ColorUtils.validateColor(dossierStatusRequest.getColor());
        //validate rank
        if (dossierStatusRequest.getRank() < 0) {
            throw new BadRequestException("The rank must not be negative");
        }
        var response = dossierStatusPersistenceService.createOrUpdateDossierStatus(CreateOrUpdateDossierStatusRequest.builder()
                .dossierStatusId(dossierStatusRequest.getDossierStatusId())
                .name(dossierStatusRequest.getName())
                .description(dossierStatusRequest.getDescription())
                .dossierTemplateId(dossierStatusRequest.getDossierTemplateId())
                .rank(dossierStatusRequest.getRank())
                .color(dossierStatusRequest.getColor())
                .build());
        return MagicConverter.convert(response, DossierStatusInfo.class);
    }


    @Override
    @PreAuthorize("hasAuthority('" + READ_DOSSIER_STATUS + "')")
    public List<DossierStatusInfo> getAllDossierStatusForTemplate(@PathVariable("dossierTemplateId") String dossierTemplateId) {

        return dossierStatusPersistenceService.getAllDossierStatusForTemplate(dossierTemplateId)
                .stream()
                .map(d -> MagicConverter.convert(d, DossierStatusInfo.class))
                .collect(Collectors.toList());
    }


    @Override
    @PreAuthorize("hasAuthority('" + READ_DOSSIER_STATUS + "')")
    public List<DossierStatusInfo> getAllDossierStatuses(@RequestBody List<String> dossierTemplateIds) {

        return dossierStatusPersistenceService.getAllDossierStatuses(dossierTemplateIds)
                .stream()
                .map(d -> MagicConverter.convert(d, DossierStatusInfo.class))
                .collect(Collectors.toList());
    }


    @Override
    @Transactional
    @PreAuthorize("hasAuthority('" + READ_DOSSIER_STATUS + "')")
    public DossierStatusInfo getDossierStatus(@PathVariable("dossierStatusId") String dossierStatusId) {

        return MagicConverter.convert(dossierStatusPersistenceService.getDossierStatus(dossierStatusId), DossierStatusInfo.class, new DossierStatusMapper());
    }


    @Override
    @PreAuthorize("hasAuthority('" + WRITE_DOSSIER_STATUS + "')")
    public void deleteDossierStatus(@PathVariable("dossierStatusId") String dossierStatusId,
                                    @RequestParam(value = DOSSIER_STATUS_REPLACE_ID, required = false) String replaceDossierStatusId) {

        dossierStatusPersistenceService.deleteDossierStatus(dossierStatusId, replaceDossierStatusId);
    }

}
