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

namespace SIGCM2.Application.Rubros.Create;

public sealed class CreateRubroCommandHandler : ICommandHandler<CreateRubroCommand, RubroCreatedDto>
{
    private readonly IRubroRepository _repo;
    private readonly IAuditLogger _audit;
    private readonly TimeProvider _timeProvider;
    private readonly RubrosOptions _options;
    private readonly IAvisoQueryRepository _avisoQuery;

    public CreateRubroCommandHandler(
        IRubroRepository repo,
        IAuditLogger audit,
        TimeProvider timeProvider,
        IOptions<RubrosOptions> options,
        IAvisoQueryRepository avisoQuery)
    {
        _repo = repo;
        _audit = audit;
        _timeProvider = timeProvider;
        _options = options.Value;
        _avisoQuery = avisoQuery;
    }

    public async Task<RubroCreatedDto> Handle(CreateRubroCommand command)
    {
        // Validate parent exists and is active (if provided)
        if (command.ParentId.HasValue)
        {
            var parent = await _repo.GetByIdAsync(command.ParentId.Value);
            if (parent is null)
                throw new RubroNotFoundException(command.ParentId.Value);
            if (!parent.Activo)
                throw new RubroPadreInactivoException(command.ParentId.Value);

            // CAT-002: Regla de Oro — padre no puede ser hoja con avisos
            // CAT-002/PRD-002 TOCTOU — evaluar upgrade a RepeatableRead o constraint DB cuando exista dbo.Aviso
            var avisosCount = await _avisoQuery.CountAvisosEnRubroAsync(command.ParentId.Value);
            if (avisosCount > 0)
                throw new RubroPadreEsHojaConAvisosException(command.ParentId.Value, avisosCount);

            // Depth check: parent's depth + 1 must not exceed MaxDepth
            var parentDepth = await _repo.GetDepthAsync(command.ParentId);
            var newDepth = parentDepth + 1;
            if (newDepth > _options.MaxDepth)
                throw new RubroMaxDepthExceededException(newDepth, _options.MaxDepth);
        }

        // Duplicate name check (CI) under same parent
        var exists = await _repo.ExistsByNombreUnderParentAsync(command.ParentId, command.Nombre, excludeId: null);
        if (exists)
            throw new RubroNombreDuplicadoEnPadreException(command.Nombre, command.ParentId);

        // Determine Orden = MAX+1 among siblings
        var orden = await _repo.GetMaxOrdenAsync(command.ParentId);

        var now = _timeProvider.GetUtcNow().UtcDateTime;
        var rubro = Rubro.ForCreation(command.Nombre, command.ParentId, orden, command.TarifarioBaseId, _timeProvider);

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

        var newId = await _repo.AddAsync(rubro);

        await _audit.LogAsync(
            action: "rubro.created",
            targetType: "Rubro",
            targetId: newId.ToString(),
            metadata: new
            {
                after = new
                {
                    rubro.Nombre,
                    rubro.ParentId,
                    rubro.Orden,
                    rubro.TarifarioBaseId,
                },
            });

        tx.Complete();

        return new RubroCreatedDto(
            Id: newId,
            Nombre: rubro.Nombre,
            ParentId: rubro.ParentId,
            Orden: rubro.Orden,
            Activo: rubro.Activo,
            TarifarioBaseId: rubro.TarifarioBaseId);
    }
}
