<?php
/**
 * Theme Manager for Paapi Product Search for Amazon
 *
 * Multi-axis appearance system: base color, accent color, style, radius, dark mode, font.
 * Each axis is independent and stored as a separate DB option.
 */

if (!defined('ABSPATH')) {
    exit;
}

// Include view helpers for psfa_is_premium()
require_once PSFA_PLUGIN_DIR . 'src/core/search/view-helpers.php';

// ── Base Colors (gray scale) ──────────────────────────────────────

/**
 * Get all available base colors
 *
 * @return array Array of base color definitions
 */
function psfa_get_base_colors() {
    return array(
        'neutral' => array('name' => 'Neutral', 'premium' => false),
        'stone'   => array('name' => 'Stone', 'premium' => true),
        'slate'   => array('name' => 'Slate', 'premium' => true),
        'zinc'    => array('name' => 'Zinc', 'premium' => true),
        'gray'    => array('name' => 'Gray', 'premium' => true),
    );
}

/**
 * Get current selected base color (with premium gating)
 *
 * @return string Base color slug
 */
function psfa_get_selected_base_color() {
    $selected = get_option('psfa_base_color', 'neutral');
    $colors = psfa_get_base_colors();

    if (!isset($colors[$selected])) {
        return 'neutral';
    }

    

    return $selected;
}

// ── Accent Colors (primary) ──────────────────────────────────────

/**
 * Get all available accent colors
 *
 * @return array Array of accent color definitions with hex values
 */
function psfa_get_accent_colors() {
    return array(
        'red'     => array('name' => 'Red', 'hex' => '#ef4444', 'premium' => true),
        'orange'  => array('name' => 'Orange', 'hex' => '#f97316', 'premium' => false),
        'amber'   => array('name' => 'Amber', 'hex' => '#f59e0b', 'premium' => true),
        'yellow'  => array('name' => 'Yellow', 'hex' => '#eab308', 'premium' => true),
        'lime'    => array('name' => 'Lime', 'hex' => '#84cc16', 'premium' => true),
        'green'   => array('name' => 'Green', 'hex' => '#22c55e', 'premium' => true),
        'emerald' => array('name' => 'Emerald', 'hex' => '#10b981', 'premium' => true),
        'teal'    => array('name' => 'Teal', 'hex' => '#14b8a6', 'premium' => true),
        'cyan'    => array('name' => 'Cyan', 'hex' => '#06b6d4', 'premium' => true),
        'sky'     => array('name' => 'Sky', 'hex' => '#0ea5e9', 'premium' => true),
        'blue'    => array('name' => 'Blue', 'hex' => '#3b82f6', 'premium' => true),
        'indigo'  => array('name' => 'Indigo', 'hex' => '#6366f1', 'premium' => true),
        'violet'  => array('name' => 'Violet', 'hex' => '#8b5cf6', 'premium' => true),
        'purple'  => array('name' => 'Purple', 'hex' => '#a855f7', 'premium' => true),
        'fuchsia' => array('name' => 'Fuchsia', 'hex' => '#d946ef', 'premium' => true),
        'pink'    => array('name' => 'Pink', 'hex' => '#ec4899', 'premium' => true),
        'rose'    => array('name' => 'Rose', 'hex' => '#f43f5e', 'premium' => true),
    );
}

/**
 * Get current selected accent color (with premium gating)
 *
 * @return string Accent color slug
 */
function psfa_get_selected_accent_color() {
    $selected = get_option('psfa_accent_color', 'orange');
    $colors = psfa_get_accent_colors();

    if (!isset($colors[$selected])) {
        return 'orange';
    }

    

    return $selected;
}

// ── Styles ────────────────────────────────────────────────────────

/**
 * Get all available styles
 *
 * @return array Array of style definitions
 */
function psfa_get_styles() {
    return array(
        'vega' => array('name' => 'Vega', 'description' => 'Classic shadcn, clean', 'premium' => false),
        'nova' => array('name' => 'Nova', 'description' => 'Compact layouts', 'premium' => true),
        'maia' => array('name' => 'Maia', 'description' => 'Soft & rounded', 'premium' => true),
        'lyra' => array('name' => 'Lyra', 'description' => 'Boxy & sharp', 'premium' => true),
        'mira' => array('name' => 'Mira', 'description' => 'Dense interfaces', 'premium' => true),
    );
}

/**
 * Get current selected style (with premium gating)
 *
 * @return string Style slug
 */
function psfa_get_selected_style() {
    $selected = get_option('psfa_style', 'vega');
    $styles = psfa_get_styles();

    if (!isset($styles[$selected])) {
        return 'vega';
    }

    

    return $selected;
}

// ── Radius ────────────────────────────────────────────────────────

