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

add_action('admin_menu', 'easy_gpt_add_admin_menu');

function easy_gpt_add_admin_menu() {
    add_menu_page(
        'Easy GPT Settings',          // Page title
        'Easy GPT',                   // Menu title
        'manage_options',             // Capability required
        'easy-gpt-settings',          // Menu slug
        'easy_gpt_settings_page',     // Function to display the menu content
        plugins_url('../images/Logo-Pequeno-admin.png', __FILE__),  // Custom icon
        2                             // Position in the menu
    );

    // Add submenus
    add_submenu_page(
        'easy-gpt-settings',
        'Settings',
        'Settings',
        'manage_options',
        'easy-gpt-settings',          // Reuse the slug for the settings page
        'easy_gpt_settings_page'
    );

    // Submenu for viewing past bulk generations
    add_submenu_page(
        'easy-gpt-settings',
        'View Bulk Generations',       // Page title
        'View Bulk Generations',       // Menu title
        'manage_options',              // Capability
        'easy-gpt-bulk-generate-view', // Menu slug
        'easy_gpt_bulk_generations_view_page' // Function to display the menu content
    );
    
    add_submenu_page(
        'easy-gpt-settings',
        'New Bulk Article Generation', // Page title
        'New Bulk Generate',           // Menu title
        'manage_options',              // Capability
        'easy-gpt-bulk-generate-new',  // Menu slug
        'easy_gpt_bulk_generate_page'  // Function to display the menu content
    );

    add_submenu_page(
        'easy-gpt-settings',
        'Article Refresher',           // Page title
        'Article Refresher',           // Menu title
        'manage_options',              // Capability
        'easy-gpt-refresh',            // Menu slug
        'easy_gpt_refresh_page_callback' // Callback function (la que generamos antes)
    );

	add_submenu_page(
		'easy-gpt-settings',
		'Translations',
		'Translations',
		'manage_options',
		'easy-gpt-translations',
		'easy_gpt_render_translations_page'
	);

    add_submenu_page(
        'easy-gpt-settings',          // Parent slug
        'Comments',                   // Page title
        'Comments',                   // Menu title
        'manage_options',             // Capability
        'easy-gpt-comments',          // Menu slug
        'easy_gpt_comments_page'      // Function callback
    );


    add_submenu_page(
        'easy-gpt-settings',
        'Cron Job Status',       // Page title
        'Cron Job Status',       // Menu title
        'manage_options',        // Capability
        'easy-gpt-cron-status',  // Menu slug
        'easy_gpt_status_page'   // Function to display the status page
    );


    add_submenu_page(
        'easy-gpt-settings',
        'How to Use',
        'How to Use',
        'manage_options',
        'easy-gpt-how-to-use',
        'easy_gpt_how_to_use_page'
    );

    add_submenu_page(
        'easy-gpt-settings',
        'Support',
        'Support',
        'manage_options',
        'easy-gpt-support',
        'easy_gpt_support_page'
    );
    add_submenu_page(
        'easy-gpt-settings',
        'License',
        'License',
        'manage_options',
        'easy-gpt-license',
        'easy_gpt_license_page'
    );

    /*add_submenu_page(
        'easy-gpt-settings',
        'Cost',
        'Cost',
        'manage_options',
        'easy-gpt-cost',
        'easy_gpt_cost_page'
    );*/
}


function easy_gpt_settings_page() {
    ?>
    <div class="wrap">
        <h2>Easy GPT for WordPress Settings</h2>
        <form method="post" action="options.php" class="easy-gpt-settings-form">
            <?php
            settings_fields('easy_gpt_options_group'); ?>
            <div id="easy-gpt-tabs-wrapper">
                <?php do_settings_sections('easy-gpt-settings'); ?>
            </div>
			<?php
            submit_button('Save Changes', 'primary', 'easy-gpt-save-button');
            ?>
        </form>
    </div>
    <?php
}

// Add cost column to the post list in the admin panel
function easy_gpt_add_cost_column($columns) {
    $columns['easy_gpt_cost'] = __('Cost', 'easy-gpt-for-wp');
    return $columns;
}
add_filter('manage_posts_columns', 'easy_gpt_add_cost_column');
add_filter('manage_pages_columns', 'easy_gpt_add_cost_column');

// Display the cost in the new column for the post list
function easy_gpt_display_cost_in_column($column, $post_id) {
    if ($column == 'easy_gpt_cost') {
        $cost = get_post_meta($post_id, '_easy_gpt_cost', true);
        if ($cost) {
            echo esc_html($cost);
        } else {
            echo esc_html__('No data', 'easy-gpt-for-wp');
        }
    }
}
add_action('manage_posts_custom_column', 'easy_gpt_display_cost_in_column', 10, 2);
add_action('manage_pages_custom_column', 'easy_gpt_display_cost_in_column', 10, 2);
