<?php
/**
 * WAHRB Color Settings
 * Handles global and vendor-specific color customization for booking modal
 *
 * @package WahraRentalsBookings
 */

defined('ABSPATH') || exit;

/**
 * Color Settings class
 */
class WAHRB_Color_Settings {

    /**
     * Default colors
     */
    private static $defaults = [
        'header_gradient_start' => '#1e40af',
        'header_gradient_end'   => '#3b82f6',
        'button_primary'        => '#2563eb',
        'button_primary_hover'  => '#1d4ed8',
        'button_secondary'      => '#f1f5f9',
        'accent_color'          => '#10b981',
    ];

    /**
     * Get color value with fallback hierarchy:
     * 1. Product-level (if set)
     * 2. Vendor-level (if set)
     * 3. Global admin setting
     * 4. Default
     *
     * @param string $key Color key
     * @param int $product_id Optional product ID
     * @param int $vendor_id Optional vendor ID
     * @return string HEX color value
     */
    public static function get_color($key, $product_id = 0, $vendor_id = 0) {
        // Try product level
        if ($product_id > 0) {
            $product_color = get_post_meta($product_id, '_wahrb_color_' . $key, true);
            if (!empty($product_color)) {
                return $product_color;
            }
            
            // Get vendor from product if not provided
            if ($vendor_id <= 0) {
                $vendor_id = get_post_field('post_author', $product_id);
            }
        }
        
        // Try vendor level
        if ($vendor_id > 0) {
            $vendor_color = get_user_meta($vendor_id, '_wahrb_color_' . $key, true);
            if (!empty($vendor_color)) {
                return $vendor_color;
            }
        }
        
        // Try global setting
        $global_color = get_option('wahrb_color_' . $key, '');
        if (!empty($global_color)) {
            return $global_color;
        }
        
        // Return default
        return self::$defaults[$key] ?? '#2563eb';
    }

    /**
     * Get all colors for a product/vendor context
     *
     * @param int $product_id Product ID
     * @param int $vendor_id Vendor ID
     * @return array All color values
     */
    public static function get_all_colors($product_id = 0, $vendor_id = 0) {
        $colors = [];
        foreach (array_keys(self::$defaults) as $key) {
            $colors[$key] = self::get_color($key, $product_id, $vendor_id);
        }
        return $colors;
    }

    /**
     * Generate CSS variables from colors
     *
     * @param array $colors Color array
     * @return string CSS custom properties
     */
    public static function generate_css_vars($colors) {
        $css = ':root {' . "\n";
        $css .= '  --wahrb-header-gradient-start: ' . sanitize_hex_color($colors['header_gradient_start']) . ';' . "\n";
        $css .= '  --wahrb-header-gradient-end: ' . sanitize_hex_color($colors['header_gradient_end']) . ';' . "\n";
        $css .= '  --wahrb-button-primary: ' . sanitize_hex_color($colors['button_primary']) . ';' . "\n";
        $css .= '  --wahrb-button-primary-hover: ' . sanitize_hex_color($colors['button_primary_hover']) . ';' . "\n";
        $css .= '  --wahrb-button-secondary: ' . sanitize_hex_color($colors['button_secondary']) . ';' . "\n";
        $css .= '  --wahrb-accent-color: ' . sanitize_hex_color($colors['accent_color']) . ';' . "\n";
        $css .= '}' . "\n";
        return $css;
    }

    /**
     * Output inline CSS with color variables
     *
     * @param int $product_id Product ID
     */
    public static function output_color_css($product_id = 0) {
        global $product;
        
        if (!$product_id && $product) {
            $product_id = $product->get_id();
        }
        
        $vendor_id = $product_id ? get_post_field('post_author', $product_id) : 0;
        $colors = self::get_all_colors($product_id, $vendor_id);
        
        wp_register_style('wahrb-dynamic-colors', false);
        wp_enqueue_style('wahrb-dynamic-colors');
        wp_add_inline_style('wahrb-dynamic-colors', wp_strip_all_tags(self::generate_css_vars($colors)));
    }

    /**
     * Save vendor colors
     *
     * @param int $vendor_id Vendor ID
     * @param array $colors Color values
     */
    public static function save_vendor_colors($vendor_id, $colors) {
        foreach (self::$defaults as $key => $default) {
            if (isset($colors[$key]) && !empty($colors[$key])) {
                update_user_meta($vendor_id, '_wahrb_color_' . $key, sanitize_hex_color($colors[$key]));
            } else {
                delete_user_meta($vendor_id, '_wahrb_color_' . $key);
            }
        }
    }

    /**
     * Save global colors (admin)
     *
     * @param array $colors Color values
     */
    public static function save_global_colors($colors) {
        foreach (self::$defaults as $key => $default) {
            if (isset($colors[$key]) && !empty($colors[$key])) {
                update_option('wahrb_color_' . $key, sanitize_hex_color($colors[$key]));
            } else {
                delete_option('wahrb_color_' . $key);
            }
        }
    }

    /**
     * Get default colors
     *
     * @return array Default colors
     */
    public static function get_defaults() {
        return self::$defaults;
    }

    /**
     * Get color labels for UI
     *
     * @return array Labels
     */
    public static function get_color_labels() {
        return [
            'header_gradient_start' => __('Header (gradient start)', 'wahra-rentals-bookings'),
            'header_gradient_end'   => __('Header (gradient end)', 'wahra-rentals-bookings'),
            'button_primary'        => __('Primary button', 'wahra-rentals-bookings'),
            'button_primary_hover'  => __('Primary button (hover)', 'wahra-rentals-bookings'),
            'button_secondary'      => __('Secondary button', 'wahra-rentals-bookings'),
            'accent_color'          => __('Accent color (success)', 'wahra-rentals-bookings'),
        ];
    }
}
