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

namespace ERPCore.Controllers.Hrm;

/// <summary>Read-only HRM reports (FR-HR-RPT, docs/13-BACKEND-HRM-API.md §6). No new entities — aggregation over existing tables.</summary>
[Route("api/v1/reports/hrm")]
public sealed class HrReportsController : ApiControllerBase
{
    private readonly IHrReportService _reports;

    public HrReportsController(IHrReportService reports) => _reports = reports;

    [HttpGet("attendance-summary")]
    [ProducesResponseType(typeof(List<AttendanceSummaryRowDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<List<AttendanceSummaryRowDto>>> AttendanceSummary(
        [FromQuery] int periodYear, [FromQuery] int periodMonth, [FromQuery] int? departmentId, CancellationToken ct)
        => Ok(await _reports.AttendanceSummaryAsync(periodYear, periodMonth, departmentId, ct));

    [HttpGet("overtime")]
    [ProducesResponseType(typeof(List<OvertimeReportRowDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<List<OvertimeReportRowDto>>> Overtime(
        [FromQuery] int periodYear, [FromQuery] int periodMonth, CancellationToken ct)
        => Ok(await _reports.OvertimeReportAsync(periodYear, periodMonth, ct));

    [HttpGet("late-arrivals")]
    [ProducesResponseType(typeof(List<LateArrivalReportRowDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<List<LateArrivalReportRowDto>>> LateArrivals(
        [FromQuery] int periodYear, [FromQuery] int periodMonth, CancellationToken ct)
        => Ok(await _reports.LateArrivalReportAsync(periodYear, periodMonth, ct));

    [HttpGet("payroll-register")]
    [ProducesResponseType(typeof(List<PayrollRegisterRowDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<List<PayrollRegisterRowDto>>> PayrollRegister([FromQuery] int payrollRunId, CancellationToken ct)
        => Ok(await _reports.PayrollRegisterAsync(payrollRunId, ct));

    [HttpGet("salary-history")]
    [ProducesResponseType(typeof(List<SalaryHistoryRowDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<List<SalaryHistoryRowDto>>> SalaryHistory([FromQuery] int employeeId, CancellationToken ct)
        => Ok(await _reports.SalaryHistoryAsync(employeeId, ct));

    [HttpGet("leave-balances")]
    [ProducesResponseType(typeof(List<LeaveBalanceReportRowDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<List<LeaveBalanceReportRowDto>>> LeaveBalances([FromQuery] int year, CancellationToken ct)
        => Ok(await _reports.LeaveBalanceReportAsync(year, ct));

    [HttpGet("document-expiry")]
    [ProducesResponseType(typeof(List<DocumentExpiryReportRowDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<List<DocumentExpiryReportRowDto>>> DocumentExpiry([FromQuery] int withinDays, CancellationToken ct)
        => Ok(await _reports.DocumentExpiryReportAsync(withinDays, ct));
}
