<?php

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

/* ==========================================================================
| ENTERPRISE CONTENT PIPELINE
|========================================================================== */

if (!function_exists('ec_load_tracks')) {

    function ec_load_tracks(): array
    {
        try {

            $db = Database::getInstance();

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

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

            return $stmt->fetchAll(PDO::FETCH_ASSOC);

        } catch (\Throwable $e) {

            error_log(
                'ec_load_tracks error: ' .
                $e->getMessage()
            );

            return [];
        }
    }
}

if (!function_exists('ec_load_speakers')) {

    function ec_load_speakers(): array
    {
        try {

            $db = Database::getInstance();

            $stmt = $db->prepare("
                SELECT *
                FROM conference_speakers
                WHERE conference_id = ?
                ORDER BY id DESC
            ");

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

            return $stmt->fetchAll(PDO::FETCH_ASSOC);

        } catch (\Throwable $e) {

            error_log(
                'ec_load_speakers error: ' .
                $e->getMessage()
            );

            return [];
        }
    }
}

if (!function_exists('ec_safe_array')) {

    function ec_safe_array($value): array
    {
        return is_array($value)
            ? $value
            : [];
    }
}

if (!function_exists('ec_safe_string')) {

    function ec_safe_string($value): string
    {
        return is_string($value)
            ? trim($value)
            : '';
    }
}
