<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * VMO Email Provisioner - Auto-create email accounts via hosting control panels
 * 
 * Supports:
 * - cPanel (UAPI)
 * - Plesk (XML API)
 * - DirectAdmin (API)
 * 
 * For unsupported hosts, directs users to Jarvis AI for guidance.
 * 
 * @since 8.2.0
 */

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

class VMO_Email_Provisioner {
    
    const OPTION_CREDENTIALS = 'vmo_email_provisioner_credentials';
    const OPTION_PANEL_TYPE = 'vmo_email_provisioner_panel';
    
    // Known managed WordPress hosts that don't support email hosting
    private static $managed_hosts = [
        'wpengine' => 'WP Engine',
        'kinsta' => 'Kinsta',
        'flywheel' => 'Flywheel',
        'pantheon' => 'Pantheon',
        'cloudways' => 'Cloudways',
        'pressable' => 'Pressable',
        'pagely' => 'Pagely',
        'nexcess' => 'Nexcess',
        'convesio' => 'Convesio'
    ];
    
    public function __construct() {
        add_action('wp_ajax_vmo_detect_hosting_panel', array($this, 'ajax_detect_hosting_panel'));
        add_action('wp_ajax_vmo_save_panel_credentials', array($this, 'ajax_save_panel_credentials'));
        add_action('wp_ajax_vmo_provision_email_account', array($this, 'ajax_provision_email_account'));
        add_action('wp_ajax_vmo_test_panel_connection', array($this, 'ajax_test_panel_connection'));
        add_action('wp_ajax_vmo_get_panel_status', array($this, 'ajax_get_panel_status'));
    }
    
    /**
     * Detect the hosting control panel
     */
    public function ajax_detect_hosting_panel() {
        check_ajax_referer('vmo_builder_nonce', 'security');
        
        $detection = $this->detect_hosting_environment();
        
        wp_send_json_success($detection);
    }
    
    /**
     * Get current panel status and saved credentials (masked)
     */
    public function ajax_get_panel_status() {
        check_ajax_referer('vmo_builder_nonce', 'security');
        
        $panel_type = get_option(self::OPTION_PANEL_TYPE, '');
        $credentials = get_option(self::OPTION_CREDENTIALS, []);

        $has_credentials = !empty($credentials['api_token']) && (
            $panel_type === 'cloudflare' || !empty($credentials['username'])
        );
        
        wp_send_json_success([
            'panel_type' => $panel_type,
            'has_credentials' => $has_credentials,
            'username' => $has_credentials ? $credentials['username'] : '',
            'panel_url' => $has_credentials ? ($credentials['panel_url'] ?? '') : '',
            'last_verified' => $credentials['last_verified'] ?? null
        ]);
    }
    
    /**
     * Detect hosting environment
     */
    private function detect_hosting_environment() {
        $result = [
            'panel_type' => null,
            'panel_url' => null,
            'supports_auto_provision' => false,
            'is_managed_host' => false,
            'managed_host_name' => null,
            'detection_method' => null,
            'domain' => $this->get_site_domain(),
            'route_mode' => 'zoho_manual',
            'route_ready' => false,
            'route_reason' => 'Vultr/Cloudflare/MXroute route not detected. Using Zoho setup.',
            'requires_zoho' => true,
            'route_signals' => []
        ];

        $route_policy = $this->detect_vultr_cloudflare_mxroute_route($result['domain']);
        $result = array_merge($result, $route_policy);

        // Route policy: only users on Vultr + Cloudflare + MXroute path get auto-provisioning.
        if (!empty($result['route_ready'])) {
            $result['panel_type'] = 'cloudflare';
            $result['panel_url'] = 'https://dash.cloudflare.com';
            $result['supports_auto_provision'] = true;
            $result['is_managed_host'] = false;
            $result['managed_host_name'] = null;
            $result['detection_method'] = 'vmo_vultr_cloudflare_mxroute';
            return $result;
        }
        
        // Check for managed WordPress hosts first
        $managed = $this->detect_managed_host();
        if ($managed) {
            $result['is_managed_host'] = true;
            $result['managed_host_name'] = $managed;
            $result['detection_method'] = 'managed_host_signature';
            return $result;
        }
        
        // Keep panel detection for diagnostics, but enforce Zoho route for non-QuickLaunch users.
        $panel = $this->detect_control_panel();
        if ($panel) {
            $result['panel_type'] = $panel['type'];
            $result['panel_url'] = $panel['url'];
            $result['detection_method'] = $panel['method'];
        }
        
        return $result;
    }

