using Aberwyn.Data;
using Aberwyn.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Globalization;
using System.Linq;

namespace Aberwyn.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        //private readonly BudgetService _budgetService;
        private readonly MenuService _menuService;
        private readonly ApplicationDbContext _context;


        // Constructor to inject dependencies
        public HomeController(ApplicationDbContext applicationDbContext, ILogger<HomeController> logger, MenuService menuService)
        {
            _logger = logger;
            _menuService = menuService;
            _context = applicationDbContext;
        }

        public IActionResult Index()
        {
            var isOpen = _context.AppSettings.FirstOrDefault(x => x.Key == "RestaurantIsOpen")?.Value == "True";
            ViewBag.RestaurantIsOpen = isOpen;
            return View();
        }

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

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

        public IActionResult Menu()
        {

            ViewData["HideSidebar"] = true;
            // Get the current date and week
            var currentDate = DateTime.Now;
            var currentYear = currentDate.Year;
            var calendar = new GregorianCalendar();
            var currentWeek = calendar.GetWeekOfYear(currentDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

            // Fetch weekly menu items for the current week and year
            var weeklyMenus = _menuService.GetWeeklyMenu(currentWeek, currentYear);
            var meals = _menuService.GetMeals();  // Fetch all meals

            var model = new MenuViewModel
            {
                WeeklyMenus = weeklyMenus,  // List of WeeklyMenu items
                Meals = meals,  // List of available meals
                WeekNumber = currentWeek,  // Dynamically set the current week number
                Year = currentYear  // Dynamically set the current year
            };

            return View(model);
        }

        public IActionResult Budget()
        {
            ViewData["HideSidebar"] = true;
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
