using BMA.EHR.Recurit.Exam.Service.Extensions;
using BMA.EHR.Recurit.Exam.Service.Controllers;
using BMA.EHR.Recurit.Exam.Service.Services;
using BMA.EHR.Recurit.Exam.Service.Data;
using BMA.EHR.Recurit.Exam.Service.Responses;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Swashbuckle.AspNetCore.Annotations;
using Telerik.Reporting;
using Telerik.Reporting.Processing;
using BMA.EHR.Recurit.Exam.Service.Response;
using BMA.EHR.Recurit.Exam.Service.Models;
using OfficeOpenXml.Style;
using OfficeOpenXml;
using System.Drawing;
using Nest;

namespace BMA.EHR.Recurit.Exam.Service.Controllers
{
    [Route("api/v{version:apiVersion}/candidate/disable-exam/report")]
    [ApiVersion("1.0")]
    [ApiController]
    [Produces("application/json")]
    [Authorize]
    [SwaggerTag("รายงานข้อมูลการสอบแข่งขัน")]
    public class DisableReportController : BaseController
    {
        #region " Fields "

        private readonly ApplicationDbContext _context;
        private readonly IWebHostEnvironment _hostingEnvironment;
        private readonly IConfiguration _configuration;
        private readonly string space = "ㅤ";

        #endregion

        #region " Constructor and Destructor "

        public DisableReportController(ApplicationDbContext context,
                                       IWebHostEnvironment hostingEnvironment,
                                       IConfiguration configuration,
                                       DisableService disableService)
        {
            this._context = context;
            this._hostingEnvironment = hostingEnvironment;
            this._configuration = configuration;
        }

        #endregion
        private int GetExamCountTes(string citizenId)
        {
            try
            {
                var count = _context.Candidates.AsQueryable()
                                    .Where(x => x.CitizenId == citizenId)
                                    .Count();

                return count;
            }
            catch
            {
                throw;
            }
        }

        #region " Methods "

        /// <summary>
        /// แสดงหนังสือรับรอง
        /// </summary>
        /// <param name="id">รหัสรอบการสอบ</param>
        /// <param name="examId">เลขประจำตัวผู้สมัครสอบ</param>
        /// <param name="type">ชนิดของรายงาน</param>
        /// <returns></returns>
        /// <response code="200">เมื่อแสดงรายงานสำเร็จ</response>
        /// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
        /// <response code="401">ไม่ได้ Login เข้าระบบ</response>
        /// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>

