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

namespace SkyArtShop.Controllers
{
    public class AboutController : Controller
    {
        private readonly MongoDBService _mongoService;
        private readonly string _pagesCollection = "Pages";

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

        public async Task<IActionResult> Index()
        {
            var pages = await _mongoService.GetAllAsync<Page>(_pagesCollection);
            var aboutPage = pages.FirstOrDefault(p => p.PageSlug == "about" && p.IsActive);

            Console.WriteLine($"[ABOUT] Found About page: {aboutPage != null}");
            if (aboutPage != null)
            {
                Console.WriteLine($"[ABOUT] Title: {aboutPage.Title}");
                Console.WriteLine($"[ABOUT] Content length: {aboutPage.Content?.Length ?? 0}");
                Console.WriteLine($"[ABOUT] Image Gallery Count: {aboutPage.ImageGallery?.Count ?? 0}");
                Console.WriteLine($"[ABOUT] Team Members Count: {aboutPage.TeamMembers?.Count ?? 0}");

                if (aboutPage.ImageGallery != null && aboutPage.ImageGallery.Any())
                {
                    Console.WriteLine($"[ABOUT] Gallery Images: {string.Join(", ", aboutPage.ImageGallery)}");
                }

                if (aboutPage.TeamMembers != null && aboutPage.TeamMembers.Any())
                {
                    foreach (var member in aboutPage.TeamMembers)
                    {
                        Console.WriteLine($"[ABOUT] Team: {member.Name} ({member.Role}) - Photo: {member.PhotoUrl}");
                    }
                }
            }

            if (aboutPage == null)
            {
                aboutPage = 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(aboutPage);
        }
    }
}
