<?php

declare(strict_types=1);

namespace App\Controllers\Public;

use App\Core\Controller;
use App\Core\Database;

class HomeController extends Controller
{
    public function index(): void
    {
        $event = Database::fetch(
            "
            SELECT *
            FROM events
            WHERE status = 'published'
            ORDER BY start_date ASC
            LIMIT 1
            "
        );

        $partners = [];
        $programme = [];

        if ($event) {
            $partners = Database::fetchAll(
                "
                SELECT *
                FROM partners
                WHERE event_id = :event_id
                  AND is_active = 1
                ORDER BY display_order ASC, name ASC
                ",
                [
                    'event_id' => $event['id'],
                ]
            );

            $programme = Database::fetchAll(
                "
                SELECT *
                FROM programme_items
                WHERE event_id = :event_id
                  AND is_active = 1
                ORDER BY
                    programme_date ASC,
                    start_time ASC,
                    display_order ASC
                LIMIT 8
                ",
                [
                    'event_id' => $event['id'],
                ]
            );
        }

        $this->view('public/home', [
            'pageTitle' => $event['short_name']
                ?? 'Traditional Authorities Conference',
            'event' => $event,
            'partners' => $partners,
            'programme' => $programme,
        ]);
    }
}