using IWS5.Core.DTO;
using IWS5.Core.Enums;
using IWS5.Core.Models.Request.Process;
using IWS5.Core.Services;
using IWS5.DatabaseAccess.Interfaces;
using Serilog;

namespace IWS5.Services;

public class ProcessDocumentTemplateRightsService(
    IProcessDocumentTemplateRightsRepository processDocumentTemplateRightsRepository,
    ILogger logger) : IProcessDocumentTemplateRightsService
{
    public async Task UpdateProcessLimitTemplate(List<UpdateProcessDefaultTemplateRequest> baseRequest)
    {
        var result = await processDocumentTemplateRightsRepository.UpdateLimitTemplate(baseRequest);
        logger.Information("Successfully updated limit template for process {ProcessId}", baseRequest[0].ProcessId);
    }

    public async Task<List<ProcessDocumentTemplateRightsDTO>> ListProcessLimitTemplate(Guid processId)
    {
        var result = await processDocumentTemplateRightsRepository.ListAsync(template => template.ProcessObjectId == processId);
        logger.Information("Successfully retrieved limit document template");

        return result.Select(r => new ProcessDocumentTemplateRightsDTO
        {
            ProcessObjectId = r.ProcessObjectId,
            DocumentTemplateObjectId = r.DocumentTemplateObjectid,
            AllowRight = (ATRightAllowRight)r.AllowRights
        }).ToList();
    }
}
