using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using SkyArtShop.Models;
using SkyArtShop.Services;

namespace SkyArtShop.Controllers
{
    [Route("admin")]
    [Authorize(Roles = "Admin")]
    public class AdminController : Controller
    {
        private readonly MongoDBService _mongoService;
        private readonly SignInManager<SkyArtShop.Data.ApplicationUser> _signInManager;
        private readonly UserManager<SkyArtShop.Data.ApplicationUser> _userManager;

        public AdminController(MongoDBService mongoService,
            SignInManager<SkyArtShop.Data.ApplicationUser> signInManager,
            UserManager<SkyArtShop.Data.ApplicationUser> userManager)
        {
            _mongoService = mongoService;
            _signInManager = signInManager;
            _userManager = userManager;
        }

        [HttpGet("login")]
        [AllowAnonymous]
        public IActionResult Login()
        {
            if (User.Identity?.IsAuthenticated == true)
            {
                return RedirectToAction("Dashboard");
            }
            return View();
        }

        [HttpPost("login")]
        [AllowAnonymous]
        public async Task<IActionResult> Login(string email, string password)
        {
            var user = await _userManager.FindByEmailAsync(email);
            if (user == null)
            {
                ViewBag.Error = "Invalid email or password";
                return View();
            }
            var result = await _signInManager.PasswordSignInAsync(user, password, true, false);
            if (!result.Succeeded)
            {
                ViewBag.Error = "Invalid email or password";
                return View();
            }
            return RedirectToAction("Dashboard");
        }

        [HttpGet("logout")]
        public async Task<IActionResult> Logout()
        {
            await _signInManager.SignOutAsync();
            return RedirectToAction("Login");
        }

        [HttpGet("dashboard")]
        public async Task<IActionResult> Dashboard()
        {
            var products = await _mongoService.GetAllAsync<Product>("Products");
            var projects = await _mongoService.GetAllAsync<PortfolioProject>("PortfolioProjects");
            var blogPosts = await _mongoService.GetAllAsync<BlogPost>("BlogPosts");
            var pages = await _mongoService.GetAllAsync<Page>("Pages");
            var settings = (await _mongoService.GetAllAsync<SiteSettings>("SiteSettings")).FirstOrDefault();

            ViewBag.ProductCount = products.Count;
            ViewBag.ProjectCount = projects.Count;
            ViewBag.BlogCount = blogPosts.Count;
            ViewBag.PageCount = pages.Count;
            ViewBag.SiteName = settings?.SiteName ?? "Sky Art Shop";
            ViewBag.AdminEmail = User.Identity?.Name;

            return View();
        }

        [HttpGet("")]
        public IActionResult Index() => RedirectToAction("Dashboard");
    }
}
