<?php
declare(strict_types=1);

namespace App\Controllers;

require_once BASE_PATH . '/app/helpers/permissions.php';

require_once BASE_PATH . '/app/helpers/conference_scope.php';

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

final class PublicLandingController extends Controller
{
    public function index(): void
    {
        $db = Database::getInstance();

        $defaultId = function_exists('default_conference_id') ? default_conference_id() : 0;

        $featured = null;
        if ($defaultId > 0) {
            $st = $db->prepare("SELECT * FROM conferences WHERE id=:id LIMIT 1");
            $st->execute([':id'=>$defaultId]);
            $featured = $st->fetch() ?: null;
        }

        $confs = [];
        try {
            $confs = $db->query("SELECT * FROM conferences WHERE (is_active=1 OR is_active IS NULL) ORDER BY start_date DESC, id DESC")->fetchAll();
        } catch (\Throwable $e) {}

        $stats = ['submissions'=>0,'reviewers'=>0,'accepted'=>0];
        try { $stats['submissions'] = (int)($db->query("SELECT COUNT(*) FROM submissions")->fetchColumn() ?: 0); } catch (\Throwable $e) {}
        try { $stats['reviewers'] = (int)($db->query("SELECT COUNT(*) FROM users WHERE role_id=4")->fetchColumn() ?: 0); } catch (\Throwable $e) {}
        try { $stats['accepted'] = (int)($db->query("SELECT COUNT(*) FROM submissions WHERE status='accepted'")->fetchColumn() ?: 0); } catch (\Throwable $e) {}

        $this->view('public/landing/index', ['featured'=>$featured,'conferences'=>$confs,'stats'=>$stats]);
    }
}
