using System;
using System.IO;

namespace TrajectoryOutputDemo;

/// <summary>Trajplanner_output 控制台入口；只输出规划诊断、轨迹摘要和 CSV 路径。</summary>
internal static class Program
{
    private static int Main(string[] args)
    {
        if (args.Length != 0)
        {
            Console.Error.WriteLine("用法：dotnet run --project TrajectoryOutputDemo.csproj");
            return 2;
        }

        string csvPath = Path.Combine(AppContext.BaseDirectory, "output", "trajectory.csv");
        try
        {
            TrajectoryOutputDemoResult result = new TrajectoryOutputDemoRunner().Run(
                TrajectoryOutputDemoConfiguration.CreateDefault(csvPath));
            if (!result.Succeeded || result.Trajectory == null || result.ControlTrajectory == null || result.CsvPath == null)
            {
                Console.Error.WriteLine(result.Diagnostic);
                return 1;
            }

            ControlTrajectoryPoint first = result.ControlTrajectory.Points[0];
            ControlTrajectoryPoint last = result.ControlTrajectory.Points[result.ControlTrajectory.Points.Count - 1];
            Console.WriteLine("轨迹 ID：" + result.Trajectory.Metadata.TrajectoryId);
            Console.WriteLine("生效时间：" + result.Trajectory.Metadata.EffectiveAtUtc.ToString("O"));
            Console.WriteLine("方向段：" + result.Trajectory.Metadata.SegmentIndex + "，终端类型：" + result.Trajectory.Metadata.TerminalType);
            Console.WriteLine("轨迹点数：" + result.ControlTrajectory.Points.Count);
            Console.WriteLine("首点：t=" + first.TimeFromStartSeconds + "s, X=" + first.XMeters + "m, Y=" + first.YMeters + "m");
            Console.WriteLine("末点：t=" + last.TimeFromStartSeconds + "s, X=" + last.XMeters + "m, Y=" + last.YMeters + "m");
            Console.WriteLine("CSV：" + result.CsvPath);
            return 0;
        }
        catch (Exception exception)
        {
            Console.Error.WriteLine("轨迹 Demo 运行异常：" + exception.Message);
            return 1;
        }
    }
}
