using Microsoft.AspNetCore.Mvc; using GreenHome.Application; namespace GreenHome.Api.Controllers; [ApiController] [Route("api/[controller]")] public class MonthlyReportController : ControllerBase { private readonly IMonthlyReportService _monthlyReportService; private readonly ILogger _logger; public MonthlyReportController( IMonthlyReportService monthlyReportService, ILogger logger) { _monthlyReportService = monthlyReportService; _logger = logger; } /// /// دریافت گزارش آماری ماهانه /// [HttpGet] public async Task> GetMonthlyReport( [FromQuery] int deviceId, [FromQuery] int year, [FromQuery] int month, CancellationToken cancellationToken) { try { if (deviceId <= 0) { return BadRequest(new { error = "شناسه دستگاه نامعتبر است" }); } if (month < 1 || month > 12) { return BadRequest(new { error = "ماه باید بین 1 تا 12 باشد" }); } var result = await _monthlyReportService.GetMonthlyReportAsync(deviceId, year, month, cancellationToken); _logger.LogInformation( "گزارش ماهانه برای دستگاه {DeviceId} و ماه {Month}/{Year} ایجاد شد", deviceId, month, year); return Ok(result); } catch (InvalidOperationException ex) { return NotFound(new { error = ex.Message }); } catch (Exception ex) { _logger.LogError(ex, "Error generating monthly report"); return StatusCode(500, new { error = "خطا در ایجاد گزارش ماهانه" }); } } }