/**
 * Get current radius value (with premium gating)
 *
 * @return string Radius value in rem
 */
function psfa_get_radius() {
    $selected = get_option('psfa_radius', '0.5');
    $valid = array('0', '0.3', '0.5', '0.75', '1.0');

    if (!in_array($selected, $valid, true)) {
        return '0.5';
    }

    

    return $selected;
}

// ── Dark Mode ─────────────────────────────────────────────────────

/**
 * Check if dark mode is enabled (with premium gating)
 *
 * @return bool
 */
function psfa_is_dark_mode() {
    $dark = get_option('psfa_dark_mode', '0') === '1';

    

    return $dark;
}

// ── Fonts ─────────────────────────────────────────────────────────

/**
 * Get all available font options
 *
 * @return array Array of font definitions
 */
function psfa_get_fonts() {
    return array(
        ''             => array('name' => 'System Default', 'family' => '', 'category' => 'system', 'premium' => false),
        'inter'        => array('name' => 'Inter', 'family' => 'Inter', 'category' => 'sans', 'premium' => true),
        'roboto'       => array('name' => 'Roboto', 'family' => 'Roboto', 'category' => 'sans', 'premium' => true),
        'open-sans'    => array('name' => 'Open Sans', 'family' => 'Open Sans', 'category' => 'sans', 'premium' => true),
        'lato'         => array('name' => 'Lato', 'family' => 'Lato', 'category' => 'sans', 'premium' => true),
        'montserrat'   => array('name' => 'Montserrat', 'family' => 'Montserrat', 'category' => 'sans', 'premium' => true),
        'poppins'      => array('name' => 'Poppins', 'family' => 'Poppins', 'category' => 'sans', 'premium' => true),
        'dm-sans'      => array('name' => 'DM Sans', 'family' => 'DM Sans', 'category' => 'sans', 'premium' => true),
        'plus-jakarta' => array('name' => 'Plus Jakarta Sans', 'family' => 'Plus Jakarta Sans', 'category' => 'sans', 'premium' => true),
        'outfit'       => array('name' => 'Outfit', 'family' => 'Outfit', 'category' => 'sans', 'premium' => true),
        'space-grotesk' => array('name' => 'Space Grotesk', 'family' => 'Space Grotesk', 'category' => 'sans', 'premium' => true),
        'rubik'        => array('name' => 'Rubik', 'family' => 'Rubik', 'category' => 'sans', 'premium' => true),
        'source-serif' => array('name' => 'Source Serif 4', 'family' => 'Source Serif 4', 'category' => 'serif', 'premium' => true),
        'merriweather' => array('name' => 'Merriweather', 'family' => 'Merriweather', 'category' => 'serif', 'premium' => true),
        'playfair'     => array('name' => 'Playfair Display', 'family' => 'Playfair Display', 'category' => 'serif', 'premium' => true),
        'lora'         => array('name' => 'Lora', 'family' => 'Lora', 'category' => 'serif', 'premium' => true),
    );
}

/**
 * Get current selected font slug (with premium gating)
 *
 * @return string Font slug (empty string = system default)
 */
function psfa_get_selected_font() {
    $selected = get_option('psfa_font', '');
    $fonts = psfa_get_fonts();

    if (!isset($fonts[$selected])) {
        return '';
    }

    

    return $selected;
}

/**
 * Get the CSS font-family string for a font slug
 *
 * @param string $font_slug Font slug
 * @return string CSS font-family value (empty if system default)
 */
function psfa_get_font_family($font_slug) {
    $fonts = psfa_get_fonts();

    if (!isset($fonts[$font_slug]) || empty($fonts[$font_slug]['family'])) {
        return '';
    }

    $family = $fonts[$font_slug]['family'];
    $category = $fonts[$font_slug]['category'];
    $fallback = $category === 'serif' ? 'serif' : 'sans-serif';

    return "'{$family}', {$fallback}";
}

/**
 * Get Google Fonts CSS URL for a font slug
 *
 * @param string $font_slug Font slug
 * @return string|false Google Fonts URL or false if system default
 */
function psfa_get_google_font_url($font_slug) {
    $fonts = psfa_get_fonts();

    if (empty($font_slug) || !isset($fonts[$font_slug]) || empty($fonts[$font_slug]['family'])) {
        return false;
    }

    $family = urlencode($fonts[$font_slug]['family']);
    return "https://fonts.googleapis.com/css2?family={$family}:wght@400;500;600;700&display=swap";
}

// ── CSS File Paths ────────────────────────────────────────────────

/**
 * Get the URL for a base color CSS file
 *
 * @param string $base_color Base color slug
 * @return string|false URL or false if not found
 */
