using Flyshot.ControllerClientCompat;
using Microsoft.AspNetCore.Mvc;

namespace Flyshot.Server.Host.Controllers;

/// <summary>
/// 提供控制器状态快照 API，状态监控页面由 wwwroot 静态资源承载。
/// </summary>
[ApiController]
[Tags("基础与状态")]
public sealed class StatusController : ControllerBase
{
    private readonly IControllerClientCompatService _compatService;

    /// <summary>
    /// 初始化状态监控控制器。
    /// </summary>
    /// <param name="compatService">ControllerClient 兼容层服务。</param>
    public StatusController(IControllerClientCompatService compatService)
    {
        _compatService = compatService ?? throw new ArgumentNullException(nameof(compatService));
    }

    /// <summary>
    /// 提供短路由 `/status`，跳转到静态状态页。
    /// </summary>
    /// <returns>重定向到 <c>/status.html</c>。</returns>
    [HttpGet("/status")]
    public IActionResult StatusPage()
    {
        return Redirect("/status.html");
    }

    /// <summary>
    /// 提供短路由 `/debug`，跳转到静态调试页。
    /// </summary>
    /// <returns>重定向到 <c>/debug.html</c>。</returns>
    [HttpGet("/debug")]
    public IActionResult DebugPage()
    {
        return Redirect("/debug.html");
    }

    /// <summary>
    /// 返回当前 ControllerClient 兼容层与控制器运行时状态快照。
    /// </summary>
    /// <returns>面向状态页和外部诊断的 JSON 快照。</returns>
    [HttpGet("/api/status/snapshot")]
    public IActionResult GetSnapshot()
    {
        var snapshot = _compatService.GetControllerSnapshot();
        var metadata = _compatService.GetStatusSnapshotMetadata();

        return Ok(new
        {
            Status = "ok",
            Service = "flyshot-server-host",
            ServerVersion = metadata.ServerVersion,
            ClientVersion = metadata.ClientVersion,
            IsSetup = metadata.IsSetup,
            RobotName = metadata.RobotName,
            DegreesOfFreedom = metadata.DegreesOfFreedom,
            UploadedTrajectories = metadata.UploadedTrajectories,
            Snapshot = snapshot
        });
    }
}
