using Microsoft.AspNetCore.Mvc;
using ReportingServices.Shared.HelperClasses;
using ReportingServices.Shared.Models.ProductionReport;
using ReportingServices.Shared.ViewModels.ProductionReport;

namespace ReportingServices.UI.Controllers
{
    public class ProductionReportController : Controller
    {
        private readonly ILogger<ProductionReportController> _logger;
        private readonly string _dailyRptFilePath = "wwwroot/Assets/DailyReportInfo.json";
        private readonly string _toolStateOwnerFilePath = "wwwroot/Assets/ToolStates.json";

        public ProductionReportController(ILogger<ProductionReportController> logger)
        {
            _logger = logger;
        }


        public IActionResult Index()
        {
            return View();
        }

        public IActionResult DailyReport()
        {
            try
            {
                DailyReport dailyReport = DailyReportHelper.SetUpDailyReport();
                Dictionary<string, List<string>> toolStateOwners = JsonFileHandler.LoadJSONFile<Dictionary<string, List<string>>>(_toolStateOwnerFilePath);

                dailyReport.ToolStatesByOwner = toolStateOwners;

                return View(dailyReport);
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex, "Failed to load report");
                return View();
            }
        }

        public IActionResult EditDailyReport()
        {
            ManualReportEntries entries = JsonFileHandler.LoadJSONFile<ManualReportEntries>(_dailyRptFilePath);

            return View(entries);
        }

        [HttpPost]
        public IActionResult EditDailyReport(ManualReportEntries rpt)
        {
            JsonFileHandler.SaveJSONFile(rpt, _dailyRptFilePath);

            return RedirectToAction("DailyReport");
        }
    }
}
