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

namespace SkyArtShop.Controllers
{
    [Route("admin/portfolio")]
    [Authorize(Roles="Admin")]
    public class AdminPortfolioController : Controller
    {
        private readonly MongoDBService _mongoService;
        private readonly SlugService _slugService;
        private readonly string _categoriesCollection = "PortfolioCategories";
        private readonly string _projectsCollection = "PortfolioProjects";

        public AdminPortfolioController(MongoDBService mongoService, SlugService slugService)
        {
            _mongoService = mongoService;
            _slugService = slugService;
        }

        [HttpGet("categories")]
        public async Task<IActionResult> Categories()
        {
            var categories = await _mongoService.GetAllAsync<PortfolioCategory>(_categoriesCollection);
            return View(categories.OrderBy(c => c.DisplayOrder).ToList());
        }

        [HttpGet("category/create")]
        public IActionResult CreateCategory() => View(new PortfolioCategory());

        [HttpPost("category/create")]
        public async Task<IActionResult> CreateCategory(PortfolioCategory category)
        {
            if (!ModelState.IsValid)
            {
                return View(category);
            }

            category.CreatedAt = DateTime.UtcNow;
            category.UpdatedAt = DateTime.UtcNow;
            category.Slug = _slugService.GenerateSlug(category.Name);
            await _mongoService.InsertAsync(_categoriesCollection, category);
            TempData["SuccessMessage"] = "Category created successfully!";
            return RedirectToAction("Categories");
        }

        [HttpGet("category/edit/{id}")]
        public async Task<IActionResult> EditCategory(string id)
        {
            var category = await _mongoService.GetByIdAsync<PortfolioCategory>(_categoriesCollection, id);
            if (category == null) return NotFound();
            return View(category);
        }

        [HttpPost("category/edit/{id}")]
        public async Task<IActionResult> EditCategory(string id, PortfolioCategory category)
        {
            if (!ModelState.IsValid)
            {
                return View(category);
            }

            category.Id = id;
            category.UpdatedAt = DateTime.UtcNow;
            category.Slug = _slugService.GenerateSlug(category.Name);
            await _mongoService.UpdateAsync(_categoriesCollection, id, category);
            TempData["SuccessMessage"] = "Category updated successfully!";
            return RedirectToAction("Categories");
        }

        [HttpPost("category/delete/{id}")]
        public async Task<IActionResult> DeleteCategory(string id)
        {
            await _mongoService.DeleteAsync<PortfolioCategory>(_categoriesCollection, id);
            TempData["SuccessMessage"] = "Category deleted successfully!";
            return RedirectToAction("Categories");
        }

        [HttpGet("projects")]
        public async Task<IActionResult> Projects(string? categoryId)
        {
            var projects = await _mongoService.GetAllAsync<PortfolioProject>(_projectsCollection);
            var categories = await _mongoService.GetAllAsync<PortfolioCategory>(_categoriesCollection);
            if (!string.IsNullOrEmpty(categoryId))
            {
                projects = projects.Where(p => p.CategoryId == categoryId).ToList();
            }
            ViewBag.Categories = categories.Where(c => c.IsActive).ToList();
            ViewBag.SelectedCategory = categoryId;
            return View(projects.OrderBy(p => p.DisplayOrder).ToList());
        }

        [HttpGet("project/create")]
        public async Task<IActionResult> CreateProject()
        {
            var categories = await _mongoService.GetAllAsync<PortfolioCategory>(_categoriesCollection);
            ViewBag.Categories = categories.Where(c => c.IsActive).ToList();
            return View(new PortfolioProject());
        }

        [HttpPost("project/create")]
        public async Task<IActionResult> CreateProject(PortfolioProject project)
        {
            if (!ModelState.IsValid)
            {
                var categories = await _mongoService.GetAllAsync<PortfolioCategory>(_categoriesCollection);
                ViewBag.Categories = categories.Where(c => c.IsActive).ToList();
                return View(project);
            }

            project.CreatedAt = DateTime.UtcNow;
            project.UpdatedAt = DateTime.UtcNow;
            await _mongoService.InsertAsync(_projectsCollection, project);
            TempData["SuccessMessage"] = "Project created successfully!";
            return RedirectToAction("Projects");
        }

        [HttpGet("project/edit/{id}")]
        public async Task<IActionResult> EditProject(string id)
        {
            var project = await _mongoService.GetByIdAsync<PortfolioProject>(_projectsCollection, id);
            if (project == null) return NotFound();
            var categories = await _mongoService.GetAllAsync<PortfolioCategory>(_categoriesCollection);
            ViewBag.Categories = categories.Where(c => c.IsActive).ToList();
            return View(project);
        }

        [HttpPost("project/edit/{id}")]
        public async Task<IActionResult> EditProject(string id, PortfolioProject project)
        {
            if (!ModelState.IsValid)
            {
                var categories = await _mongoService.GetAllAsync<PortfolioCategory>(_categoriesCollection);
                ViewBag.Categories = categories.Where(c => c.IsActive).ToList();
                return View(project);
            }

            project.Id = id;
            project.UpdatedAt = DateTime.UtcNow;
            await _mongoService.UpdateAsync(_projectsCollection, id, project);
            TempData["SuccessMessage"] = "Project updated successfully!";
            return RedirectToAction("Projects");
        }

        [HttpPost("project/delete/{id}")]
        public async Task<IActionResult> DeleteProject(string id)
        {
            await _mongoService.DeleteAsync<PortfolioProject>(_projectsCollection, id);
            TempData["SuccessMessage"] = "Project deleted successfully!";
            return RedirectToAction("Projects");
        }
    }
}