    /**
     * Determine if Vultr + Cloudflare + MXroute route is configured and usable.
     */
    private function detect_vultr_cloudflare_mxroute_route($domain) {
        $vultr_key = trim((string) get_option('vmo_vultr_api_key', ''));
        $cf_token = trim((string) get_option('vmo_cloudflare_api_token', ''));
        $mx_server = trim((string) get_option('vmo_mxroute_server', ''));
        $mx_user = trim((string) get_option('vmo_mxroute_username', ''));
        $mx_key = trim((string) get_option('vmo_mxroute_api_key', ''));

        $signals = [
            'vultr_configured' => !empty($vultr_key),
            'cloudflare_configured' => !empty($cf_token),
            'mxroute_configured' => !empty($mx_server) && !empty($mx_user) && !empty($mx_key),
            'dns_check_supported' => class_exists('VMO_DNS_Detector'),
            'cloudflare_dns' => false,
            'dns_host_type' => null
        ];

        if ($signals['dns_check_supported'] && !empty($domain)) {
            $dns = VMO_DNS_Detector::get_instance()->detect_dns_host($domain);
            if (is_array($dns)) {
                $signals['dns_host_type'] = $dns['host_type'] ?? null;
                $signals['cloudflare_dns'] = ($signals['dns_host_type'] === 'cloudflare');
            }
        }

        $keys_ready = $signals['vultr_configured'] && $signals['cloudflare_configured'] && $signals['mxroute_configured'];
        $dns_ready = $signals['cloudflare_dns'] || !$signals['dns_check_supported'];
        $route_ready = $keys_ready && $dns_ready;

        if ($route_ready) {
            return [
                'route_mode' => 'vultr_cloudflare_mxroute',
                'route_ready' => true,
                'route_reason' => 'Vultr/Cloudflare/MXroute route detected. Auto-provisioning is available.',
                'requires_zoho' => false,
                'route_signals' => $signals
            ];
        }

        $reason = 'Vultr/Cloudflare/MXroute route not fully configured. Using Zoho setup.';
        if (!$keys_ready) {
            $reason = 'Missing Vultr, Cloudflare, or MXroute configuration. Using Zoho setup.';
        } elseif (!$dns_ready) {
            $reason = 'Domain is not on Cloudflare DNS yet. Using Zoho setup.';
        }

        return [
            'route_mode' => 'zoho_manual',
            'route_ready' => false,
            'route_reason' => $reason,
            'requires_zoho' => true,
            'route_signals' => $signals
        ];
    }
    
    /**
     * Detect managed WordPress hosts that don't support email
     */
    private function detect_managed_host() {
        // Check server software and environment variables
        $server_software = $_SERVER['SERVER_SOFTWARE'] ?? '';
        $server_name = $_SERVER['SERVER_NAME'] ?? '';
        $document_root = $_SERVER['DOCUMENT_ROOT'] ?? '';
        
        // WP Engine detection
        if (defined('WPE_APIKEY') || 
            strpos($document_root, 'wpe') !== false ||
            getenv('IS_WPE')) {
            return self::$managed_hosts['wpengine'];
        }
        
        // Kinsta detection
        if (defined('KINSTA_CACHE_ZONE') || 
            strpos($document_root, 'kinsta') !== false ||
            getenv('KINSTA_TOOL_SECRET')) {
            return self::$managed_hosts['kinsta'];
        }
        
        // Flywheel detection
        if (defined('FLYWHEEL_CONFIG_DIR') ||
            strpos($document_root, 'flywheel') !== false) {
            return self::$managed_hosts['flywheel'];
        }
        
        // Pantheon detection
        if (defined('PANTHEON_ENVIRONMENT') ||
            getenv('PANTHEON_ENVIRONMENT')) {
            return self::$managed_hosts['pantheon'];
        }
        
        // Cloudflare Detection (DNS Level)
        // If Cloudflare is detected, we can support auto-provisioning via API
        if (class_exists('VMO_DNS_Detector')) {
            $dns = VMO_DNS_Detector::get_instance()->detect_dns_host($this->get_site_domain());
            if (isset($dns['host_type']) && $dns['host_type'] === 'cloudflare') {
                // Cloudflare is technically a DNS host, but we treat it as a provisioner
                // because it has an API. We return null here so it doesn't fail as "Managed Host"
                // but we will catch it in detect_control_panel logic later.
                return null; 
            }
        }
        
        // Cloudways detection
        if (strpos($document_root, 'cloudways') !== false ||
            strpos($server_software, 'cloudways') !== false) {
            return self::$managed_hosts['cloudways'];
        }
        
        // Pressable detection
        if (defined('STARTER_VERSION') && strpos($document_root, 'developer') !== false) {
            return self::$managed_hosts['pressable'];
        }
        
        // Pagely detection
        if (defined('JEOMOMO_STATIC_CDN_URL') ||
            strpos($document_root, 'pagely') !== false) {
            return self::$managed_hosts['pagely'];
        }
        
        // Check for common managed host environment variables
        $env_indicators = [
            'IS_WPE' => 'wpengine',
            'KINSTA_TOOL_SECRET' => 'kinsta',
            'PANTHEON_SITE' => 'pantheon',
        ];
        
        foreach ($env_indicators as $env_var => $host_key) {
            if (getenv($env_var)) {
                return self::$managed_hosts[$host_key];
            }
        }
        
        return null;
    }
    
