using ERPCore.Dtos.Sales;
using ERPCore.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace ERPCore.Controllers;

[Route("api/v1/reports/sales")]
public sealed class SalesReportsController : ApiControllerBase
{
    private readonly ISalesReportService _reports;

    public SalesReportsController(ISalesReportService reports) => _reports = reports;

    [HttpGet]
    [ProducesResponseType(typeof(IReadOnlyList<SalesReportDefinitionDto>), StatusCodes.Status200OK)]
    public ActionResult<IReadOnlyList<SalesReportDefinitionDto>> ListReports()
        => Ok(_reports.ListReports());

    [HttpGet("{reportId}")]
    [ProducesResponseType(typeof(SalesReportDefinitionDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public ActionResult<SalesReportDefinitionDto> GetReport(string reportId)
    {
        var report = _reports.GetReport(reportId);
        return report is null ? NotFound() : Ok(report);
    }

    [HttpPost("query")]
    [ProducesResponseType(typeof(SalesReportQueryResponse), StatusCodes.Status200OK)]
    public async Task<ActionResult<SalesReportQueryResponse>> Query([FromBody] SalesReportQueryRequest request, CancellationToken ct)
    {
        var rows = await _reports.QueryAsync(request.ReportType, request.From, request.To, request.ItemId, request.CustomerId, request.WarehouseId, ct);
        return Ok(new SalesReportQueryResponse(request.ReportType, request.From, request.To, rows));
    }
}
