using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Tiku.Api.Contracts;
using Tiku.Application.Security;

namespace Tiku.Api.Controllers;

[ApiController]
[Tags("平台端-系统健康")]
[AllowAnonymous]
[Produces("application/json")]
[Route("api/system/health")]
public sealed class HealthController(IDependencyReadinessProbe readinessProbe) : ControllerBase
{
    [HttpGet]
    [EndpointSummary("健康检查")]
    [EndpointDescription("用于本地、CI 和部署环境 smoke test 的轻量健康检查。")]
    [ProducesResponseType<HealthResponseDto>(StatusCodes.Status200OK)]
    public ActionResult<HealthResponseDto> Get()
    {
        return Ok(new HealthResponseDto(
            "ok",
            "tiku-api",
            DateTimeOffset.UtcNow));
    }

    [HttpGet("ready")]
    [EndpointSummary("依赖就绪检查")]
    public async Task<ActionResult<object>> Ready(CancellationToken cancellationToken)
    {
        var readiness = await readinessProbe.CheckAsync(cancellationToken);
        var response = new { status = readiness.Ready ? "ready" : "not_ready", readiness.CheckedAt };
        return readiness.Ready ? Ok(response) : StatusCode(StatusCodes.Status503ServiceUnavailable, response);
    }
}