<?php
use Core\App\Models\ThemeSettings;

$lpThemeImports = [];
$lpBodyFont = "system-ui,-apple-system,'Segoe UI',Tahoma,sans-serif";
$lpHeadingFont = $lpBodyFont;
$lpThemeVars = [
    'primary' => '#f97316',
    'secondary' => '#2563eb',
    'accent' => '#0ea5e9',
    'bg' => '#f4f7fb',
    'panel' => '#ffffff',
    'panel_soft' => '#f8fafc',
    'text' => '#0f172a',
    'muted' => '#64748b',
    'border' => '#dde6f1',
    'success' => '#16a34a',
    'danger' => '#dc2626',
    'warning' => '#d97706',
];

if (class_exists(ThemeSettings::class)) {
    try {
        $lpSettings = (new ThemeSettings())->getAll();
        $lpAdminTheme = $lpSettings['admin_theme'] ?? [];
        $lpAdminFonts = $lpAdminTheme['fonts'] ?? [];
        $lpAdminColors = $lpAdminTheme['colors'] ?? [];
        $lpFontMap = [
            'ar_cairo' => ['family' => "'Cairo', sans-serif", 'import' => '/public/assets/fonts/cairo/cairo.css'],
            'ar_tajawal' => ['family' => "'Tajawal', sans-serif", 'import' => '/public/assets/fonts/tajawal/tajawal.css'],
            'ar_noto_kufi' => ['family' => "'Noto Kufi Arabic', sans-serif", 'import' => '/public/assets/fonts/noto-kufi-arabic/noto-kufi-arabic.css'],
            'en_poppins' => ['family' => "'Poppins', sans-serif", 'import' => '/public/assets/fonts/poppins/poppins.css'],
            'en_roboto' => ['family' => "'Roboto', sans-serif", 'import' => '/public/assets/fonts/roboto/roboto.css'],
        ];

        $bodyKey = $lpAdminFonts['body'] ?? null;
        $headingKey = $lpAdminFonts['heading'] ?? null;
        if ($bodyKey && isset($lpFontMap[$bodyKey])) {
            $lpThemeImports[$bodyKey] = $lpFontMap[$bodyKey]['import'];
            $lpBodyFont = $lpFontMap[$bodyKey]['family'];
        }
        if ($headingKey && isset($lpFontMap[$headingKey])) {
            $lpThemeImports[$headingKey] = $lpFontMap[$headingKey]['import'];
            $lpHeadingFont = $lpFontMap[$headingKey]['family'];
        }

        $lpThemeVars['primary'] = (string)($lpAdminColors['primary'] ?? $lpThemeVars['primary']);
        $lpThemeVars['secondary'] = (string)($lpAdminColors['secondary'] ?? $lpThemeVars['secondary']);
        $lpThemeVars['accent'] = (string)($lpAdminColors['info'] ?? $lpThemeVars['accent']);
        $lpThemeVars['bg'] = (string)($lpAdminColors['body_bg'] ?? $lpThemeVars['bg']);
        $lpThemeVars['text'] = (string)($lpAdminColors['body_text'] ?? $lpThemeVars['text']);
        $lpThemeVars['border'] = (string)($lpAdminColors['border'] ?? $lpThemeVars['border']);
        $lpThemeVars['success'] = (string)($lpAdminColors['success'] ?? $lpThemeVars['success']);
        $lpThemeVars['danger'] = (string)($lpAdminColors['danger'] ?? $lpThemeVars['danger']);
        $lpThemeVars['warning'] = (string)($lpAdminColors['warning'] ?? $lpThemeVars['warning']);
    } catch (\Throwable $e) {
    }
}
