using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SkyArtShop.Models;
using SkyArtShop.Services;

namespace SkyArtShop.Controllers;

[Route("admin/menu")]
[Authorize(Roles = "Admin,MasterAdmin")]
public class AdminMenuController : Controller
{
	private readonly PostgreSQLService _pgService;

	public AdminMenuController(PostgreSQLService pgService)
	{
		_pgService = pgService;
	}

	[HttpGet("")]
	public async Task<IActionResult> Index()
	{
		return View((await _pgService.GetAllAsync<MenuItem>("MenuItems")).OrderBy((MenuItem m) => m.DisplayOrder).ToList());
	}

	[HttpPost("reseed")]
	public async Task<IActionResult> ReseedMenu()
	{
		foreach (MenuItem item in await _pgService.GetAllAsync<MenuItem>("MenuItems"))
		{
			await _pgService.DeleteAsync<MenuItem>("MenuItems", item.Id);
		}
		MenuItem[] array = new MenuItem[10]
		{
			new MenuItem
			{
				Label = "Home",
				Url = "/",
				DisplayOrder = 1,
				IsActive = true,
				ShowInNavbar = true,
				ShowInDropdown = true
			},
			new MenuItem
			{
				Label = "Shop",
				Url = "/Shop",
				DisplayOrder = 2,
				IsActive = true,
				ShowInNavbar = true,
				ShowInDropdown = true
			},
			new MenuItem
			{
				Label = "Top Sellers",
				Url = "/#top-sellers",
				DisplayOrder = 3,
				IsActive = true,
				ShowInNavbar = true,
				ShowInDropdown = true
			},
			new MenuItem
			{
				Label = "Promotion",
				Url = "/#promotion",
				DisplayOrder = 4,
				IsActive = true,
				ShowInNavbar = true,
				ShowInDropdown = true
			},
			new MenuItem
			{
				Label = "Portfolio",
				Url = "/Portfolio",
				DisplayOrder = 5,
				IsActive = true,
				ShowInNavbar = true,
				ShowInDropdown = true
			},
			new MenuItem
			{
				Label = "Blog",
				Url = "/Blog",
				DisplayOrder = 6,
				IsActive = true,
				ShowInNavbar = true,
				ShowInDropdown = true
			},
			new MenuItem
			{
				Label = "About",
				Url = "/About",
				DisplayOrder = 7,
				IsActive = true,
				ShowInNavbar = true,
				ShowInDropdown = true
			},
			new MenuItem
			{
				Label = "Instagram",
				Url = "#instagram",
				DisplayOrder = 8,
				IsActive = true,
				ShowInNavbar = false,
				ShowInDropdown = true
			},
			new MenuItem
			{
				Label = "Contact",
				Url = "/Contact",
				DisplayOrder = 9,
				IsActive = true,
				ShowInNavbar = true,
				ShowInDropdown = true
			},
			new MenuItem
			{
				Label = "My Wishlist",
				Url = "#wishlist",
				DisplayOrder = 10,
				IsActive = true,
				ShowInNavbar = false,
				ShowInDropdown = true
			}
		};
		MenuItem[] array2 = array;
		foreach (MenuItem entity in array2)
		{
			await _pgService.InsertAsync("MenuItems", entity);
		}
		base.TempData["SuccessMessage"] = "Menu items reseeded successfully!";
		return RedirectToAction("Index");
	}

	[HttpGet("create")]
	public IActionResult Create()
	{
		return View(new MenuItem());
	}

	[HttpPost("create")]
	public async Task<IActionResult> Create(MenuItem menuItem)
	{
		if (!base.ModelState.IsValid)
		{
			return View(menuItem);
		}
		menuItem.CreatedAt = DateTime.UtcNow;
		await _pgService.InsertAsync("MenuItems", menuItem);
		base.TempData["SuccessMessage"] = "Menu item created successfully!";
		return RedirectToAction("Index");
	}

	[HttpGet("edit/{id}")]
	public async Task<IActionResult> Edit(string id)
	{
		MenuItem menuItem = await _pgService.GetByIdAsync<MenuItem>("MenuItems", id);
		if (menuItem == null)
		{
			return NotFound();
		}
		return View(menuItem);
	}

	[HttpPost("edit/{id}")]
	public async Task<IActionResult> Edit(string id, MenuItem menuItem)
	{
		if (!base.ModelState.IsValid)
		{
			return View(menuItem);
		}
		menuItem.Id = id;
		await _pgService.UpdateAsync("MenuItems", id, menuItem);
		base.TempData["SuccessMessage"] = "Menu item updated successfully!";
		return RedirectToAction("Index");
	}

	[HttpPost("delete/{id}")]
	public async Task<IActionResult> Delete(string id)
	{
		await _pgService.DeleteAsync<MenuItem>("MenuItems", id);
		base.TempData["SuccessMessage"] = "Menu item deleted successfully!";
		return RedirectToAction("Index");
	}
}