    /**
     * Detect control panel type
     */
    private function detect_control_panel() {
        $site_url = site_url();
        $parsed = parse_url($site_url);
        $host = $parsed['host'] ?? '';
        $scheme = $parsed['scheme'] ?? 'https';
        
        // VMO: Check for Cloudflare first (API based)
        // If the site is identifying as having Cloudflare DNS, we treat that as our control panel
        if (class_exists('VMO_DNS_Detector')) {
            $dns = VMO_DNS_Detector::get_instance()->detect_dns_host($host);
            if (isset($dns['host_type']) && $dns['host_type'] === 'cloudflare') {
                require_once plugin_dir_path(__FILE__) . 'class-vmo-cloudflare-manager.php';
                return [
                    'type' => 'cloudflare',
                    'url' => 'https://dash.cloudflare.com',
                    'method' => 'dns_api_check'
                ];
            }
        }

        // Get server IP for direct panel access
        $server_ip = $_SERVER['SERVER_ADDR'] ?? gethostbyname($host);
        
        // Common panel ports and paths
        $panels = [
            'cpanel' => [
                'ports' => [2083, 2082],
                'paths' => ['/cpanel', '/.cpanel'],
                'api_path' => ':2083/execute/'
            ],
            'plesk' => [
                'ports' => [8443, 8880],
                'paths' => ['/plesk', '/smb'],
                'api_path' => ':8443/api/v2/'
            ],
            'directadmin' => [
                'ports' => [2222],
                'paths' => ['/CMD_LOGIN'],
                'api_path' => ':2222/CMD_API_'
            ]
        ];
        
        // Method 1: Check for panel files in common locations
        $panel_indicators = [
            'cpanel' => ['/usr/local/cpanel', '/var/cpanel'],
            'plesk' => ['/usr/local/psa', '/opt/psa'],
            'directadmin' => ['/usr/local/directadmin']
        ];
        
        foreach ($panel_indicators as $panel_type => $paths) {
            foreach ($paths as $path) {
                if (@is_dir($path)) {
                    return [
                        'type' => $panel_type,
                        'url' => $this->build_panel_url($host, $panels[$panel_type]['ports'][0]),
                        'method' => 'filesystem'
                    ];
                }
            }
        }
        
        // Method 2: Check for environment variables
        if (getenv('CPANEL') || defined('CPANEL')) {
            return [
                'type' => 'cpanel',
                'url' => $this->build_panel_url($host, 2083),
                'method' => 'environment'
            ];
        }
        
        // Method 3: Try to connect to panel ports (with short timeout)
        foreach ($panels as $panel_type => $config) {
            foreach ($config['ports'] as $port) {
                $test_url = "{$scheme}://{$host}:{$port}/";
                
                $response = wp_remote_head($test_url, [
                    'timeout' => 3,
                    'sslverify' => false,
                    'redirection' => 0
                ]);
                
                if (!is_wp_error($response)) {
                    $code = wp_remote_retrieve_response_code($response);
                    if ($code >= 200 && $code < 500) {
                        return [
                            'type' => $panel_type,
                            'url' => $test_url,
                            'method' => 'port_probe'
                        ];
                    }
                }
            }
        }
        
        // Method 4: Check server headers for panel signatures
        $headers = function_exists('apache_response_headers') ? apache_response_headers() : [];
        $server_signature = $_SERVER['SERVER_SIGNATURE'] ?? '';
        
        if (stripos($server_signature, 'cpanel') !== false || 
            stripos($server_signature, 'whm') !== false) {
            return [
                'type' => 'cpanel',
                'url' => $this->build_panel_url($host, 2083),
                'method' => 'server_signature'
            ];
        }
        
        if (stripos($server_signature, 'plesk') !== false) {
            return [
                'type' => 'plesk',
                'url' => $this->build_panel_url($host, 8443),
                'method' => 'server_signature'
            ];
        }
        
        return null;
    }
    
