using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SIGCM.Domain.Interfaces;

namespace SIGCM.API.Controllers;

[ApiController]
[Route("api/[controller]")]
[Authorize]
public class DashboardController : ControllerBase
{
    private readonly IListingRepository _repository;

    public DashboardController(IListingRepository repository)
    {
        _repository = repository;
    }

    // Obtiene estadísticas básicas para el dashboard principal
    [HttpGet("stats")]
    public async Task<IActionResult> GetStats([FromQuery] DateTime? start, [FromQuery] DateTime? end)
    {
        var startDate = start ?? DateTime.UtcNow.AddDays(-7);
        var endDate = end ?? DateTime.UtcNow;

        var stats = await _repository.GetDashboardStatsAsync(startDate, endDate);
        return Ok(stats);
    }

    // Obtiene analítica avanzada para reportes gerenciales detallados
    [HttpGet("analytics")]
    [Authorize(Roles = "Admin,Gerente")]
    public async Task<IActionResult> GetAdvancedAnalytics([FromQuery] DateTime? start, [FromQuery] DateTime? end)
    {
        var startDate = start ?? DateTime.UtcNow.AddMonths(-1);
        var endDate = end ?? DateTime.UtcNow;

        var analytics = await _repository.GetAdvancedAnalyticsAsync(startDate, endDate);
        return Ok(analytics);
    }
}
