using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.PuntosDeVenta.Deactivate;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;

namespace SIGCM2.Application.Tests.PuntosDeVenta.Deactivate;

public class DeactivatePuntoDeVentaCommandHandlerTests
{
    private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
    private readonly IMedioRepository _medioRepo = Substitute.For<IMedioRepository>();
    private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
    private readonly DeactivatePuntoDeVentaCommandHandler _handler;

    private static PuntoDeVenta MakePdv(int id = 10, int medioId = 5, bool activo = true)
        => new(id, medioId, 1, "PdV Test", null, activo, DateTime.UtcNow, null);

    private static Medio MakeMedio(int id = 5, bool activo = true)
        => new(id, "COD" + id, "Medio " + id, TipoMedio.Diario, null, activo, DateTime.UtcNow, null);

    public DeactivatePuntoDeVentaCommandHandlerTests()
    {
        _handler = new DeactivatePuntoDeVentaCommandHandler(_repo, _medioRepo, _audit, TimeProvider.System);
        _medioRepo.GetByIdAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(MakeMedio(5, true));
    }

    [Fact]
    public async Task Handle_NotFound_ThrowsPuntoDeVentaNotFoundException()
    {
        _repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((PuntoDeVenta?)null);

        await Assert.ThrowsAsync<PuntoDeVentaNotFoundException>(
            () => _handler.Handle(new DeactivatePuntoDeVentaCommand(999)));
    }

    [Fact]
    public async Task Handle_AlreadyInactive_IsIdempotentAndDoesNotCallUpdateAsync()
    {
        _repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));

        await _handler.Handle(new DeactivatePuntoDeVentaCommand(10));

        await _repo.DidNotReceive().UpdateAsync(Arg.Any<PuntoDeVenta>(), Arg.Any<CancellationToken>());
    }

    [Fact]
    public async Task Handle_AlreadyInactive_DoesNotWriteAuditEvent()
    {
        _repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));

        await _handler.Handle(new DeactivatePuntoDeVentaCommand(10));

        await _audit.DidNotReceive().LogAsync(
            Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
            Arg.Any<object?>(), Arg.Any<CancellationToken>());
    }

    [Fact]
    public async Task Handle_ActivePdv_CallsUpdateAsyncWithInactiveEntity()
    {
        _repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: true));

        await _handler.Handle(new DeactivatePuntoDeVentaCommand(10));

        await _repo.Received(1).UpdateAsync(
            Arg.Is<PuntoDeVenta>(p => !p.Activo),
            Arg.Any<CancellationToken>());
    }

    [Fact]
    public async Task Handle_ActivePdv_WritesAuditWithDeactivateAction()
    {
        _repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: true));

        await _handler.Handle(new DeactivatePuntoDeVentaCommand(10));

        await _audit.Received(1).LogAsync(
            action: "punto_de_venta.deactivate",
            targetType: "PuntoDeVenta",
            targetId: "10",
            metadata: Arg.Any<object?>(),
            ct: Arg.Any<CancellationToken>());
    }
}
