<?php

// Old menu - disabled in favor of new AI_Magic_Settings class
// This prevents duplicate menu items
/*
function ai_magic_add_menu() {
    add_menu_page(
        'AI Magic',
        'AI Magic',
        'manage_options',
        'ai-magic-menu',
        'ai_magic_menu_page',
        plugin_dir_url(__FILE__) .'icon/magic.png'

    );

    add_submenu_page(
        'ai-magic-menu',
        'Settings',
        'Settings',
        'manage_options',
        'ai-magic-settings',
        'ai_magic_settings_page'
    );

    add_submenu_page(
        'ai-magic-menu',
        'Subscription',
        'Subscription',
        'manage_options',
        'ai-magic-subscription',
        'ai_magic_subscription_page'
    );

    // add_submenu_page(
    //     'ai-magic-menu',
    //     'About',
    //     'About',
    //     'manage_options',
    //     'ai-magic-about',
    //     'ai_magic_about_page'
    // );
}

add_action('admin_menu', 'ai_magic_add_menu');
*/


add_action('wp_ajax_save_plugin_settings', 'save_ai_magic_settings');

// API Key Test Handler
add_action('wp_ajax_ai_magic_test_api_key', 'ai_magic_test_api_key');

function ai_magic_test_api_key()
{
    // Verify nonce for CSRF protection
    if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'ai_magic_settings_nonce')) {
        wp_send_json_error(['message' => 'Security check failed']);
        wp_die();
    }

    // Check user permissions
    if (!current_user_can('manage_options')) {
        wp_send_json_error(['message' => 'Insufficient permissions']);
        wp_die();
    }

    $provider = isset($_POST['provider']) ? sanitize_text_field($_POST['provider']) : '';
    $api_key = isset($_POST['api_key']) ? sanitize_text_field($_POST['api_key']) : '';

    if (empty($provider) || empty($api_key)) {
        wp_send_json_error(['message' => 'Provider and API key are required']);
        wp_die();
    }

    $result = ['success' => false, 'message' => 'Unknown provider'];

    switch ($provider) {
        case 'openai':
            $response = wp_remote_get('https://api.openai.com/v1/models', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $api_key,
                ],
                'timeout' => 15,
            ]);
            if (is_wp_error($response)) {
                $result = ['success' => false, 'message' => 'Connection failed: ' . $response->get_error_message()];
            } else {
                $code = wp_remote_retrieve_response_code($response);
                if ($code === 200) {
                    $result = ['success' => true, 'message' => 'API key is valid! Connected to OpenAI.'];
                } else {
                    $body = json_decode(wp_remote_retrieve_body($response), true);
                    $error_msg = isset($body['error']['message']) ? $body['error']['message'] : 'Invalid API key';
                    $result = ['success' => false, 'message' => $error_msg];
                }
            }
            break;

        case 'anthropic':
            $response = wp_remote_post('https://api.anthropic.com/v1/messages', [
                'headers' => [
                    'x-api-key' => $api_key,
                    'anthropic-version' => '2023-06-01',
                    'content-type' => 'application/json',
                ],
                'body' => json_encode([
                    'model' => 'claude-3-haiku-20240307',
                    'max_tokens' => 10,
                    'messages' => [['role' => 'user', 'content' => 'Hi']],
                ]),
                'timeout' => 15,
            ]);
            if (is_wp_error($response)) {
                $result = ['success' => false, 'message' => 'Connection failed: ' . $response->get_error_message()];
            } else {
                $code = wp_remote_retrieve_response_code($response);
                if ($code === 200) {
                    $result = ['success' => true, 'message' => 'API key is valid! Connected to Anthropic.'];
                } else {
                    $body = json_decode(wp_remote_retrieve_body($response), true);
                    $error_msg = isset($body['error']['message']) ? $body['error']['message'] : 'Invalid API key';
                    $result = ['success' => false, 'message' => $error_msg];
                }
            }
            break;

        case 'google':
            $response = wp_remote_get('https://generativelanguage.googleapis.com/v1beta/models?key=' . $api_key, [
                'timeout' => 15,
            ]);
            if (is_wp_error($response)) {
                $result = ['success' => false, 'message' => 'Connection failed: ' . $response->get_error_message()];
            } else {
                $code = wp_remote_retrieve_response_code($response);
                if ($code === 200) {
                    $result = ['success' => true, 'message' => 'API key is valid! Connected to Google AI.'];
                } else {
                    $body = json_decode(wp_remote_retrieve_body($response), true);
                    $error_msg = isset($body['error']['message']) ? $body['error']['message'] : 'Invalid API key';
                    $result = ['success' => false, 'message' => $error_msg];
                }
            }
            break;

        case 'openrouter':
            $response = wp_remote_get('https://openrouter.ai/api/v1/auth/key', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $api_key,
                ],
                'timeout' => 15,
            ]);
            if (is_wp_error($response)) {
                $result = ['success' => false, 'message' => 'Connection failed: ' . $response->get_error_message()];
            } else {
                $code = wp_remote_retrieve_response_code($response);
                if ($code === 200) {
                    $body = json_decode(wp_remote_retrieve_body($response), true);
                    $label = isset($body['data']['label']) ? $body['data']['label'] : 'OpenRouter';
                    $result = ['success' => true, 'message' => 'API key is valid! Connected to ' . $label];
                } else {
                    $body = json_decode(wp_remote_retrieve_body($response), true);
                    $error_msg = isset($body['error']['message']) ? $body['error']['message'] : 'Invalid API key';
                    $result = ['success' => false, 'message' => $error_msg];
                }
            }
            break;
    }

    if ($result['success']) {
        wp_send_json_success(['message' => $result['message']]);
    } else {
        wp_send_json_error(['message' => $result['message']]);
    }

    wp_die();
}

