using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ROLAC.API.DTOs.MealAttendance;
using ROLAC.API.Services;

namespace ROLAC.API.Controllers;

[ApiController]
[Route("api/meal-attendance")]
public class MealAttendanceController : ControllerBase
{
    private readonly IMealAttendanceService _svc;

    public MealAttendanceController(IMealAttendanceService svc) => _svc = svc;

    /// <summary>Today's live counts. Public — feeds the volunteer counter page on first load.</summary>
    [HttpGet("today")]
    [AllowAnonymous]
    public async Task<IActionResult> GetToday()
        => Ok(await _svc.GetOrCreateAsync(_svc.ServiceDay));

    /// <summary>Daily counts within a date range, for the back-office dashboard chart.</summary>
    [HttpGet]
    [Authorize]
    public async Task<IActionResult> GetRange([FromQuery] DateOnly from, [FromQuery] DateOnly to)
        => Ok(await _svc.GetRangeAsync(from, to));

    /// <summary>Overwrite a specific Sunday's counts (back-office editor). Authenticated only.</summary>
    [HttpPut("{date}")]
    [Authorize]
    public async Task<IActionResult> SetCounts(DateOnly date, [FromBody] SetAttendanceRequest body)
        => Ok(await _svc.SetCountsAsync(date, body.Adult, body.Youth, body.Kid));
}