function psfa_get_base_css_url($base_color) {
    $file = $base_color . '.css';
    $full_path = PSFA_PLUGIN_DIR . 'src/assets/themes/base/' . $file;

    if (file_exists($full_path)) {
        return PSFA_PLUGIN_URL . 'src/assets/themes/base/' . $file;
    }

    return false;
}

/**
 * Get the URL for a dark mode base color CSS file
 *
 * @param string $base_color Base color slug
 * @return string|false URL or false if not found
 */
function psfa_get_base_dark_css_url($base_color) {
    $file = $base_color . '-dark.css';
    $full_path = PSFA_PLUGIN_DIR . 'src/assets/themes/base/' . $file;

    if (file_exists($full_path)) {
        return PSFA_PLUGIN_URL . 'src/assets/themes/base/' . $file;
    }

    return false;
}

/**
 * Get the URL for an accent color CSS file
 *
 * @param string $accent_color Accent color slug
 * @return string|false URL or false if not found
 */
function psfa_get_accent_css_url($accent_color) {
    $file = $accent_color . '.css';
    $full_path = PSFA_PLUGIN_DIR . 'src/assets/themes/accent/' . $file;

    if (file_exists($full_path)) {
        return PSFA_PLUGIN_URL . 'src/assets/themes/accent/' . $file;
    }

    return false;
}

/**
 * Get the URL for a style CSS file
 *
 * @param string $style Style slug
 * @return string|false URL or false if not found
 */
function psfa_get_style_css_url($style) {
    $file = $style . '.css';
    $full_path = PSFA_PLUGIN_DIR . 'src/assets/themes/style/' . $file;

    if (file_exists($full_path)) {
        return PSFA_PLUGIN_URL . 'src/assets/themes/style/' . $file;
    }

    return false;
}

/**
 * Get the URL for the component overrides CSS file
 *
 * @return string|false URL or false if not found
 */
function psfa_get_product_css_url() {
    $full_path = PSFA_PLUGIN_DIR . 'src/assets/themes/overrides.css';

    if (file_exists($full_path)) {
        return PSFA_PLUGIN_URL . 'src/assets/themes/overrides.css';
    }

    return false;
}

/**
 * Get the URL for the dark mode rules CSS file
 *
 * @return string|false URL or false if not found
 */
function psfa_get_dark_rules_css_url() {
    $full_path = PSFA_PLUGIN_DIR . 'src/assets/themes/dark-rules.css';

    if (file_exists($full_path)) {
        return PSFA_PLUGIN_URL . 'src/assets/themes/dark-rules.css';
    }

    return false;
}

// ── Migration ─────────────────────────────────────────────────────

/**
 * Migrate from old monolithic theme system to multi-axis appearance.
 *
 * Maps old psfa_selected_theme to the new axis options. Runs once,
 * guarded by psfa_appearance_migrated flag.
 *
 * @return void
 */
function psfa_migrate_to_multi_axis() {
    $migrated = get_option('psfa_appearance_migrated', false);
    if ($migrated) {
        return;
    }

    $old_theme = get_option('psfa_selected_theme', '');

    if (!empty($old_theme)) {
        $map = array(
            'stone'           => array('base' => 'neutral', 'accent' => 'orange', 'dark' => false),
            'slate'           => array('base' => 'slate', 'accent' => 'blue', 'dark' => false),
            'zinc'            => array('base' => 'zinc', 'accent' => 'indigo', 'dark' => false),
            'neutral'         => array('base' => 'neutral', 'accent' => 'orange', 'dark' => false),
            'dark'            => array('base' => 'neutral', 'accent' => 'orange', 'dark' => true),
            // Legacy slugs from pre-shadcn migration
            'amazon-inspired' => array('base' => 'neutral', 'accent' => 'orange', 'dark' => false),
            'ocean-blue'      => array('base' => 'zinc', 'accent' => 'blue', 'dark' => false),
            'modern'          => array('base' => 'neutral', 'accent' => 'orange', 'dark' => false),
            'minimal'         => array('base' => 'neutral', 'accent' => 'orange', 'dark' => false),
        );

        if (isset($map[$old_theme])) {
            $m = $map[$old_theme];
            update_option('psfa_base_color', $m['base']);
            update_option('psfa_accent_color', $m['accent']);
            if ($m['dark']) {
                update_option('psfa_dark_mode', '1');
            }
        }

        delete_option('psfa_selected_theme');
    }

    // Clean up old customization options
    delete_option('psfa_theme_colors');
    delete_option('psfa_button_border_radius');
    delete_option('psfa_button_font_size');
    delete_option('psfa_button_font_weight');
    delete_option('psfa_title_font');
    delete_option('psfa_description_font');
    delete_option('psfa_price_font');
    delete_option('psfa_theme_migration_version');

    update_option('psfa_appearance_migrated', true);
}
add_action('init', 'psfa_migrate_to_multi_axis');