function save_ai_magic_settings()
{
    // Verify nonce for CSRF protection
    if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'ai_magic_settings_nonce')) {
        wp_send_json_error('Security check failed');
        wp_die();
    }

    // Check user permissions
    if (!current_user_can('manage_options')) {
        wp_send_json_error('Insufficient permissions');
        wp_die();
    }

    if (isset($_POST['settings'])) {
        // Process and save the settings
        parse_str($_POST['settings'], $settings);

        // Whitelist of allowed settings to prevent arbitrary option updates
        $allowed_settings = array(
            'ai_magic_api_key',
            'ai_magic_model',
            'ai_magic_max_tokens',
            'ai_magic_temperature',
            'ai_magic_auto_post',
            'ai_magic_post_status',
            'ai_magic_post_category',
            // New Provider Settings
            'ai_magic_active_provider',
            'ai_magic_api_key_openai',
            'ai_magic_api_key_anthropic',
            'ai_magic_api_key_google',
            'ai_magic_api_key_openrouter',
            'ai_magic_openai_model',
            'ai_magic_openai_custom_model',
            'ai_magic_anthropic_model',
            'ai_magic_anthropic_custom_model',
            'ai_magic_google_model',
            'ai_magic_google_custom_model',
            'ai_magic_openrouter_model',
            'ai_magic_openrouter_custom_model',
            // Content Preferences
            'ai_magic_language',
            'ai_magic_style',
            'ai_magic_tone',
            'ai_magic_perspective'
        );

        // Save settings to the database with sanitization
        foreach ($settings as $settin_key => $settin_value) {
            // Only save whitelisted settings
            if (in_array($settin_key, $allowed_settings)) {
                update_option($settin_key, sanitize_text_field($settin_value));
            }
        }

        // Send a success response
        wp_send_json_success('Settings saved successfully');
    } else {
        // Send an error response if needed
        wp_send_json_error('Error while saving settings');
    }

    wp_die();
}


add_action('wp_ajax_save_generate_text', 'save_ai_magic_generate_text');

function save_ai_magic_generate_text()
{
    // Verify nonce for CSRF protection
    if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'ai_magic_generate_nonce')) {
        wp_send_json_error('Security check failed');
        wp_die();
    }

    // Check user permissions - users should be able to edit posts
    if (!current_user_can('edit_posts')) {
        wp_send_json_error('Insufficient permissions');
        wp_die();
    }

    if (isset($_POST['ai_magic_try_text'])) {

        $ai_magic_try_text = sanitize_textarea_field($_POST['ai_magic_try_text']);

        if (empty($ai_magic_try_text)) {
            wp_send_json_error(['message' => 'Invalid prompt', 'text' => '', 'data' => null]);
            wp_die();
        }

        $openai = new AI_Magic_OpenAI();
        $response = (object) $openai->generateText($ai_magic_try_text);

        if ($response->type == 'error') {
            wp_send_json_error(['message' => 'Error while generating text', 'text' => esc_html($response->message)]);
        }
        // Send a success response
        wp_send_json_success(['message' => 'Generating text successfully', 'text' => esc_html($response->message), 'data' => $response->data]);
    } else {
        // Send an error response if needed
        wp_send_json_error('Error while generating text');
    }

    wp_die();
}

