<?php
declare(strict_types=1);

namespace App\Controllers;

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

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

        $id = (int)($_GET['id'] ?? 0);

        if ($id <= 0 && function_exists('default_conference_id')) {
            $id = (int)\default_conference_id();
        }

        if ($id <= 0) {
            http_response_code(404);
            exit('Conference not found');
        }

        $st = $db->prepare("
            SELECT c.*, o.name AS org_name
            FROM conferences c
            LEFT JOIN organizations o ON o.id = c.org_id
            WHERE c.id = ?
              AND c.is_active = 1
            LIMIT 1
        ");
        $st->execute([$id]);
        $conf = $st->fetch(\PDO::FETCH_ASSOC);

        if (!$conf) {
            http_response_code(404);
            exit('Conference not found');
        }

        $bt = $db->prepare("
            SELECT *
            FROM conference_blocks
            WHERE conference_id = ?
              AND is_active = 1
            ORDER BY sort_order ASC, id DESC
        ");
        $bt->execute([$id]);
        $blocks = $bt->fetchAll(\PDO::FETCH_ASSOC);

        $byType = [
            'hero_slide' => [],
            'panel' => [],
            'topic' => [],
            'section_html' => [],
            'important_date' => [],
        ];

        foreach ($blocks as $b) {
            $t = (string)($b['block_type'] ?? '');
            if (!isset($byType[$t])) {
                $byType[$t] = [];
            }
            $byType[$t][] = $b;
        }

        $kpi = [
            'submissions' => 0,
            'accepted' => 0,
            'under_review' => 0,
            'published' => 0,
            'assignments' => 0,
            'reviewers' => 0,
        ];

        $queries = [
            'submissions' => "SELECT COUNT(*) FROM submissions WHERE conference_id = ?",
            'accepted' => "SELECT COUNT(*) FROM submissions WHERE conference_id = ? AND status='accepted'",
            'under_review' => "SELECT COUNT(*) FROM submissions WHERE conference_id = ? AND status='under_review'",
            'published' => "SELECT COUNT(*) FROM submissions WHERE conference_id = ? AND status='published'",
            'assignments' => "SELECT COUNT(*) FROM review_assignments WHERE conference_id = ?",
            'reviewers' => "SELECT COUNT(DISTINCT reviewer_id) FROM review_assignments WHERE conference_id = ?",
        ];

        foreach ($queries as $key => $sql) {
            try {
                $stmt = $db->prepare($sql);
                $stmt->execute([$id]);
                $kpi[$key] = (int)$stmt->fetchColumn();
            } catch (\Throwable $e) {
                $kpi[$key] = 0;
            }
        }

        $workflow = [];
        try {
            $stmt = $db->prepare("
                SELECT cw.stage, cw.is_enabled, ws.title_en, ws.title_ar, ws.sort_order
                FROM conference_workflow cw
                LEFT JOIN workflow_stages ws ON ws.stage_key = cw.stage
                WHERE cw.conference_id = ?
                ORDER BY COALESCE(ws.sort_order, 999), cw.id
            ");
            $stmt->execute([$id]);
            $workflow = $stmt->fetchAll(\PDO::FETCH_ASSOC);
        } catch (\Throwable $e) {
            $workflow = [];
        }

        $committees = [];
        try {
            $stmt = $db->prepare("
                SELECT *
                FROM conference_committees
                WHERE conference_id = ?
                  AND is_active = 1
                ORDER BY sort_order ASC, id DESC
                LIMIT 12
            ");
            $stmt->execute([$id]);
            $committees = $stmt->fetchAll(\PDO::FETCH_ASSOC);
        } catch (\Throwable $e) {
            $committees = [];
        }

        $sessions = [];
        try {
            $stmt = $db->prepare("
                SELECT *
                FROM conference_sessions
                WHERE conference_id = ?
                  AND is_active = 1
                ORDER BY session_date ASC, start_time ASC, sort_order ASC
                LIMIT 10
            ");
            $stmt->execute([$id]);
            $sessions = $stmt->fetchAll(\PDO::FETCH_ASSOC);
        } catch (\Throwable $e) {
            $sessions = [];
        }

        require BASE_PATH . '/app/views/public/conference/home.php';
    }
}
