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

namespace ROLAC.API.Controllers;

[ApiController]
[Route("api/form1099-report")]
[HasPermission(Modules.Form1099, PermissionActions.Read)]
public class Form1099ReportController : ControllerBase
{
    private readonly IForm1099ReportService _svc;
    private readonly I1099FormService _form;
    public Form1099ReportController(IForm1099ReportService svc, I1099FormService form)
    {
        _svc = svc;
        _form = form;
    }

    [HttpGet("boxes")]
    public async Task<IActionResult> Boxes() => Ok(await _svc.GetBoxesAsync());

    [HttpGet("summary")]
    public async Task<IActionResult> Summary([FromQuery] int taxYear)
        => Ok(await _svc.GetAnnualSummaryAsync(taxYear));

    [HttpGet("recipient/{payeeId:int}")]
    public async Task<IActionResult> Recipient(int payeeId, [FromQuery] int taxYear)
        => await _svc.GetRecipientDetailAsync(payeeId, taxYear) is { } d ? Ok(d) : NotFound();

    [HttpGet("recipient/{payeeId:int}/copy-b")]
    public async Task<IActionResult> CopyB(int payeeId, [FromQuery] int taxYear)
    {
        var (stream, contentType, fileName) = await _form.RenderCopyBAsync(payeeId, taxYear);
        return File(stream, contentType, fileName);
    }

    [HttpGet("export-csv")]
    public async Task<IActionResult> ExportCsv([FromQuery] int taxYear)
    {
        var (stream, contentType, fileName) = await _form.ExportFilingCsvAsync(taxYear);
        return File(stream, contentType, fileName);
    }
}
