using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Aberwyn.Models;
using Aberwyn.Data;
using Microsoft.EntityFrameworkCore;

namespace Aberwyn.Controllers
{

    [Authorize(Roles = "Admin")]
    public class AdminController : Controller
    {
        private readonly UserManager<ApplicationUser> _userManager;
        private readonly RoleManager<IdentityRole> _roleManager;
        private readonly IConfiguration _configuration;
        private readonly IHostEnvironment _env;
        private readonly ApplicationDbContext _context;

        public AdminController(
            UserManager<ApplicationUser> userManager,
            RoleManager<IdentityRole> roleManager,
            IConfiguration configuration,
            IHostEnvironment env,
            ApplicationDbContext context)
        {
            _userManager = userManager;
            _roleManager = roleManager;
            _configuration = configuration;
            _env = env;
            _context = context;
        }

        public async Task<IActionResult> Index()
        {
            var users = _userManager.Users.ToList();
            var model = new List<AdminUserViewModel>();

            foreach (var user in users)
            {
                var roles = await _userManager.GetRolesAsync(user);
                model.Add(new AdminUserViewModel
                {
                    UserId = user.Id,
                    Email = user.Email,
                    Roles = roles.ToList()
                });
            }
            var allRoles = _roleManager.Roles.Select(r => r.Name).ToList();
            ViewBag.AllRoles = allRoles;
            return View(model);
        }
        [HttpPost]
        public async Task<IActionResult> CreateUser(string email, string password)
        {
            var user = new ApplicationUser { UserName = email, Email = email };
            var result = await _userManager.CreateAsync(user, password);

            if (result.Succeeded)
            {
                TempData["Message"] = "Användare skapad!";
                return RedirectToAction("Index");
            }

            foreach (var error in result.Errors)
            {
                ModelState.AddModelError("", error.Description);
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public async Task<IActionResult> AddToRole(string userId, string role)
        {
            var user = await _userManager.FindByIdAsync(userId);
            if (user != null && await _roleManager.RoleExistsAsync(role))
            {
                await _userManager.AddToRoleAsync(user, role);
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public async Task<IActionResult> RemoveFromRole(string userId, string role)
        {
            var user = await _userManager.FindByIdAsync(userId);
            if (user != null && await _userManager.IsInRoleAsync(user, role))
            {
                await _userManager.RemoveFromRoleAsync(user, role);
            }
            return RedirectToAction("Index");
        }
        [HttpPost]
        public async Task<IActionResult> CreateRole(string roleName)
        {
            if (!string.IsNullOrWhiteSpace(roleName) && !await _roleManager.RoleExistsAsync(roleName))
            {
                await _roleManager.CreateAsync(new IdentityRole(roleName));
            }

            return RedirectToAction("Index");
        }
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public IActionResult ImportMealsFromProd()
        {
            var prodService = MenuService.CreateWithConfig(_configuration, _env, useProdDb: true);
            var devService = new MenuService(_context); // injicerad context används

            var prodMeals = prodService.GetMealsDetailed();

            foreach (var meal in prodMeals)
            {
                // Kopiera utan ID (för att undvika konflikt) och spara
                var newMeal = new Meal
                {
                    Name = meal.Name,
                    Description = meal.Description,
                    ProteinType = meal.ProteinType,
                    Category = meal.Category,
                    CarbType = meal.CarbType,
                    RecipeUrl = meal.RecipeUrl,
                    ImageUrl = meal.ImageUrl,
                    IsAvailable = meal.IsAvailable,
                    CreatedAt = meal.CreatedAt,
                    Instructions = meal.Instructions,
                    ImageData = meal.ImageData,
                    ImageMimeType = meal.ImageMimeType,
                    Ingredients = meal.Ingredients.Select(i => new Ingredient
                    {
                        Quantity = i.Quantity,
                        Item = i.Item
                    }).ToList()
                };

                devService.SaveOrUpdateMealWithIngredients(newMeal);
            }

            return Content("Import klar!");
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public IActionResult ImportMenusFromProd()
        {
            var prodService = MenuService.CreateWithConfig(_configuration, _env, useProdDb: true);
            var devService = new MenuService(_context);

            var allProdMenus = prodService.GetAllWeeklyMenus();
            var allMeals = devService.GetMeals();

            foreach (var menu in allProdMenus)
            {
                var newMenu = new WeeklyMenu
                {
                    DayOfWeek = menu.DayOfWeek,
                    WeekNumber = menu.WeekNumber,
                    Year = menu.Year,
                    Cook = menu.Cook,
                    BreakfastMealId = null,
                    LunchMealId = null,
                    DinnerMealId = null
                };

                if (!string.IsNullOrEmpty(menu.BreakfastMealName))
                    newMenu.BreakfastMealId = allMeals.FirstOrDefault(m => m.Name == menu.BreakfastMealName)?.Id;

                if (!string.IsNullOrEmpty(menu.LunchMealName))
                    newMenu.LunchMealId = allMeals.FirstOrDefault(m => m.Name == menu.LunchMealName)?.Id;

                if (!string.IsNullOrEmpty(menu.DinnerMealName))
                    newMenu.DinnerMealId = allMeals.FirstOrDefault(m => m.Name == menu.DinnerMealName)?.Id;

                _context.WeeklyMenus.Add(newMenu);
            }

            _context.SaveChanges();
            TempData["Message"] = "Import av veckomenyer klar.";
            return RedirectToAction("Index");
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public IActionResult ImportBudgetFromProd()
        {
            // Hämta connection till produktion
            using var prodContext = ApplicationDbContextFactory.CreateWithConfig(_configuration, _env, useProdDb: true);

            // Importera definitioner först
            var prodCategoryDefs = prodContext.BudgetCategoryDefinitions.ToList();
            var prodItemDefs = prodContext.BudgetItemDefinitions.ToList();

            foreach (var def in prodCategoryDefs)
            {
                if (!_context.BudgetCategoryDefinitions.Any(d => d.Name == def.Name))
                {
                    _context.BudgetCategoryDefinitions.Add(new BudgetCategoryDefinition
                    {
                        Name = def.Name,
                        Color = def.Color ?? "#cccccc"
                    });

                }
            }

            foreach (var def in prodItemDefs)
            {
                if (!_context.BudgetItemDefinitions.Any(d => d.Name == def.Name))
                {
                    _context.BudgetItemDefinitions.Add(new BudgetItemDefinition { Name = def.Name });
                }
            }

            _context.SaveChanges(); // Se till att ID:n finns för FK:n nedan

            // Ladda definitioner i minnet för snabb lookup
            var devCategoryDefs = _context.BudgetCategoryDefinitions.ToList();
            var devItemDefs = _context.BudgetItemDefinitions.ToList();

            // Importera budgetperioder med kategorier och items
            var prodPeriods = prodContext.BudgetPeriods
                .Include(p => p.Categories)
                    .ThenInclude(c => c.Items)
                .ToList();

            foreach (var period in prodPeriods)
            {
                var exists = _context.BudgetPeriods
                    .Any(p => p.Year == period.Year && p.Month == period.Month);

                if (exists)
                    continue;

                var newPeriod = new BudgetPeriod
                {
                    Year = period.Year,
                    Month = period.Month,
                    Categories = period.Categories.Select(c => new BudgetCategory
                    {
                        Name = c.Name,
                        Color = string.IsNullOrWhiteSpace(c.Color) ? "#cccccc" : c.Color,
                        Order = c.Order,
                        BudgetCategoryDefinitionId = devCategoryDefs
                            .FirstOrDefault(d => d.Name == c.Definition?.Name)?.Id,
                        Items = c.Items.Select(i => new BudgetItem
                        {
                            Name = i.Name,
                            Amount = i.Amount,
                            IsExpense = i.IsExpense,
                            IncludeInSummary = i.IncludeInSummary,
                            Order = i.Order,
                            BudgetItemDefinitionId = devItemDefs
                                .FirstOrDefault(d => d.Name == i.BudgetItemDefinition?.Name)?.Id
                        }).ToList()
                    }).ToList()
                };

                _context.BudgetPeriods.Add(newPeriod);
            }

            _context.SaveChanges();
            TempData["Message"] = "✅ Import av budgetdata från produktion är klar.";
            return RedirectToAction("Index");
        }


        //Todo

        [HttpGet]
        public IActionResult Todo()
        {
            return View();
        }


        [HttpGet]
        public IActionResult GetTodoTasks()
        {
            var tasks = _context.TodoTasks
                .OrderByDescending(t => t.CreatedAt)
                .ToList();
            return Json(tasks);
        }

        [HttpPost]
        public IActionResult AddTodoTask([FromBody] TodoTask task)
        {
            if (string.IsNullOrWhiteSpace(task?.Title))
                return BadRequest("Titel krävs");

            task.CreatedAt = DateTime.UtcNow;
            _context.TodoTasks.Add(task);
            _context.SaveChanges();

            return Json(task);
        }

        [HttpPost]
        public IActionResult UpdateTodoTask([FromBody] TodoTask task)
        {
            var existing = _context.TodoTasks.FirstOrDefault(t => t.Id == task.Id);
            if (existing == null)
                return NotFound();

            existing.Title = task.Title;
            existing.Status = task.Status;
            existing.Priority = task.Priority;

            _context.SaveChanges();

            return Ok();
        }




    }

        public class AdminUserViewModel
        {
            public string UserId { get; set; }
            public string Email { get; set; }
            public List<string> Roles { get; set; }
        }
}
