using ClosedXML.Excel;
using SolutionErp.Application.Forms.Services;
using SolutionErp.Application.Office;
using SolutionErp.Application.Reports.Services;

namespace SolutionErp.Infrastructure.Reports;

// Phase 11 P11-E — mirror ContractExcelExporter (ClosedXML XLWorkbook → MemoryStream → RenderResult).
// Input = AttendanceReportDto (controller query rồi pass — KHÔNG đụng DB).
public class AttendanceReportExcelExporter : IAttendanceReportExcelExporter
{
    public RenderResult Export(AttendanceReportDto report)
    {
        using var wb = new XLWorkbook();
        var ws = wb.Worksheets.Add("ChamCong");

        // Title row
        var headers = new[]
        {
            "STT", "Họ tên", "Phòng ban", "Ngày công", "Tổng giờ làm",
            "OT thường", "OT cuối tuần", "OT lễ", "OT quy đổi"
        };
        var titleRange = ws.Range(1, 1, 1, headers.Length);
        titleRange.Merge();
        titleRange.Value = $"BÁO CÁO CHẤM CÔNG THÁNG {report.Month}/{report.Year}";
        titleRange.Style.Font.Bold = true;
        titleRange.Style.Font.FontSize = 14;
        titleRange.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;

        // Header row
        const int headerRowIdx = 2;
        for (int i = 0; i < headers.Length; i++)
            ws.Cell(headerRowIdx, i + 1).Value = headers[i];

        var headerRange = ws.Range(headerRowIdx, 1, headerRowIdx, headers.Length);
        headerRange.Style.Font.Bold = true;
        headerRange.Style.Fill.BackgroundColor = XLColor.LightBlue;
        headerRange.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;

        // Data rows
        const int firstDataRow = headerRowIdx + 1;
        for (int i = 0; i < report.Rows.Count; i++)
        {
            var r = report.Rows[i];
            var rowIdx = firstDataRow + i;
            ws.Cell(rowIdx, 1).Value = i + 1;
            ws.Cell(rowIdx, 2).Value = r.FullName;
            ws.Cell(rowIdx, 3).Value = r.DepartmentName ?? "—";
            ws.Cell(rowIdx, 4).Value = r.DaysPresent;
            ws.Cell(rowIdx, 5).Value = r.TotalWorkHours;
            ws.Cell(rowIdx, 5).Style.NumberFormat.Format = "#,##0.0#";
            ws.Cell(rowIdx, 6).Value = r.OtWeekday;
            ws.Cell(rowIdx, 6).Style.NumberFormat.Format = "#,##0.0#";
            ws.Cell(rowIdx, 7).Value = r.OtWeekend;
            ws.Cell(rowIdx, 7).Style.NumberFormat.Format = "#,##0.0#";
            ws.Cell(rowIdx, 8).Value = r.OtHoliday;
            ws.Cell(rowIdx, 8).Style.NumberFormat.Format = "#,##0.0#";
            ws.Cell(rowIdx, 9).Value = r.OtWeighted;
            ws.Cell(rowIdx, 9).Style.NumberFormat.Format = "#,##0.0#";
        }

        // Footer total row
        if (report.Rows.Count > 0)
        {
            var sumRow = firstDataRow + report.Rows.Count;
            ws.Cell(sumRow, 4).Value = "TỔNG:";
            ws.Cell(sumRow, 4).Style.Font.Bold = true;
            ws.Cell(sumRow, 4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
            ws.Cell(sumRow, 5).Value = report.GrandTotalWorkHours;
            ws.Cell(sumRow, 5).Style.NumberFormat.Format = "#,##0.0#";
            ws.Cell(sumRow, 5).Style.Font.Bold = true;
            ws.Cell(sumRow, 9).Value = report.GrandTotalOtWeighted;
            ws.Cell(sumRow, 9).Style.NumberFormat.Format = "#,##0.0#";
            ws.Cell(sumRow, 9).Style.Font.Bold = true;
        }

        ws.Columns().AdjustToContents();
        ws.SheetView.FreezeRows(headerRowIdx);

        using var ms = new MemoryStream();
        wb.SaveAs(ms);
        return new RenderResult(
            ms.ToArray(),
            $"BaoCao-ChamCong-{report.Year}-{report.Month:D2}.xlsx",
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    }
}