        [HttpGet("certificate/{type:int}/{id:length(36)}/{examId}")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status400BadRequest)]
        [ProducesResponseType(StatusCodes.Status401Unauthorized)]
        [ProducesResponseType(StatusCodes.Status500InternalServerError)]
        public async Task<ActionResult<ResponseObject>> GetCertificateReportAsync(Guid id, string examId, int type)
        {
            try
            {
                var data = await _context.Disables.AsQueryable()
                                    .Include(x => x.PeriodExam)
                                    .Where(x => x.PeriodExam.Id == id)
                                    .Where(x => x.ExamId == examId)
                                    .Join(_context.DisableScores.AsQueryable()
                                            .Include(x => x.ScoreImport),
                                            rc => new { rc.PeriodExam.Year, rc.ExamId },
                                            sc => new { sc.ScoreImport.Year, sc.ExamId },
                                            (p, sr) => new
                                            {
                                                ExamID = p.ExamId,
                                                p.CitizenId,
                                                Order = p.PeriodExam.Round,
                                                Year = p.PeriodExam.Year.Value.ToThaiYear(),
                                                FullName = $"{p.Prefix}{p.FirstName} {p.LastName}",
                                                ExamResult = sr == null ? "" : sr.ExamStatus,
                                                EndDate = p.PeriodExam.RegisterEndDate == null ? null : p.PeriodExam.RegisterEndDate.Value.ToThaiFullDate3(),
                                                AuthName = p.PeriodExam.AuthName,
                                                AuthPosition = p.PeriodExam.AuthPosition,
                                            })
                                    .FirstOrDefaultAsync();

                var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptCertificate{type}.trdp");

                ReportPackager reportPackager = new ReportPackager();
                Telerik.Reporting.Report? report = null;
                using (var sourceStream = System.IO.File.OpenRead(rptFile))
                {
                    report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
                }

                report.DataSource = data;
                System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();

                InstanceReportSource instanceReportSource = new InstanceReportSource()
                {
                    ReportDocument = report
                };


                ReportProcessor reportProcessor = new ReportProcessor(_configuration);
                RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo);

                var content = result.DocumentBytes;
                return File(content, "application/pdf", $"หนังสือรับรอง_{data.CitizenId}_{data.FullName}.pdf");

            }
            catch (Exception ex)
            {
                return Error(ex, "เกิดข้อผิดพลาดในการแสดงรายงาน");
            }
        }

        /// <summary>
        /// แสดงเอกสารผลสอบ
        /// </summary>
        /// <param name="id">รหัสรอบการสอบ</param>
        /// <param name="examId">เลขประจำตัวผู้สมัครสอบ</param>
        /// <returns></returns>
        /// <response code="200">เมื่อแสดงรายงานสำเร็จ</response>
        /// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
        /// <response code="401">ไม่ได้ Login เข้าระบบ</response>
        /// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
        [HttpGet("score/{id:length(36)}/{examId}")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status400BadRequest)]
        [ProducesResponseType(StatusCodes.Status401Unauthorized)]
        [ProducesResponseType(StatusCodes.Status500InternalServerError)]
        public async Task<ActionResult<ResponseObject>> GetScoreReportAsync(Guid id, string examId)
        {
            try
            {
                var data = await _context.Disables.AsQueryable()
                                    .Include(x => x.PeriodExam)
                                    .Include(x => x.Documents)
                                    .ThenInclude(x => x.DocumentFile)
                                    .Where(x => x.PeriodExam.Id == id)
                                    .Where(x => x.ExamId == examId)
                                    .Join(_context.DisableScores.AsQueryable()
                                            .Include(x => x.ScoreImport),
                                            rc => new { rc.PeriodExam.Year, rc.ExamId },
                                            sc => new { sc.ScoreImport.Year, sc.ExamId },
                                            (p, sr) => new
                                            {
                                                ExamId = p.ExamId,
                                                CitizenId = p.CitizenId,
                                                p.Prefix,
                                                FullName = $"{p.Prefix}{p.FirstName} {p.LastName}",
                                                DateOfBirth = p.DateOfBirth.HasValue && p.DateOfBirth.Value != DateTime.MinValue 
                                                    ? p.DateOfBirth.Value.ToThaiShortDate() 
                                                    : "",
                                                Gender = p.Gendor,
                                                Degree = p.Educations.First().Degree,
                                                Major = p.Educations.First().Major,
                                                ExamResult = sr == null ? "" : sr.ExamStatus,
                                                University = p.Educations.First().University,
                                                PositionName = p.PositionName,
                                                PositionType = p.PositionType,
                                                PositionLevel = p.PositionLevel,
                                                ExamName = $"{p.PeriodExam.Name} ครั้งที่ {p.PeriodExam.Round}/{p.PeriodExam.Year.Value.ToThaiYear()}",
                                                Number = sr == null ? "" : sr.Number,
                                                // ExamCount = 10,
                                                // ExamCount = GetExamCountTes(p.CitizenId),
                                                ScoreExpire = p.PeriodExam.AnnouncementDate == null ? "" : p.PeriodExam.AnnouncementDate.Value.AddYears(2).ToThaiShortDate(),
                                                FullA = sr == null ? 0 : sr.FullA,
                                                SumA = sr == null ? 0 : sr.SumA,
                                                FullB = sr == null ? 0 : sr.FullB,
                                                SumB = sr == null ? 0 : sr.SumB,
                                                FullC = sr == null ? 0 : sr.FullC,
                                                SumC = sr == null ? 0 : sr.SumC,
                                            })
                                    .FirstOrDefaultAsync();
                var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptExamResult.trdp");
                ReportPackager reportPackager = new ReportPackager();
                Telerik.Reporting.Report report = null;
                using (var sourceStream = System.IO.File.OpenRead(rptFile))
                {
                    report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
                }

                report.DataSource = data;
                if (data != null)
                {
                    var _data = new
                    {
                        ExamId = data.ExamId,
                        CitizenId = data.CitizenId,
                        Prefix = data.Prefix,
                        FullName = data.FullName,
                        DateOfBirth = data.DateOfBirth,
                        Gender = data.Gender,
                        Degree = data.Degree,
                        Major = data.Major,
                        ExamResult = data.ExamResult,
                        University = data.University,
                        PositionName = data.PositionName,
                        ExamName = data.ExamName,
                        Number = data.Number,
                        ExamCount = GetExamCountTes(data.CitizenId),
                        ScoreExpire = data.ScoreExpire,
                        FullA = data.FullA,
                        SumA = data.SumA,
                        FullB = data.FullB,
                        SumB = data.SumB,
                        FullC = data.FullC,
                        SumC = data.SumC,
                    };
                    report.DataSource = _data;
                }
                System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();

                InstanceReportSource instanceReportSource = new InstanceReportSource()
                {
                    ReportDocument = report
                };


                ReportProcessor reportProcessor = new ReportProcessor(_configuration);
                RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo);

                var content = result.DocumentBytes;
                return File(content, "application/pdf", $"ผลคะแนนสอบ_{data.CitizenId}_{data.FullName}.pdf");
            }
            catch (Exception ex)
            {
                return Error(ex, "เกิดข้อผิดพลาดในการแสดงรายงาน");
            }
        }

        /// <summary>
        /// รายงานรายชื่อผู้มีสิทธิ์สอบ
        /// </summary>
        /// <param name="id">รหัสรอบการสอบแข่งขัน</param>
        /// <returns></returns>
        [HttpGet("candidate-new/{id:length(36)}")]
        public async Task<IActionResult> GetCandidateNewListReportAsync(Guid id)
        {
            var data = await _context.Disables.AsQueryable()
                .Include(x => x.PeriodExam)
                .Where(x => x.PeriodExam.Id == id)
                .OrderBy(x => x.ExamId)
                .Select(p => new
                {
                    FullName = $"{p.Prefix}{p.FirstName} {p.LastName}",
                    PositionName = p.PositionName,
                    Remark = p.Remark != null ? p.Remark.ToThaiNumber() : "",
                    RefNo1 = p.Payments.Select(x => x.RefNo1).FirstOrDefault() != null
                        ? p.Payments.Select(x => x.RefNo1).FirstOrDefault().ToThaiNumber()
                        : "",
                    ExamName =
                        $"{p.PeriodExam.Name} ครั้งที่ {(p.PeriodExam != null ? p.PeriodExam.Round.ToString().ToThaiNumber() : "")}/{(p.PeriodExam.Year > 2500 ? p.PeriodExam.Year.ToString().ToThaiNumber() : (p.PeriodExam.Year+543).ToString().ToThaiNumber())}",
                }).ToListAsync();

            if (data.Count == 0)
            {
                return NotFound(new { Message = "ไม่พบข้อมูลในระบบ" });
            }

            var examName = data[0].ExamName;

            var groupData = data
                .GroupBy(x => x.PositionName)
                .Select(g => new
                {
                    PositionName = $"ตำแหน่ง {g.Key}",
                    Total = $"จำนวน {g.Count().ToString().ToThaiNumber()} ราย",
                    Persons = g.Select((x, index) => new
                    {
                        No = (index + 1).ToString().ToThaiNumber(),
                        RefNo1 = x.RefNo1,
                        FullName = x.FullName,
                        Remark = x.Remark
                    }).ToList()
                })
                .ToList();

            using var package = new ExcelPackage();
            var ws = package.Workbook.Worksheets.Add(examName);
            // ตั้งค่า Font ทั้ง sheet
            ws.Cells.Style.Font.Name = "TH Sarabun PSK";
            ws.Cells.Style.Font.Size = 14;
            int row = 1;

            // ======= Header =======
            ws.Cells[row, 1, row + 2, 4].Merge = true;
            ws.Cells[row, 1].Value = $"รายชื่อผู้มีสิทธิ์สอบ" +
                                     $"{examName}";

            ws.Cells[row, 1].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
            ws.Cells[row, 1].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
            ws.Cells[row, 1].Style.Font.Bold = true;
            ws.Cells[row, 1].Style.WrapText = true;

            row += 4;

            foreach (var group in groupData)
            {
                // ======= Position Header =======
                ws.Cells[row, 1, row + 1, 4].Merge = true; // merge 2 แถว และ column A-D
                ws.Cells[row, 1].Value = $"{group.PositionName}\n{group.Total}";
                ws.Cells[row, 1].Style.Font.Bold = true;
                ws.Cells[row, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                ws.Cells[row, 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                ws.Cells[row, 1].Style.WrapText = true;

                row += 2; // ขยับ row ไปหลัง header ที่ merge 2 แถว

                // ======= Table Header =======
                ws.Cells[row, 1].Value = "ลำดับ";
                ws.Cells[row, 2].Value = "เลขประจำตัวสอบ";
                ws.Cells[row, 3].Value = "ชื่อ-สกุล";
                ws.Cells[row, 4].Value = "หมายเหตุ";

                ws.Cells[row, 1, row, 4].Style.Font.Bold = true;
                ws.Cells[row, 1, row, 4].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                ws.Cells[row, 1, row, 4].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                ws.Cells[row, 1, row, 4].Style.Fill.PatternType = ExcelFillStyle.Solid;
                ws.Cells[row, 1, row, 4].Style.Fill.BackgroundColor.SetColor(Color.LightGray);

                row++;

                // ======= Table Detail =======
                foreach (var person in group.Persons)
                {
                    ws.Cells[row, 1].Value = person.No;
                    ws.Cells[row, 2].Value = person.RefNo1;
                    ws.Cells[row, 3].Value = person.FullName;
                    ws.Cells[row, 4].Value = person.Remark;

                    // จัดกลางเฉพาะ No และ RefNo1
                    ws.Cells[row, 1, row, 2].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    ws.Cells[row, 1, row, 2].Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    // เพิ่มกรอบให้แต่ละ cell
                    for (int col = 1; col <= 4; col++)
                    {
                        ws.Cells[row, col].Style.Border.Top.Style = ExcelBorderStyle.Thin;
                        ws.Cells[row, col].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
                        ws.Cells[row, col].Style.Border.Left.Style = ExcelBorderStyle.Thin;
                        ws.Cells[row, col].Style.Border.Right.Style = ExcelBorderStyle.Thin;
                    }

                    row++;
                }

                row++;
            }

            ws.Cells.AutoFitColumns();

            var fileBytes = package.GetAsByteArray();
            var fileName = $"รายชื่อผู้สอบ_{examName}.xlsx";

            return File(fileBytes,
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                fileName);
        }

        #endregion
    }
}
