using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SolutionErp.Application.Reports.Commands.ExportContractsToExcel;
using SolutionErp.Application.Reports.Dtos;
using SolutionErp.Application.Reports.Queries.GetDashboardStats;
using SolutionErp.Application.Reports.Queries.GetMyDashboard;
using SolutionErp.Domain.Contracts;

namespace SolutionErp.Api.Controllers;

[ApiController]
[Route("api/reports")]
[Authorize]
public class ReportsController(IMediator mediator) : ControllerBase
{
    [HttpGet("dashboard")]
    public async Task<ActionResult<DashboardStatsDto>> Dashboard(CancellationToken ct)
        => Ok(await mediator.Send(new GetDashboardStatsQuery(), ct));

    [HttpGet("my-dashboard")]
    public async Task<ActionResult<MyDashboardDto>> MyDashboard(CancellationToken ct)
        => Ok(await mediator.Send(new GetMyDashboardQuery(), ct));

    [HttpGet("contracts/export")]
    public async Task<IActionResult> ExportContracts(
        [FromQuery] ContractPhase? phase,
        [FromQuery] Guid? supplierId,
        [FromQuery] Guid? projectId,
        [FromQuery] DateTime? fromDate,
        [FromQuery] DateTime? toDate,
        CancellationToken ct)
    {
        var result = await mediator.Send(new ExportContractsToExcelCommand(phase, supplierId, projectId, fromDate, toDate), ct);
        return File(result.Content, result.ContentType, result.FileName);
    }
}
