<?php
class SeekMake_Security {

    public static function verify_nonce($nonce_name = 'seekmake_nonce', $action = 'seekmake_action') {
        $nonce = isset($_POST[$nonce_name]) ? sanitize_text_field($_POST[$nonce_name]) : '';
        
        if (!wp_verify_nonce($nonce, $action)) {
            return false;
        }
        
        return true;
    }

    public static function check_capability($capability = 'manage_options') {
        return current_user_can($capability);
    }

    public static function sanitize_manufacturer_id($manufacturer_id) {
        return preg_replace('/[^a-zA-Z0-9]/', '', $manufacturer_id);
    }

    public static function validate_manufacturer_id($manufacturer_id, $log_error = true) {
        if (empty($manufacturer_id)) {
            return false;
        }

        $sanitized_id = self::sanitize_manufacturer_id($manufacturer_id);
        
        if (empty($sanitized_id)) {
            return false;
        }

        if (strlen($sanitized_id) < 3) {
            return false;
        }

        return true;
    }

    public static function require_manufacturer_id($manufacturer_id, $context = 'operation') {
        if (!self::validate_manufacturer_id($manufacturer_id, true)) {
            if (wp_doing_ajax()) {
                wp_send_json_error(array(
                    'message' => 'Manufacturer ID is required but not configured. Please authenticate your SeekMake account first.',
                    'code' => 'MISSING_MANUFACTURER_ID'
                ), 400);
            }
            
            wp_die(
                esc_html__('SeekMake Error: Manufacturer ID is required but not configured. Please authenticate your account first.', 'seekmake'),
                esc_html__('Configuration Error', 'seekmake'),
                array('response' => 400)
            );
        }
    }

    public static function sanitize_seekmake_url($url) {
        $url = esc_url_raw($url);
        
        if (strpos($url, 'https://') !== 0) {
            return false;
        }
        
        $parsed_url = parse_url($url);
        $allowed_domains = array('seekmake.com', 'dev.seekmake.com');
        
        if (!isset($parsed_url['host']) || !in_array($parsed_url['host'], $allowed_domains)) {
            return false;
        }
        
        return $url;
    }

    public static function sanitize_event_data($data) {
        if (!is_array($data)) {
            return array();
        }

        $sanitized = array();

        foreach ($data as $key => $value) {
            $key = sanitize_key($key);
            
            if (is_array($value)) {
                $sanitized[$key] = self::sanitize_event_data($value);
            } elseif (is_string($value)) {
                $sanitized[$key] = sanitize_text_field($value);
            } elseif (is_numeric($value)) {
                $sanitized[$key] = is_float($value) ? floatval($value) : intval($value);
            } elseif (is_bool($value)) {
                $sanitized[$key] = (bool) $value;
            }
        }

        return $sanitized;
    }

    public static function validate_event_origin($origin) {
        $allowed_origins = array(
            'https://seekmake.com',
            'https://dev.seekmake.com',
            'https://www.seekmake.com'
        );

        return in_array($origin, $allowed_origins);
    }

    public static function sanitize_button_text($text) {
        return sanitize_text_field($text);
    }

    public static function sanitize_button_style($style) {
        $allowed_styles = array('primary', 'secondary', 'link', 'outline');
        return in_array($style, $allowed_styles) ? $style : 'primary';
    }

    public static function sanitize_display_mode($mode) {
        $allowed_modes = array('modal', 'inline', 'button');
        return in_array($mode, $allowed_modes) ? $mode : 'modal';
    }

    public static function sanitize_language($lang) {
        return preg_replace('/[^a-z]/', '', strtolower(substr($lang, 0, 2)));
    }

    public static function create_nonce($action = 'seekmake_action') {
        return wp_create_nonce($action);
    }

    public static function validate_json($json) {
        if (!is_string($json)) {
            return false;
        }

        $data = json_decode($json, true);
        
        if (json_last_error() !== JSON_ERROR_NONE) {
            return false;
        }

        return $data;
    }
}