    /**
     * Build panel URL
     */
    private function build_panel_url($host, $port) {
        return "https://{$host}:{$port}/";
    }
    
    /**
     * Get site domain
     */
    private function get_site_domain() {
        $site_url = site_url();
        $parsed = parse_url($site_url);
        return $parsed['host'] ?? '';
    }
    
    /**
     * Save panel credentials
     */
    public function ajax_save_panel_credentials() {
        check_ajax_referer('vmo_builder_nonce', 'security');
        
        $panel_type = sanitize_text_field($_POST['panel_type'] ?? '');
        $username = sanitize_text_field($_POST['username'] ?? '');
        $api_token = sanitize_text_field($_POST['api_token'] ?? '');
        $panel_url = esc_url_raw($_POST['panel_url'] ?? '');

        $requires_username = ($panel_type !== 'cloudflare');

        if (empty($panel_type) || empty($api_token) || ($requires_username && empty($username))) {
            wp_send_json_error(['message' => 'Missing required credentials']);
            return;
        }

        if ($panel_type === 'cloudflare') {
            if (empty($username)) {
                $username = 'cloudflare';
            }
            if (empty($panel_url)) {
                $panel_url = 'https://dash.cloudflare.com';
            }
        }
        
        // Test the connection first
        $test_result = $this->test_panel_connection($panel_type, $username, $api_token, $panel_url);
        
        if (!$test_result['success']) {
            wp_send_json_error([
                'message' => 'Connection test failed: ' . $test_result['message'],
                'details' => $test_result
            ]);
            return;
        }
        
        // Save credentials (encrypted)
        $credentials = [
            'username' => $username,
            'api_token' => $this->encrypt_token($api_token),
            'panel_url' => $panel_url,
            'last_verified' => current_time('mysql'),
            'verified_by' => get_current_user_id()
        ];
        
        update_option(self::OPTION_CREDENTIALS, $credentials, false);
        update_option(self::OPTION_PANEL_TYPE, $panel_type, false);
        
        wp_send_json_success([
            'message' => 'Credentials saved and verified successfully',
            'panel_type' => $panel_type,
            'capabilities' => $test_result['capabilities'] ?? []
        ]);
    }
    
    /**
     * Test panel connection
     */
    public function ajax_test_panel_connection() {
        check_ajax_referer('vmo_builder_nonce', 'security');
        
        $panel_type = sanitize_text_field($_POST['panel_type'] ?? '');
        $username = sanitize_text_field($_POST['username'] ?? '');
        $api_token = sanitize_text_field($_POST['api_token'] ?? '');
        $panel_url = esc_url_raw($_POST['panel_url'] ?? '');
        
        $result = $this->test_panel_connection($panel_type, $username, $api_token, $panel_url);
        
        if ($result['success']) {
            wp_send_json_success($result);
        } else {
            wp_send_json_error($result);
        }
    }
    
    /**
     * Test connection to control panel
     */
    private function test_panel_connection($panel_type, $username, $api_token, $panel_url) {
        switch ($panel_type) {
            case 'cpanel':
                return $this->test_cpanel_connection($username, $api_token, $panel_url);
            case 'plesk':
                return $this->test_plesk_connection($username, $api_token, $panel_url);
            case 'cloudflare':
                return $this->test_cloudflare_connection($api_token);
            case 'directadmin':
                return $this->test_directadmin_connection($username, $api_token, $panel_url);
            default:
                return ['success' => false, 'message' => 'Unknown panel type'];
        }
    }
    