function ai_magic_menu_page()
{
    ai_magic_about_page();
}

function ai_magic_settings_page()
{
    include(plugin_dir_path(__FILE__) . 'partials/ai-magic-settings-page.php');
}

function ai_magic_subscription_page()
{
    ?>

    <div class="wrap">
        <h2>Premium Features</h2>
        <div class="subscription-info">
            <h3>Subscription Advantages</h3>
            <p>Our subscription plans offer a range of benefits to meet your content creation needs. Whether you choose our
                Free, Pro, or Premium plan, you'll enjoy the following advantages:</p>
            <ul>
                <li>Access to advanced content creation features.</li>
                <li>Priority support for quick assistance.</li>
                <li>Integration with third-party applications through API access.</li>
                <li>Personalized training to help you get the most out of our AI-powered content generation tool.</li>
            </ul>
        </div>

        <div class="pricing-table">
            <div class="pricing-column">
                <h3>Free</h3>
                <div class="price">0$</div>
                <ul>
                    <li>Basic Features</li>
                    <li>Limited Support</li>
                </ul>
                <a href="#" class="button">Get Started</a>
            </div>
            <div class="pricing-column">
                <h3>Pro</h3>
                <div class="price">$9.99/month</div>
                <ul>
                    <li>All Free Features</li>
                    <li>Premium Support</li>
                </ul>
                <a href="#" class="button">Get Started</a>
            </div>
            <div class="pricing-column">
                <h3>Premium</h3>
                <div class="price">$19.99/month</div>
                <ul>
                    <li>All Pro Features</li>
                    <li>Priority Support</li>
                </ul>
                <a href="#" class="button">Get Started</a>
            </div>
        </div>

        <div class="subscription-about">
            <h3>About Subscriptions</h3>
            <p>Our premium subscription plans empower you to create high-quality content effortlessly using AI technology.
                With a focus on user-friendly features and SEO optimization, our tool is designed to save you time and
                enhance your website's content.</p>
            <p>Unlock the full potential of AI content creation with our subscription plans and take your content to the
                next level.</p>
        </div>
    </div>

    <style>
        .pricing-table {
            display: flex;
            justify-content: center;
        }

        .pricing-column {
            text-align: center;
            margin: 20px;
            padding: 20px;
            border: 1px solid #ccc;
            background-color: #f9f9f9;
        }

        .price {
            font-size: 24px;
            margin: 10px 0;
        }

        ul {
            list-style: none;
            padding: 0;
        }

        li {
            margin: 5px 0;
        }

        .button {
            display: block;
            width: 100%;
            padding: 10px;
            background-color: #0073e6;
            color: #fff;
            text-decoration: none;
            border-radius: 5px;
            margin-top: 10px;
        }
    </style>

    <?php
}

function ai_magic_about_page()
{
    ?>
    <div class="wrap">
        <h2>About AI Magic Plugin</h2>
        <p>Welcome to the AI Magic Plugin for WordPress, your AI-powered article and content generator.</p>
        <p>AI Magic is based on the ChatGPT technology and can assist you in generating high-quality articles and content
            for your website.</p>
        <p>Feel the magic of AI as it creates content that meets your requirements and saves you time.</p>

        <p>Welcome to the AI Magic Plugin for WordPress, your AI-powered article and content generator.</p>
        <p>AI Magic is a cutting-edge content creation tool based on ChatGPT, one of the most advanced AI language models.
            It empowers you to effortlessly produce high-quality articles and web content with the click of a button.</p>
        <p>Key Features:
            - Instantly generate SEO-friendly articles.
            - Customize content for your specific needs.
            - Save time and effort on content creation.
            - Enhance your website's quality with AI-generated content.
        </p>
        <p>Experience the magic of AI with AI Magic and unlock a new level of productivity for your WordPress website.</p>

        <style>
            .wrap {
                background-color: #f5f5f5;
                padding: 20px;
                border: 1px solid #ccc;
                border-radius: 5px;
            }

            h2 {
                color: #333;
            }

            p {
                color: #666;
            }
        </style>
    </div>
    <?php
}