<?php
require_once __DIR__ . '/../models/CmsSlide.php';
require_once __DIR__ . '/../models/CmsService.php';
require_once __DIR__ . '/../models/Blog.php';
require_once __DIR__ . '/../models/Gallery.php';
require_once __DIR__ . '/../models/Testimonial.php';
require_once __DIR__ . '/../models/Project.php';
class HomeController extends Controller
{
    public function index(): void
    {
        $defaultSlides = [
            ['title' => 'Transforming Structures Into Beautiful Spaces', 'subtitle' => 'Premium Building Finishes, Interior Design, Renovations, and Complete Finishing Solutions Delivered with Quality, Craftsmanship, and Attention to Detail.', 'image' => '/images/slider01.png'],
            ['title' => 'Balorp Ventures transforms ordinary structures into beautiful, functional, and inspiring spaces.', 'subtitle' => 'Whether you\'re building, renovating, or upgrading your property, we deliver exceptional craftsmanship, quality materials, and professional service that bring your vision to life.', 'image' => '/images/slider02.png'],
            ['title' => 'From Bare Walls To Elegant Living', 'subtitle' => 'Transform your space with premium materials and professional finishing expertise.', 'image' => '/images/slider03.png'],
            ['title' => 'Elegant Living Room Finishes', 'subtitle' => 'Beautiful finishes designed for comfort, style, and lasting quality.', 'image' => '/images/slider04.png'],
            ['title' => 'Modern Commercial Spaces Refined', 'subtitle' => 'Commercial fit-outs and finishes that elevate your business environment.', 'image' => '/images/slider05.png'],
            ['title' => 'Complete Renovation & Finishing Solutions', 'subtitle' => 'From planning to polish, we deliver exceptional finishing results.', 'image' => '/images/slider06.png'],
        ];

        $slideModel = new CmsSlide();
        $slideModel->createTableIfNotExists();
        $heroSlides = $slideModel->activeAll();

        if (empty($heroSlides)) {
            $heroSlides = $defaultSlides;
        }

        $heroSlides = array_map(static function (array $slide): array {
            $slide['image'] = $slide['image'] ?? $slide['image_path'] ?? '';
            return $slide;
        }, $heroSlides);

        $serviceModel = new CmsService();
        $serviceModel->createTableIfNotExists();
        $homepageServiceCards = $serviceModel->activeAll();

        if (empty($homepageServiceCards)) {
            $homepageServiceCards = [
                ['title' => 'Building Finishes', 'summary' => 'Painting, screeding, plastering, flooring, tiling, ceilings, and roofing finishes.', 'icon' => 'format_paint', 'id' => 1],
                ['title' => 'Interior Décor', 'summary' => 'Interior design, wallpaper, curtains, lighting design, and wall panels.', 'icon' => 'chair', 'id' => 2],
                ['title' => 'Renovations', 'summary' => 'Residential and commercial renovations, remodeling, and project management.', 'icon' => 'home_repair_service', 'id' => 3],
                ['title' => 'External Works', 'summary' => 'Landscaping design, paving, pathways, and compound beautification.', 'icon' => 'park', 'id' => 4],
            ];
        }

        $blogModel = new Blog();
        $blogModel->createTableIfNotExists();
        $latestPosts = array_slice($blogModel->published(), 0, 3);

        $galleryModel = new Gallery();
        $galleryModel->createTableIfNotExists();
        $galleryItems = $galleryModel->published();

        $projectModel = new Project();
        $featuredProjects = $projectModel->all();

        $this->view('home.index', [
            'title' => 'Balorp Ventures | Home',
            'heroSlides' => $heroSlides,
            'homepageServiceCards' => $homepageServiceCards,
            'latestPosts' => $latestPosts,
            'galleryItems' => $galleryItems,
            'featuredProjects' => $featuredProjects,
        ]);
    }

    public function about(): void
    {
        $this->view('home.about', ['title' => 'About Us | Balorp Ventures']);
    }

    public function services(): void
    {
        $serviceModel = new CmsService();
        $serviceModel->createTableIfNotExists();
        $services = $serviceModel->activeAll();
        $this->view('home.services', [
            'title' => 'Our Services | Balorp Ventures',
            'services' => $services,
        ]);
    }

    public function serviceDetail(): void
    {
        $id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
        $serviceModel = new CmsService();
        $serviceModel->createTableIfNotExists();
        $service = $serviceModel->find($id) ?: null;
        if ($service) {
            $this->view('home.service-detail', [
                'title' => $service['title'] . ' | Balorp Ventures',
                'service' => $service,
            ]);
        } else {
            $this->view('home.service-not-found', [
                'title' => 'Service Not Found | Balorp Ventures',
            ]);
        }
    }

    public function portfolio(): void
    {
        $this->view('home.portfolio', ['title' => 'Projects | Balorp Ventures']);
    }

    public function gallery(): void
    {
        $galleryModel = new Gallery();
        $galleryModel->createTableIfNotExists();
        $items = $galleryModel->published();
        $categories = $galleryModel->categories();
        $this->view('home.gallery', [
            'title' => 'Gallery | Balorp Ventures',
            'items' => $items,
            'categories' => $categories,
        ]);
    }

    public function blog(): void
    {
        $blogModel = new Blog();
        $blogModel->createTableIfNotExists();
        $posts = $blogModel->published();
        $this->view('home.blog', [
            'title' => 'Blog | Balorp Ventures',
            'posts' => $posts,
        ]);
    }

    public function projectLivingRoom(): void
    {
        $this->view('home.project-living-room', ['title' => 'Living Room Finish | Balorp Ventures']);
    }

    public function projectKitchen(): void
    {
        $this->view('home.project-kitchen', ['title' => 'Kitchen Renovation | Balorp Ventures']);
    }

    public function projectOffice(): void
    {
        $this->view('home.project-office', ['title' => 'Office Space Design | Balorp Ventures']);
    }

    public function projectLuxuryDetail(): void
    {
        $this->view('home.project-luxury-detail', ['title' => 'Luxury Interior Detail | Balorp Ventures']);
    }

    public function projectModernFinish(): void
    {
        $this->view('home.project-modern-finish', ['title' => 'Modern Finish Sample | Balorp Ventures']);
    }

    public function projectRefinedStyling(): void
    {
        $this->view('home.project-refined-styling', ['title' => 'Refined Space Styling | Balorp Ventures']);
    }

    public function testimonials(): void
    {
        $testimonialModel = new Testimonial();
        $testimonialModel->createTableIfNotExists();
        $testimonials = $testimonialModel->published();
        $this->view('home.testimonials', [
            'title' => 'Testimonials | Balorp Ventures',
            'testimonials' => $testimonials,
        ]);
    }

    public function estimate(): void
    {
        $this->view('home.estimate', ['title' => 'Get Estimate | Balorp Ventures']);
    }

    public function contact(): void
    {
        $this->view('home.contact', ['title' => 'Contact Us | Balorp Ventures']);
    }
}