    /**
     * Test cPanel connection
     */
    private function test_cpanel_connection($username, $api_token, $panel_url) {
        $api_url = rtrim($panel_url, '/') . '/execute/Email/list_pops';
        
        $response = wp_remote_get($api_url, [
            'headers' => [
                'Authorization' => 'cpanel ' . $username . ':' . $api_token
            ],
            'timeout' => 30,
            'sslverify' => false
        ]);
        
        if (is_wp_error($response)) {
            return [
                'success' => false,
                'message' => 'Connection failed: ' . $response->get_error_message()
            ];
        }
        
        $code = wp_remote_retrieve_response_code($response);
        $body = json_decode(wp_remote_retrieve_body($response), true);
        
        if ($code === 200 && isset($body['status']) && $body['status'] === 1) {
            return [
                'success' => true,
                'message' => 'Connected to cPanel successfully',
                'capabilities' => ['create_email', 'delete_email', 'list_emails', 'change_password'],
                'existing_emails' => count($body['data'] ?? [])
            ];
        }
        
        // Check for authentication error
        if ($code === 401 || (isset($body['status']) && $body['status'] === 0)) {
            return [
                'success' => false,
                'message' => 'Authentication failed. Check your username and API token.',
                'hint' => 'Generate an API token in cPanel > Security > Manage API Tokens'
            ];
        }
        
        return [
            'success' => false,
            'message' => 'Unexpected response from cPanel (HTTP ' . $code . ')',
            'details' => $body
        ];
    }
    
    /**
     * Test Plesk connection
     */
    private function test_plesk_connection($username, $api_token, $panel_url) {
        $api_url = rtrim($panel_url, '/') . '/api/v2/server';
        
        $response = wp_remote_get($api_url, [
            'headers' => [
                'X-API-Key' => $api_token,
                'Content-Type' => 'application/json'
            ],
            'timeout' => 30,
            'sslverify' => false
        ]);
        
        if (is_wp_error($response)) {
            return [
                'success' => false,
                'message' => 'Connection failed: ' . $response->get_error_message()
            ];
        }
        
        $code = wp_remote_retrieve_response_code($response);
        
        if ($code === 200) {
            return [
                'success' => true,
                'message' => 'Connected to Plesk successfully',
                'capabilities' => ['create_email', 'delete_email', 'list_emails']
            ];
        }
        
        if ($code === 401 || $code === 403) {
            return [
                'success' => false,
                'message' => 'Authentication failed. Check your API key.',
                'hint' => 'Generate an API key in Plesk > Tools & Settings > API'
            ];
        }
        
        return [
            'success' => false,
            'message' => 'Unexpected response from Plesk (HTTP ' . $code . ')'
        ];
    }
    
    /**
     * Test DirectAdmin connection
     */
    private function test_directadmin_connection($username, $api_token, $panel_url) {
        $api_url = rtrim($panel_url, '/') . '/CMD_API_SHOW_DOMAINS';
        
        $response = wp_remote_get($api_url, [
            'headers' => [
                'Authorization' => 'Basic ' . base64_encode($username . ':' . $api_token) // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- HTTP Basic auth header
            ],
            'timeout' => 30,
            'sslverify' => false
        ]);
        
        if (is_wp_error($response)) {
            return [
                'success' => false,
                'message' => 'Connection failed: ' . $response->get_error_message()
            ];
        }
        
        $code = wp_remote_retrieve_response_code($response);
        
        if ($code === 200) {
            return [
                'success' => true,
                'message' => 'Connected to DirectAdmin successfully',
                'capabilities' => ['create_email', 'delete_email', 'list_emails']
            ];
        }
        
        if ($code === 401) {
            return [
                'success' => false,
                'message' => 'Authentication failed. Check your username and login key.',
                'hint' => 'Generate a login key in DirectAdmin > Account Manager > Login Keys'
            ];
        }
        
        return [
            'success' => false,
            'message' => 'Unexpected response from DirectAdmin (HTTP ' . $code . ')'
        ];
    }
    
    /**
     * VMO: Test Cloudflare Connection
     */
    private function test_cloudflare_connection($api_token) {
        if (!class_exists('VMO_Cloudflare_Manager')) {
            require_once plugin_dir_path(__FILE__) . 'class-vmo-cloudflare-manager.php';
        }
        $cf = new VMO_Cloudflare_Manager();
        $cf->set_token($api_token);
        
        $response = $cf->get_zone_id($this->get_site_domain());
        
        if (is_wp_error($response)) {
             return [
                'success' => false,
                'message' => 'Cloudflare connection failed: ' . $response->get_error_message(),
                'hint' => 'Ensure your API Token has Zone.DNS permissions.'
            ];
        }

        return [
            'success' => true,
            'message' => 'Connected to Cloudflare successfully',
            'capabilities' => ['config_dns', 'enable_email_routing']
        ];
    }
    
