using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Protocol;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Types;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening;
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Validation;

namespace ZB.MOM.WW.ScadaBridge.DeploymentManager;

/// <summary>
/// Orchestrates the TemplateEngine services (FlatteningService, ValidationService, RevisionHashService)
/// into a single pipeline for deployment use.
///
/// WP-16: This captures template state at the time of flatten, ensuring that concurrent template edits
/// (last-write-wins) do not conflict with in-progress deployments.
/// </summary>
public class FlatteningPipeline : IFlatteningPipeline
{
    private readonly ITemplateEngineRepository _templateRepo;
    private readonly ISiteRepository _siteRepo;
    private readonly FlatteningService _flatteningService;
    private readonly ValidationService _validationService;
    private readonly RevisionHashService _revisionHashService;

    /// <summary>Initializes a new <see cref="FlatteningPipeline"/> with the required template engine and site repositories and services.</summary>
    /// <param name="templateRepo">Repository for loading templates and instance data.</param>
    /// <param name="siteRepo">Repository for loading site data used during validation.</param>
    /// <param name="flatteningService">Service that flattens the template inheritance chain into a resolved config.</param>
    /// <param name="validationService">Service that performs semantic validation on the flattened config.</param>
    /// <param name="revisionHashService">Service that computes the revision hash for staleness detection.</param>
    public FlatteningPipeline(
        ITemplateEngineRepository templateRepo,
        ISiteRepository siteRepo,
        FlatteningService flatteningService,
        ValidationService validationService,
        RevisionHashService revisionHashService)
    {
        _templateRepo = templateRepo;
        _siteRepo = siteRepo;
        _flatteningService = flatteningService;
        _validationService = validationService;
        _revisionHashService = revisionHashService;
    }

    /// <inheritdoc />
    public async Task<Result<FlatteningPipelineResult>> FlattenAndValidateAsync(
        int instanceId,
        CancellationToken cancellationToken = default)
    {
        // Load instance with full graph
        var instance = await _templateRepo.GetInstanceByIdAsync(instanceId, cancellationToken);
        if (instance == null)
            return Result<FlatteningPipelineResult>.Failure($"Instance with ID {instanceId} not found.");

        // Build template chain
        var templateChain = await BuildTemplateChainAsync(instance.TemplateId, cancellationToken);
        if (templateChain.Count == 0)
            return Result<FlatteningPipelineResult>.Failure("Template chain is empty.");

        // Build composition maps, walking nested compositions so the flattener
        // can resolve composed-of-composed attributes / alarms / scripts (e.g.
        // a parent that composes Pump where Pump itself composes AlarmSensor
        // produces "Pump.AlarmSensor.SensorReading").
        var compositionMap = new Dictionary<int, IReadOnlyList<Commons.Entities.Templates.TemplateComposition>>();
        var composedChains = new Dictionary<int, IReadOnlyList<Commons.Entities.Templates.Template>>();
        var processedTemplateIds = new HashSet<int>();
        var pendingChains = new Queue<IReadOnlyList<Commons.Entities.Templates.Template>>();

        pendingChains.Enqueue(templateChain);
        while (pendingChains.Count > 0)
        {
            var chain = pendingChains.Dequeue();
            foreach (var template in chain)
            {
                if (!processedTemplateIds.Add(template.Id)) continue;

                var compositions = await _templateRepo.GetCompositionsByTemplateIdAsync(template.Id, cancellationToken);
                if (compositions.Count == 0) continue;

                compositionMap[template.Id] = compositions;
                foreach (var comp in compositions)
                {
                    if (composedChains.ContainsKey(comp.ComposedTemplateId)) continue;

                    var composedChain = await BuildTemplateChainAsync(comp.ComposedTemplateId, cancellationToken);
                    composedChains[comp.ComposedTemplateId] = composedChain;
                    pendingChains.Enqueue(composedChain);
                }
            }
        }

        // Load data connections for the site
        var dataConnections = await LoadDataConnectionsAsync(instance.SiteId, cancellationToken);

        // Flatten
        var flattenResult = _flatteningService.Flatten(
            instance, templateChain, compositionMap, composedChains, dataConnections);

        if (flattenResult.IsFailure)
            return Result<FlatteningPipelineResult>.Failure(flattenResult.Error);

        var config = flattenResult.Value;

        // Load shared scripts for semantic validation
        var sharedScriptEntities = await _templateRepo.GetAllSharedScriptsAsync(cancellationToken);
        var resolvedSharedScripts = sharedScriptEntities.Select(s => new ResolvedScript
        {
            CanonicalName = s.Name,
            Code = s.Code,
            ParameterDefinitions = s.ParameterDefinitions,
            ReturnDefinition = s.ReturnDefinition
        }).ToList();

        // Compute the alarm-capable connection-name set so the semantic validator
        // can gate native-alarm-source bindings. "Alarm-capable" matches the DCL
        // runtime decision (DataConnectionActor: _adapter is IAlarmSubscribableConnection);
        // here we filter connections by alarm-capable protocol, then collect their names.
        //
        // StringComparer.Ordinal is intentional: connection names are stored and
        // matched as authored throughout the pipeline (all other name-keyed
        // dictionaries in FlatteningService and SemanticValidator use the same
        // case-sensitive semantics). OrdinalIgnoreCase would be inconsistent with
        // the rest of the binding-resolution path.
        var alarmCapableConnectionNames = dataConnections.Values
            .Where(c => AlarmCapableProtocols.IsAlarmCapable(c.Protocol))
            .Select(c => c.Name)
            .ToHashSet(StringComparer.Ordinal);

        // Validate
        var validation = _validationService.Validate(
            config, resolvedSharedScripts, alarmCapableConnectionNames);

        // Compute revision hash
        var hash = _revisionHashService.ComputeHash(config);

        return Result<FlatteningPipelineResult>.Success(
            new FlatteningPipelineResult(config, hash, validation));
    }

    private async Task<IReadOnlyList<Commons.Entities.Templates.Template>> BuildTemplateChainAsync(
        int templateId,
        CancellationToken cancellationToken)
    {
        var chain = new List<Commons.Entities.Templates.Template>();
        var currentId = (int?)templateId;

        while (currentId.HasValue)
        {
            var template = await _templateRepo.GetTemplateWithChildrenAsync(currentId.Value, cancellationToken);
            if (template == null) break;
            chain.Add(template);
            currentId = template.ParentTemplateId;
        }

        return chain;
    }

    private async Task<IReadOnlyDictionary<int, DataConnection>> LoadDataConnectionsAsync(
        int siteId,
        CancellationToken cancellationToken)
    {
        var connections = await _siteRepo.GetDataConnectionsBySiteIdAsync(siteId, cancellationToken);
        return connections.ToDictionary(c => c.Id);
    }
}
