<?php
namespace Core\System;

use Core\App\Models\ThemeSettings;
use PDO;

class AdminAuthTheme
{
    public static function get(): array
    {
        /** =========================
         * Theme settings (JSON)
         * ========================= */
        $themeSettings = new ThemeSettings();
        $theme = $themeSettings->getAll();

        $adminTheme = $theme['admin_theme'] ?? [];
        $fonts      = $adminTheme['fonts']  ?? [];
        $colors     = $adminTheme['colors'] ?? [];
        $mode       = $adminTheme['mode']   ?? 'light';

        /** =========================
         * Fonts map
         * ========================= */
        $fontMap = [
            '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 = $fonts['body'] ?? null;

        /** =========================
         * Logos (FROM settings table)
         * ========================= */
        $db = \db();

        $stmt = $db->prepare("
            SELECT setting_key, setting_value
            FROM settings
            WHERE setting_key IN ('admin_logo', 'site_logo')
        ");
        $stmt->execute();

        $logos = [];
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $logos[$row['setting_key']] = $row['setting_value'];
        }

        // admin_logo → site_logo → empty
        $logoPath = $logos['admin_logo'] ?: ($logos['site_logo'] ?? '');

        // Base URL (project in subfolder)
        $baseUrl = '/';

        $logo = $logoPath ? $baseUrl . $logoPath : '';

        /** =========================
         * Final theme array
         * ========================= */
        return [
            'lang' => Language::code(),
            'dir'  => Language::direction(),

            'logo' => $logo,

            'font_family' => ($bodyKey && isset($fontMap[$bodyKey]))
                ? $fontMap[$bodyKey]['family']
                : null,

            'font_css' => ($bodyKey && isset($fontMap[$bodyKey]))
                ? $fontMap[$bodyKey]['import']
                : null,

            'primary'    => $colors['primary']    ?? null,
            'background' => $colors['background'] ?? null,
            'mode'       => $mode,
        ];
    }
}