    /**
     * Provision email account
     */
    public function ajax_provision_email_account() {
        check_ajax_referer('vmo_builder_nonce', 'security');
        
        $email_address = sanitize_email($_POST['email'] ?? '');
        $password = wp_unslash($_POST['password'] ?? '');
        $quota = intval($_POST['quota'] ?? 1024); // Default 1GB
        $category = sanitize_text_field($_POST['category'] ?? 'general');
        
        if (empty($email_address) || !is_email($email_address)) {
            wp_send_json_error(['message' => 'Invalid email address']);
            return;
        }

        // Extract email parts
        $parts = explode('@', $email_address);
        $local_part = $parts[0];
        $domain = $parts[1];
        
        // Get stored credentials
        $credentials = get_option(self::OPTION_CREDENTIALS, []);
        $panel_type = get_option(self::OPTION_PANEL_TYPE, '');

        // Route policy override: when Vultr + Cloudflare + MXroute is ready,
        // always use the Cloudflare route token from plugin settings.
        $route_policy = $this->detect_vultr_cloudflare_mxroute_route($domain);
        if (!empty($route_policy['route_ready'])) {
            $route_token = trim((string) get_option('vmo_cloudflare_api_token', ''));
            if (!empty($route_token)) {
                $credentials = [
                    'username' => 'cloudflare',
                    'api_token' => $this->encrypt_token($route_token),
                    'panel_url' => 'https://dash.cloudflare.com'
                ];
                $panel_type = 'cloudflare';
            }
        }
        
        if (empty($credentials) || empty($panel_type)) {
            wp_send_json_error([
                'message' => 'Control panel not configured. Please set up your hosting credentials first.'
            ]);
            return;
        }
        
        // Generate password if not provided
        if (empty($password)) {
            $password = $this->generate_secure_password();
        }
        
        // Create the email account
        $result = $this->create_email_account(
            $panel_type,
            $credentials,
            $local_part,
            $domain,
            $password,
            $quota
        );
        
        if ($result['success']) {
            // Save email details to VMO database
            $this->save_provisioned_email($email_address, $category, $domain, get_current_user_id());
            
            wp_send_json_success([
                'message' => 'Email account created successfully',
                'email' => $email_address,
                'password' => $password, // Show once for user to save
                'category' => $category,
                'smtp_settings' => $this->get_smtp_settings_for_panel($panel_type, $domain)
            ]);
        } else {
            wp_send_json_error($result);
        }
    }
    
    /**
     * Create email account via control panel API
     */
    private function create_email_account($panel_type, $credentials, $local_part, $domain, $password, $quota) {
        $api_token = $this->decrypt_token($credentials['api_token']);
        $username = $credentials['username'];
        $panel_url = $credentials['panel_url'];
        
        switch ($panel_type) {
            case 'cpanel':
                return $this->create_cpanel_email($username, $api_token, $panel_url, $local_part, $domain, $password, $quota);
            case 'cloudflare':
                 // For Cloudflare, "provisioning" means creating a Routing Rule
                 return $this->create_cloudflare_rule($api_token, $local_part, $domain);
            case 'plesk':
                return $this->create_plesk_email($api_token, $panel_url, $local_part, $domain, $password, $quota);
            case 'directadmin':
                return $this->create_directadmin_email($username, $api_token, $panel_url, $local_part, $domain, $password, $quota);
            default:
                return ['success' => false, 'message' => 'Unknown panel type'];
        }
    }
    
    /**
     * Create email via cPanel UAPI
     */
    private function create_cpanel_email($username, $api_token, $panel_url, $local_part, $domain, $password, $quota) {
        $api_url = rtrim($panel_url, '/') . '/execute/Email/add_pop';
        
        $response = wp_remote_post($api_url, [
            'headers' => [
                'Authorization' => 'cpanel ' . $username . ':' . $api_token,
                'Content-Type' => 'application/x-www-form-urlencoded'
            ],
            'body' => [
                'email' => $local_part,
                'domain' => $domain,
                'password' => $password,
                'quota' => $quota
            ],
            'timeout' => 30,
            'sslverify' => false
        ]);
        
        if (is_wp_error($response)) {
            return [
                'success' => false,
                'message' => 'API request failed: ' . $response->get_error_message()
            ];
        }
        
        $body = json_decode(wp_remote_retrieve_body($response), true);
        
        if (isset($body['status']) && $body['status'] === 1) {
            return [
                'success' => true,
                'message' => 'Email account created via cPanel'
            ];
        }
        
        // Check for specific errors
        $errors = $body['errors'] ?? [];
        if (!empty($errors)) {
            $error_msg = is_array($errors) ? implode(', ', $errors) : $errors;
            
            // Check if email already exists
            if (stripos($error_msg, 'already exists') !== false) {
                return [
                    'success' => false,
                    'message' => 'This email address already exists',
                    'already_exists' => true
                ];
            }
            
            return ['success' => false, 'message' => $error_msg];
        }
        
        return ['success' => false, 'message' => 'Unknown error creating email account'];
    }
    
