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

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

            if (response.IsSuccess)
            {

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