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;


        // Constructor to inject dependencies
        public HomeController(ILogger<HomeController> logger, BudgetService budgetService, MenuService menuService)
        {
            _logger = logger;
            _budgetService = budgetService;
            _menuService = menuService;
        }

        public IActionResult Index()
        {
            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);
        }



        // Optimized Budget Action to fetch filtered data directly from the database
        public IActionResult Budget(string month, int? year)
        {
            // Default to current month and year if parameters are not provided
            int selectedMonth = !string.IsNullOrEmpty(month)
                ? Array.IndexOf(CultureInfo.CurrentCulture.DateTimeFormat.MonthNames, month) + 1
                : DateTime.Now.Month;

            int selectedYear = year ?? DateTime.Now.Year;

            // Fetch budget items for the selected month and year directly from the database
            var budgetItems = _budgetService.GetBudgetItems(selectedMonth, selectedYear);

            // Create the BudgetModel
            var budgetModel = new BudgetModel
            {
                BudgetItems = budgetItems.ToList() // Ensure this is a list
            };

            // Pass the BudgetModel to the view
            return View(budgetModel);
        }


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