using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ROLAC.API.Data;

namespace ROLAC.API.Controllers;

[ApiController]
[Route("api/health")]
[AllowAnonymous]
public class HealthController : ControllerBase
{
    private readonly AppDbContext _db;
    public HealthController(AppDbContext db) => _db = db;

    [HttpGet]
    public async Task<IActionResult> Get(CancellationToken cancellationToken)
    {
        var canConnectToDatabase = await _db.Database.CanConnectAsync(cancellationToken);

        var payload = new
        {
            status   = canConnectToDatabase ? "healthy" : "degraded",
            database = canConnectToDatabase ? "up" : "down",
            time     = DateTimeOffset.UtcNow
        };

        return canConnectToDatabase ? Ok(payload) : StatusCode(503, payload);
    }
}
