package com.iqser.red.service.persistence.management.v1.processor.service;

import com.iqser.red.service.persistence.management.v1.processor.acl.custom.dossier.DossierACLService;
import com.iqser.red.service.persistence.management.v1.processor.utils.DossierMapper;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.CreateOrUpdateDossierRequest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.Dossier;
import com.knecon.fforesight.databasetenantcommons.providers.utils.MagicConverter;

import io.micrometer.observation.annotation.Observed;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

import java.util.Set;

@Slf4j
@Service
@RequiredArgsConstructor
public class DossierCreatorService {

    private final DossierService dossierService;
    private final DossierACLService dossierACLService;


    @Transactional
    @Observed(name = "DossierCreatorService", contextualName = "create-dossier-acl")
    public Dossier addDossier(CreateOrUpdateDossierRequest dossierRequest, Set<String> members, Set<String> approvers, String ownerId) {

        var dossier = dossierService.addDossier(dossierRequest);
        dossierACLService.updateDossierACL(members, approvers, ownerId, dossier.getId());

        return MagicConverter.convert(dossier, Dossier.class, new DossierMapper());
    }

}
