<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;

/**
 * Page Controller
 *
 * Handles static CMS pages including the About page
 * and slug-based page display.
 */
class PageController extends Controller
{
    /**
     * Display the About page with site settings.
     *
     * @return void
     */
    public function about(): void
    {
        $settingModel = $this->model('Setting');
        $settings = $settingModel->getAll();

        // Try to load a CMS page with 'about' slug, fall back to settings
        $pageModel = $this->model('Page');
        $aboutPage = $pageModel->getBySlug('about');

        $this->view('pages/about', [
            'title'      => 'About Us - Gmind Institute',
            'settings'   => $settings,
            'page'       => $aboutPage,
            'currentNav' => 'about',
        ]);
    }

    /**
     * Display a CMS page by its slug.
     *
     * @param string $slug The URL-friendly slug of the page
     * @return void
     */
    public function show(string $slug): void
    {
        $pageModel = $this->model('Page');

        $page = $pageModel->getBySlug($slug);

        if ($page === null) {
            http_response_code(404);
            $settingModel = $this->model('Setting');
            $settings = $settingModel->getAll();

            $this->view('pages/about', [
                'title'      => 'Page Not Found - Gmind Institute',
                'settings'   => $settings,
                'page'       => null,
                'currentNav' => '',
            ]);
            return;
        }

        $this->view('pages/show', [
            'title'      => $page['title'] . ' - Gmind Institute',
            'page'       => $page,
            'currentNav' => '',
        ]);
    }
}
