using OmsorgCore.Application.Abstractions;
using OmsorgCore.Domain.Entities;
using OmsorgCore.Domain.Enums;

namespace OmsorgCore.Application.Services;

public class AssignmentValidationSettingsService : IAssignmentValidationSettingsService
{
    private readonly IAssignmentValidationSettingsRepository _repository;

    public AssignmentValidationSettingsService(IAssignmentValidationSettingsRepository repository)
    {
        _repository = repository;
    }

    public Task<AssignmentValidationSettings> GetAsync(CancellationToken cancellationToken = default)
        => _repository.GetOrCreateAsync(cancellationToken);

    public async Task<AssignmentValidationSettings> UpdateAsync(
        ValidationSeverity qualificationMode,
        ValidationSeverity absenceMode,
        ValidationSeverity workingHoursMode,
        ValidationSeverity overlapMode,
        ValidationSeverity contractMode,
        CancellationToken cancellationToken = default)
    {
        var settings = await _repository.GetOrCreateAsync(cancellationToken);

        settings.QualificationMode = qualificationMode;
        settings.AbsenceMode = absenceMode;
        settings.WorkingHoursMode = workingHoursMode;
        settings.OverlapMode = overlapMode;
        settings.ContractMode = contractMode;

        await _repository.UpdateAsync(settings, cancellationToken);
        await _repository.SaveChangesAsync(cancellationToken);
        return settings;
    }
}