    /**
     * Create email via Plesk API
     */
    private function create_plesk_email($api_token, $panel_url, $local_part, $domain, $password, $quota) {
        // First, get the domain ID
        $domain_api = rtrim($panel_url, '/') . '/api/v2/domains?name=' . urlencode($domain);
        
        $domain_response = wp_remote_get($domain_api, [
            'headers' => [
                'X-API-Key' => $api_token,
                'Content-Type' => 'application/json'
            ],
            'timeout' => 30,
            'sslverify' => false
        ]);
        
        if (is_wp_error($domain_response)) {
            return ['success' => false, 'message' => 'Failed to fetch domain info'];
        }
        
        $domains = json_decode(wp_remote_retrieve_body($domain_response), true);
        
        if (empty($domains) || !is_array($domains)) {
            return ['success' => false, 'message' => 'Domain not found in Plesk'];
        }
        
        $domain_id = $domains[0]['id'] ?? null;
        
        if (!$domain_id) {
            return ['success' => false, 'message' => 'Could not determine domain ID'];
        }
        
        // Create the email account
        $email_api = rtrim($panel_url, '/') . '/api/v2/cli/mail/create';
        
        $response = wp_remote_post($email_api, [
            'headers' => [
                'X-API-Key' => $api_token,
                'Content-Type' => 'application/json'
            ],
            'body' => json_encode([
                'params' => [
                    '-domain' => $domain,
                    '-name' => $local_part,
                    '-passwd' => $password,
                    '-mailbox' => 'true',
                    '-mbox_quota' => $quota . 'M'
                ]
            ]),
            'timeout' => 30,
            'sslverify' => false
        ]);
        
        if (is_wp_error($response)) {
            return ['success' => false, 'message' => 'API request failed: ' . $response->get_error_message()];
        }
        
        $code = wp_remote_retrieve_response_code($response);
        
        if ($code === 200 || $code === 201) {
            return ['success' => true, 'message' => 'Email account created via Plesk'];
        }
        
        $body = json_decode(wp_remote_retrieve_body($response), true);
        return [
            'success' => false,
            'message' => $body['message'] ?? 'Failed to create email account'
        ];
    }
    
    /**
     * Create email via DirectAdmin API
     */
    /**
     * Create Cloudflare Routing Rule
     */
    private function create_cloudflare_rule($api_token, $local_part, $domain) {
        if (!class_exists('VMO_Cloudflare_Manager')) {
            require_once plugin_dir_path(__FILE__) . 'class-vmo-cloudflare-manager.php';
        }
        
        $cf = new VMO_Cloudflare_Manager();
        $cf->set_token($api_token);
        
        // Ensure routing is enabled
        $cf->enable_email_routing($domain);
        
        // Determine destination (default to site admin email)
        $destination = get_option('admin_email');
        
        // Create the rule
        // user@domain.com -> admin_email
        $full_email = "$local_part@$domain";
        $result = $cf->create_routing_rule($domain, 'literal', $full_email, $destination);
        
        if (is_wp_error($result)) {
            return [
                'success' => false,
                'message' => 'Cloudflare Routing Error: ' . $result->get_error_message()
            ];
        }
        
        return [
            'success' => true,
            'message' => "Routing created: $full_email -> $destination",
            'is_forwarder' => true
        ];
    }

    private function create_directadmin_email($username, $api_token, $panel_url, $local_part, $domain, $password, $quota) {
        $api_url = rtrim($panel_url, '/') . '/CMD_API_POP';
        
        $response = wp_remote_post($api_url, [
            'headers' => [
                'Authorization' => 'Basic ' . base64_encode($username . ':' . $api_token), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- HTTP Basic auth header
                'Content-Type' => 'application/x-www-form-urlencoded'
            ],
            'body' => [
                'action' => 'create',
                'domain' => $domain,
                'user' => $local_part,
                'passwd' => $password,
                'passwd2' => $password,
                'quota' => $quota
            ],
            'timeout' => 30,
            'sslverify' => false
        ]);
        
        if (is_wp_error($response)) {
            return ['success' => false, 'message' => 'API request failed: ' . $response->get_error_message()];
        }
        
        $body = wp_remote_retrieve_body($response);
        
        // DirectAdmin returns URL-encoded response
        parse_str($body, $result);
        
        if (isset($result['error']) && $result['error'] === '0') {
            return ['success' => true, 'message' => 'Email account created via DirectAdmin'];
        }
        
        return [
            'success' => false,
            'message' => $result['text'] ?? 'Failed to create email account'
        ];
    }
    
