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

namespace SkyArtShop.Controllers
{
    public class PortfolioController : Controller
    {
        private readonly MongoDBService _mongoService;
        private readonly string _categoriesCollection = "PortfolioCategories";
        private readonly string _projectsCollection = "PortfolioProjects";

        public PortfolioController(MongoDBService mongoService)
        {
            _mongoService = mongoService;
        }

        public async Task<IActionResult> Index()
        {
            var categories = await _mongoService.GetAllAsync<PortfolioCategory>(_categoriesCollection);
            var activeCategories = categories.Where(c => c.IsActive).OrderBy(c => c.DisplayOrder).ToList();
            
            return View(activeCategories);
        }

        public async Task<IActionResult> Category(string slug)
        {
            var categories = await _mongoService.GetAllAsync<PortfolioCategory>(_categoriesCollection);
            var category = categories.FirstOrDefault(c => c.Slug == slug && c.IsActive);
            
            if (category == null)
            {
                return NotFound();
            }

            var projects = await _mongoService.GetAllAsync<PortfolioProject>(_projectsCollection);
            var categoryProjects = projects
                .Where(p => p.CategoryId == category.Id && p.IsActive)
                .OrderBy(p => p.DisplayOrder)
                .ToList();

            ViewBag.Category = category;
            return View(categoryProjects);
        }

        public async Task<IActionResult> Project(string id)
        {
            var project = await _mongoService.GetByIdAsync<PortfolioProject>(_projectsCollection, id);
            
            if (project == null)
            {
                return NotFound();
            }

            return View(project);
        }
    }
}
