using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using WorkFlowCheck.Common.Enums;
using WorkFlowCheck.Web.Services.Interfaces;

namespace WorkFlowCheck.Web.Pages.CheckList.CheckListHeader
{
    public class CheckListHeaderPageModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;
        private readonly ICheckListService _checkListService;

        public List<SelectListItem> CheckStatus { get; set; }

        public CheckListHeaderPageModel(ILogger<IndexModel> logger, ICheckListService checkListService)
        {
            _logger = logger;
            _checkListService = checkListService;
        }
        public async Task OnGet()
        {
            CheckStatus = _checkListService.GetCheckStatus();
        }


        public async Task<JsonResult> OnGetLoadCheckListHeaders()
        {
            var results = await _checkListService.GetAllCheckListHeaderAsync();
            return new JsonResult(new { data = results });
        }

        public async Task<IActionResult> OnGetCreatePDF(int id)
        {
            var checkListHeaderDTO = await _checkListService.GetCheckListHeader(id);

            var response = await _checkListService.CreatePDF(id);

            if (response.IsSuccess)
            {
                var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
                var fileName = $"PH_{timestamp}_{checkListHeaderDTO.Id}.pdf";
                var result = new FileContentResult(response.Data, "application/pdf")
                {
                    FileDownloadName = fileName,
                    FileContents = response.Data
                };
                return result;
            }
            else
            {
                return new JsonResult(new { success = false });
            }
        }
    }
}