    /**
     * Get SMTP settings for panel type
     */
    private function get_smtp_settings_for_panel($panel_type, $domain) {
        if ($panel_type === 'cloudflare') {
            return [
                'type' => 'forwarding',
                'description' => 'Email Routing Active. Incoming mail loads to destination. For sending, configure external SMTP.',
                'smtp_host' => 'smtp.gmail.com (Example)',
                'smtp_port' => 587
            ];
        }

        return [
            'smtp_host' => 'mail.' . $domain,
            'smtp_port' => 587,
            'smtp_secure' => 'tls',
            'imap_host' => 'mail.' . $domain,
            'imap_port' => 993,
            'pop3_host' => 'mail.' . $domain,
            'pop3_port' => 995,
            'webmail' => $this->get_webmail_url($panel_type, $domain)
        ];
    }
    
    /**
     * Get webmail URL
     */
    private function get_webmail_url($panel_type, $domain) {
        switch ($panel_type) {
            case 'cpanel':
                return 'https://' . $domain . ':2096';
            case 'plesk':
                return 'https://webmail.' . $domain;
            case 'directadmin':
                return 'https://' . $domain . '/webmail';
            default:
                return 'https://webmail.' . $domain;
        }
    }
    
    /**
     * Save provisioned email to database
     */
    private function save_provisioned_email($email, $category, $domain, $user_id) {
        global $wpdb;
        
        $table = $wpdb->prefix . 'vmo_email_accounts';
        
        // Create table if not exists
        $this->maybe_create_email_table();
        
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        return $wpdb->insert($table, [
            'user_id' => $user_id,
            'email_address' => $email,
            'category' => $category,
            'domain' => $domain,
            'provisioned_at' => current_time('mysql'),
            'provisioned_via' => get_option(self::OPTION_PANEL_TYPE, 'unknown'),
            'status' => 'active'
        ]);
    }
    
    /**
     * Create email accounts table
     */
    private function maybe_create_email_table() {
        global $wpdb;
        
        $table = $wpdb->prefix . 'vmo_email_accounts';
        
        if ($wpdb->get_var("SHOW TABLES LIKE '$table'") === $table) {
            return;
        }
        
        $charset = $wpdb->get_charset_collate();
        
        $sql = "CREATE TABLE $table (
            id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            user_id bigint(20) unsigned NOT NULL,
            email_address varchar(255) NOT NULL,
            category varchar(50) DEFAULT 'general',
            domain varchar(255) NOT NULL,
            provisioned_at datetime DEFAULT CURRENT_TIMESTAMP,
            provisioned_via varchar(50) DEFAULT 'unknown',
            status varchar(20) DEFAULT 'active',
            PRIMARY KEY (id),
            KEY user_id (user_id),
            KEY email_address (email_address),
            KEY domain (domain)
        ) $charset;";
        
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }
    
    /**
     * Generate secure password
     */
    private function generate_secure_password($length = 16) {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*';
        $password = '';
        
        for ($i = 0; $i < $length; $i++) {
            $password .= $chars[random_int(0, strlen($chars) - 1)];
        }
        
        return $password;
    }
    
    /**
     * Encrypt API token for storage
     */
    private function encrypt_token($token) {
        $key = $this->get_encryption_key();
        $iv = random_bytes(16);
        $encrypted = openssl_encrypt($token, 'AES-256-CBC', $key, 0, $iv);
        return base64_encode($iv . $encrypted); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- encryption/decryption
    }
    
    /**
     * Decrypt API token
     */
    private function decrypt_token($encrypted) {
        $key = $this->get_encryption_key();
        $data = base64_decode($encrypted); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- encryption/decryption
        $iv = substr($data, 0, 16);
        $encrypted_token = substr($data, 16);
        return openssl_decrypt($encrypted_token, 'AES-256-CBC', $key, 0, $iv);
    }
    
    /**
     * Get encryption key
     */
    private function get_encryption_key() {
        $key = get_option('vmo_encryption_key');
        
        if (!$key) {
            $key = base64_encode(random_bytes(32)); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- data encoding
            update_option('vmo_encryption_key', $key, false);
        }
        
        return base64_decode($key); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- data decoding
    }
}

// Initialize
new VMO_Email_Provisioner();
