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

namespace SIGCM2.Application.PuntosDeVenta.Update;

public sealed class UpdatePuntoDeVentaCommandHandler : ICommandHandler<UpdatePuntoDeVentaCommand, PuntoDeVentaUpdatedDto>
{
    private readonly IPuntoDeVentaRepository _repo;
    private readonly IMedioRepository _medioRepo;
    private readonly IAuditLogger _audit;
    private readonly TimeProvider _timeProvider;

    public UpdatePuntoDeVentaCommandHandler(
        IPuntoDeVentaRepository repo,
        IMedioRepository medioRepo,
        IAuditLogger audit,
        TimeProvider timeProvider)
    {
        _repo = repo;
        _medioRepo = medioRepo;
        _audit = audit;
        _timeProvider = timeProvider;
    }

    public async Task<PuntoDeVentaUpdatedDto> Handle(UpdatePuntoDeVentaCommand command)
    {
        var target = await _repo.GetByIdAsync(command.Id)
            ?? throw new PuntoDeVentaNotFoundException(command.Id);

        var medio = await _medioRepo.GetByIdAsync(target.MedioId)
            ?? throw new MedioNotFoundException(target.MedioId);

        if (!medio.Activo)
            throw new MedioInactivoException(medio.Id);

        // Re-validate uniqueness excluding current entity (REQ-PDV-004)
        var exists = await _repo.ExistsByNumeroAFIPInMedioAsync(target.MedioId, command.NumeroAFIP, excludeId: command.Id);
        if (exists)
            throw new NumeroAFIPDuplicadoException(target.MedioId, command.NumeroAFIP);

        var now = _timeProvider.GetUtcNow().UtcDateTime;
        var updated = target.WithUpdatedProfile(command.Nombre, command.NumeroAFIP, command.Descripcion, now);

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

        await _repo.UpdateAsync(updated);

        await _audit.LogAsync(
            action: "punto_de_venta.update",
            targetType: "PuntoDeVenta",
            targetId: command.Id.ToString(),
            metadata: new
            {
                before = new { target.Nombre, target.NumeroAFIP, target.Descripcion },
                after = new { updated.Nombre, updated.NumeroAFIP, updated.Descripcion },
            });

        tx.Complete();

        return new PuntoDeVentaUpdatedDto(
            Id: updated.Id,
            MedioId: updated.MedioId,
            NumeroAFIP: updated.NumeroAFIP,
            Nombre: updated.Nombre,
            Descripcion: updated.Descripcion,
            Activo: updated.Activo);
    }
}
