using System;
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 AboutController : Controller
{
	private readonly PostgreSQLService _pgService;

	private readonly string _pagesCollection = "Pages";

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

	public async Task<IActionResult> Index()
	{
		Page page = (await _pgService.GetAllAsync<Page>(_pagesCollection)).FirstOrDefault((Page p) => p.PageSlug == "about" && p.IsActive);
		Console.WriteLine($"[ABOUT] Found About page: {page != null}");
		if (page != null)
		{
			Console.WriteLine("[ABOUT] Title: " + page.Title);
			Console.WriteLine($"[ABOUT] Content length: {page.Content?.Length ?? 0}");
			Console.WriteLine($"[ABOUT] Image Gallery Count: {page.ImageGallery?.Count ?? 0}");
			Console.WriteLine($"[ABOUT] Team Members Count: {page.TeamMembers?.Count ?? 0}");
			if (page.ImageGallery != null && page.ImageGallery.Any())
			{
				Console.WriteLine("[ABOUT] Gallery Images: " + string.Join(", ", page.ImageGallery));
			}
			if (page.TeamMembers != null && page.TeamMembers.Any())
			{
				foreach (TeamMember teamMember in page.TeamMembers)
				{
					Console.WriteLine($"[ABOUT] Team: {teamMember.Name} ({teamMember.Role}) - Photo: {teamMember.PhotoUrl}");
				}
			}
		}
		if (page == null)
		{
			page = new Page
			{
				PageName = "About",
				PageSlug = "about",
				Title = "About Sky Art Shop",
				Subtitle = "Creating moments, one craft at a time",
				Content = "<h2>Our Story</h2><p>Sky Art Shop specializes in scrapbooking, journaling, cardmaking, and collaging stationery.</p>",
				ImageGallery = new List<string>(),
				TeamMembers = new List<TeamMember>()
			};
		}
		return View(page);
	}
}
