using System.Transactions;
using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Domain.Exceptions;

namespace SIGCM2.Application.Rubros.Deactivate;

public sealed class DeactivateRubroCommandHandler : ICommandHandler<DeactivateRubroCommand, RubroStatusDto>
{
    private readonly IRubroRepository _repo;
    private readonly IAuditLogger _audit;
    private readonly TimeProvider _timeProvider;

    public DeactivateRubroCommandHandler(
        IRubroRepository repo,
        IAuditLogger audit,
        TimeProvider timeProvider)
    {
        _repo = repo;
        _audit = audit;
        _timeProvider = timeProvider;
    }

    public async Task<RubroStatusDto> Handle(DeactivateRubroCommand command)
    {
        var target = await _repo.GetByIdAsync(command.Id)
            ?? throw new RubroNotFoundException(command.Id);

        var activeChildren = await _repo.CountActiveChildrenAsync(command.Id);
        if (activeChildren > 0)
            throw new RubroTieneHijosActivosException(command.Id, activeChildren);

        var deactivated = target.WithActivo(false, _timeProvider);

        using var tx = new TransactionScope(
            TransactionScopeOption.Required,
            new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted },
            TransactionScopeAsyncFlowOption.Enabled);

        await _repo.UpdateAsync(deactivated);

        await _audit.LogAsync(
            action: "rubro.deleted",
            targetType: "Rubro",
            targetId: command.Id.ToString(),
            metadata: new
            {
                rubroId = command.Id,
                nombre = target.Nombre,
                activeChildrenCount = 0,
            });

        tx.Complete();

        return new RubroStatusDto(Id: deactivated.Id, Activo: deactivated.Activo);
    }
}
