<?php

namespace App\Controllers;

use PDO;

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

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

        $conferenceId =
            TenantContext::conferenceId();

        if ($conferenceId <= 0) {

            $conferenceId =
                (int)($_SESSION['conference_id'] ?? 0);
        }

        error_log(
            'ADMIN THEMES CONF ID: '
            . $conferenceId
        );

        $themes = [];

        try {

            $stmt = $db->prepare("
                SELECT *
                FROM tracks
                WHERE conference_id = ?
                ORDER BY
                    is_active DESC,
                    sort_order ASC,
                    id ASC
            ");

            $stmt->execute([
                $conferenceId
            ]);

            $themes =
                $stmt->fetchAll(PDO::FETCH_ASSOC);

            error_log(
                'TRACKS FOUND: '
                . count($themes)
            );

        } catch (\Throwable $e) {

            error_log(
                'TRACKS LOAD ERROR: '
                . $e->getMessage()
            );

            $themes = [];
        }

        $this->render(
            'admin/conference_themes/index',
            [
                'themes' => $themes,
                'conferenceId' => $conferenceId
            ]
        );
    }
}
