using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using MultiWheelC.TrajectoryPlanning.Mapping;

namespace MultiWheelC.TrajectoryPlanning.CoarsePath.Facade;

/// <summary>
/// 一次粗路径业务编排的不可变结果。
/// 始终同时保留地图构建结果和规划结果；调试旁路消息只用于诊断，不会修改地图或规划内容。
/// </summary>
public sealed class CoarsePathPlanningJobResult
{
    /// <summary>
    /// 创建业务编排结果。
    /// 参数：mapResult 和 planningResult 均不能为空；debugDiagnostics 可为 null，返回时会转换为只读字符串集合。
    /// </summary>
    public CoarsePathPlanningJobResult(PlanningMapBuildResult mapResult, PlanningResult planningResult,
        IReadOnlyList<string> debugDiagnostics = null)
    {
        MapResult = mapResult ?? throw new ArgumentNullException(nameof(mapResult));
        PlanningResult = planningResult ?? throw new ArgumentNullException(nameof(planningResult));
        DebugDiagnostics = CopyDiagnostics(debugDiagnostics);
    }

    /// <summary>本次调用的地图创建结果；失败时读取 <see cref="PlanningMapBuildResult.FailureReason"/>。</summary>
    public PlanningMapBuildResult MapResult { get; }

    /// <summary>本次调用的粗路径结果；地图创建失败时为无路径的失败状态。</summary>
    public PlanningResult PlanningResult { get; }

    /// <summary>调试旁路的非致命诊断信息；为空时表示未启用、未发生异常或无额外调试消息。</summary>
    public IReadOnlyList<string> DebugDiagnostics { get; }

    private static IReadOnlyList<string> CopyDiagnostics(IReadOnlyList<string> source)
    {
        if (source == null || source.Count == 0)
            return new ReadOnlyCollection<string>(new List<string>());

        var copy = new List<string>(source.Count);
        for (int index = 0; index < source.Count; index++)
            copy.Add(source[index] ?? string.Empty);
        return new ReadOnlyCollection<string>(copy);
    }
}
