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

namespace ERPCore.Controllers.Hrm;

/// <summary>Payslip retrieval + HTML print view (docs/13-BACKEND-HRM-API.md §6).</summary>
[Route("api/v1/payslips")]
public sealed class PayslipsController : ApiControllerBase
{
    private readonly IPayslipService _payslips;

    public PayslipsController(IPayslipService payslips) => _payslips = payslips;

    [HttpGet("{payslipId:int}")]
    [ProducesResponseType(typeof(PayslipDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<PayslipDto>> GetById(int payslipId, CancellationToken ct)
    {
        var result = await _payslips.GetAsync(payslipId, ct);
        return result is null ? NotFound() : Ok(result);
    }

    [HttpGet("{payslipId:int}/view")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> View(int payslipId, CancellationToken ct)
    {
        var html = await _payslips.RenderHtmlAsync(payslipId, ct);
        return html is null ? NotFound() : Content(html, "text/html");
    }
}
