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

namespace SkyArtShop.Controllers;

public class PortfolioController : Controller
{
	private readonly PostgreSQLService _pgService;

	private readonly string _categoriesCollection = "PortfolioCategories";

	private readonly string _projectsCollection = "PortfolioProjects";

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

	public async Task<IActionResult> Index()
	{
		List<PortfolioCategory> model = (from c in await _pgService.GetAllAsync<PortfolioCategory>(_categoriesCollection)
			where c.IsActive
			orderby c.DisplayOrder
			select c).ToList();
		return View(model);
	}

	public async Task<IActionResult> Category(string slug)
	{
		PortfolioCategory category = (await _pgService.GetAllAsync<PortfolioCategory>(_categoriesCollection)).FirstOrDefault((PortfolioCategory c) => c.Slug == slug && c.IsActive);
		if (category == null)
		{
			return NotFound();
		}
		List<PortfolioProject> model = (from p in await _pgService.GetAllAsync<PortfolioProject>(_projectsCollection)
			where p.CategoryId == category.Id && p.IsActive
			orderby p.DisplayOrder
			select p).ToList();
		base.ViewBag.Category = category;
		return View(model);
	}

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