using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using WorkFlowCheck.Web.Services.Interfaces;

namespace WorkFlowCheck.Web.Pages.BaseStock.PDFFiles
{
    [Authorize]
    public class PDFFilesPageModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;
        private readonly ISystemService _systemService;
        public PDFFilesPageModel(ILogger<IndexModel> logger, ISystemService systemService)
        {
            _logger = logger;
            _systemService = systemService;
        }
        public async Task OnGet()
        {
        }
        public async Task<JsonResult> OnGetLoadPDFFiles()
        {
            var results = await _systemService.GetAllPDFFilesAsync();
            return new JsonResult(new { data = results });
        }
        public async Task<IActionResult> OnGetDownloadFile(string filename)
        {
            var response = await _systemService.DownloadPDFFile(filename);
                       

            if (response.IsSuccess)
            {

                var result = new FileContentResult(response.Data, "application/pdf")
                {
                    FileDownloadName = filename,
                    FileContents = response.Data
                };
                return result;
            }
            else
            {
                return new JsonResult(new { success = false });
            }
        }
    }
}
