<?php
/**
 * Provide a admin area view for the Developer Mode page
 *
 * This file is used to markup the admin-facing aspects of the plugin.
 *
 * @link       https://example.com
 * @since      1.0.0
 *
 * @package    Subscription_System
 */

// If this file is called directly, abort.
if (!defined('WPINC')) {
    die;
}

// Get WordPress version
global $wp_version;
if (!isset($wp_version)) {
    include(ABSPATH . WPINC . '/version.php');
}
?>

<div class="wrap subscription-system-developer-mode">
    <h1><?php echo esc_html(get_admin_page_title()); ?></h1>
    
    <div class="notice notice-warning">
        <p><strong><?php esc_html_e('Warning:', 'subscription-system'); ?></strong> <?php esc_html_e('Developer Mode is intended for advanced users and developers only. Changes made here can affect the functionality of your subscription system.', 'subscription-system'); ?></p>
    </div>
    
    <div class="subscription-system-card">
        <h2><?php esc_html_e('Database Tools', 'subscription-system'); ?></h2>
        
        <div class="subscription-system-card-content">
            <h3><?php esc_html_e('Recreate Database Tables', 'subscription-system'); ?></h3>
            <p><?php esc_html_e('Use these options to recreate database tables if you encounter issues with the database structure.', 'subscription-system'); ?></p>
            
            <form method="post" action="">
                <?php wp_nonce_field('subscription_recreate_tables', 'subscription_recreate_tables_nonce'); ?>
                <table class="form-table">
                    <tr>
                        <th scope="row"><?php esc_html_e('Subscription Plans Table', 'subscription-system'); ?></th>
                        <td>
                            <input type="submit" name="recreate_plans_table" class="button button-secondary" value="<?php esc_attr_e('Recreate Plans Table', 'subscription-system'); ?>">
                        </td>
                    </tr>
                    <tr>
                        <th scope="row"><?php esc_html_e('Plan Categories Table', 'subscription-system'); ?></th>
                        <td>
                            <input type="submit" name="recreate_categories_table" class="button button-secondary" value="<?php esc_attr_e('Recreate Categories Table', 'subscription-system'); ?>">
                        </td>
                    </tr>
                    <tr>
                        <th scope="row"><?php esc_html_e('Subscriptions Table', 'subscription-system'); ?></th>
                        <td>
                            <input type="submit" name="recreate_subscriptions_table" class="button button-secondary" value="<?php esc_attr_e('Recreate Subscriptions Table', 'subscription-system'); ?>">
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </div>
    
    <div class="subscription-system-card">
        <h2><?php esc_html_e('Debug Information', 'subscription-system'); ?></h2>
        
        <div class="subscription-system-card-content">
            <h3><?php esc_html_e('System Information', 'subscription-system'); ?></h3>
            
            <table class="widefat" cellspacing="0">
                <tbody>
                    <tr>
                        <th><?php esc_html_e('PHP Version', 'subscription-system'); ?></th>
                        <td><?php echo esc_html(phpversion()); ?></td>
                    </tr>
                    <tr>
                        <th><?php esc_html_e('WordPress Version', 'subscription-system'); ?></th>
                        <td><?php echo esc_html($wp_version); ?></td>
                    </tr>
                    <tr>
                        <th><?php esc_html_e('Plugin Version', 'subscription-system'); ?></th>
                        <td>
                            <?php 
                            if (defined('SUBS_SYS_VERSION')) {
                                echo esc_html(SUBS_SYS_VERSION);
                            } else {
                                echo esc_html__('Unknown', 'subscription-system');
                            }
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <th><?php esc_html_e('Database Prefix', 'subscription-system'); ?></th>
                        <td><?php global $wpdb; echo esc_html($wpdb->prefix); ?></td>
                    </tr>
                    <tr>
                        <th><?php esc_html_e('Stripe SDK Status', 'subscription-system'); ?></th>
                        <td>
                            <?php 
                            if (class_exists('\Stripe\Stripe')) {
                                echo '<span class="status-ok">' . esc_html__('Loaded', 'subscription-system') . '</span>';
                            } else {
                                echo '<span class="status-error">' . esc_html__('Not Loaded', 'subscription-system') . '</span>';
                            }
                            ?>
                        </td>
                    </tr>
                </tbody>
            </table>
            
            <h3><?php esc_html_e('Database Tables', 'subscription-system'); ?></h3>
            
            <table class="widefat" cellspacing="0">
                <thead>
                    <tr>
                        <th><?php esc_html_e('Table Name', 'subscription-system'); ?></th>
                        <th><?php esc_html_e('Status', 'subscription-system'); ?></th>
                        <th><?php esc_html_e('Records', 'subscription-system'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    global $wpdb;
                    $tables = [
                        $wpdb->prefix . 'subscription_plans' => esc_html__('Plans', 'subscription-system'),
                        $wpdb->prefix . 'subscription_plan_categories' => esc_html__('Categories', 'subscription-system'),
                        $wpdb->prefix . 'subscriptions' => esc_html__('Subscriptions', 'subscription-system'),
                    ];
                    
                    foreach ($tables as $table_name => $label) {
                        $table_exists = $wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name;
                        $count = 0;
                        
                        if ($table_exists) {
                            $count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name");
                        }
                        
                        echo '<tr>';
                        echo '<td>' . esc_html($table_name) . '</td>';
                        echo '<td>';
                        if ($table_exists) {
                            echo '<span class="status-ok">' . esc_html__('Exists', 'subscription-system') . '</span>';
                        } else {
                            echo '<span class="status-error">' . esc_html__('Missing', 'subscription-system') . '</span>';
                        }
                        echo '</td>';
                        echo '<td>' . esc_html($count) . '</td>';
                        echo '</tr>';
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
    
    <div class="subscription-system-card">
        <h2><?php esc_html_e('Logging Options', 'subscription-system'); ?></h2>
        
        <div class="subscription-system-card-content">
            <form method="post" action="">
                <?php wp_nonce_field('subscription_logging_options', 'subscription_logging_nonce'); ?>
                <table class="form-table">
                    <tr>
                        <th scope="row"><?php esc_html_e('Enable Debug Logging', 'subscription-system'); ?></th>
                        <td>
                            <label>
                                <input type="checkbox" name="enable_debug_logging" value="1" <?php checked(get_option('subscription_enable_debug_logging', 0), 1); ?>>
                                <?php esc_html_e('Enable detailed logging for debugging purposes', 'subscription-system'); ?>
                            </label>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row"><?php esc_html_e('Log Stripe API Calls', 'subscription-system'); ?></th>
                        <td>
                            <label>
                                <input type="checkbox" name="log_stripe_api" value="1" <?php checked(get_option('subscription_log_stripe_api', 0), 1); ?>>
                                <?php esc_html_e('Log all Stripe API interactions', 'subscription-system'); ?>
                            </label>
                        </td>
                    </tr>
                </table>
                
                <p class="submit">
                    <input type="submit" name="save_logging_options" class="button button-primary" value="<?php esc_attr_e('Save Logging Options', 'subscription-system'); ?>">
                </p>
            </form>
            
            <?php if (file_exists(WP_CONTENT_DIR . '/subscription-system-debug.log')): ?>
                <h3><?php esc_html_e('Debug Log', 'subscription-system'); ?></h3>
                <p>
                    <?php 
                    $log_size = size_format(filesize(WP_CONTENT_DIR . '/subscription-system-debug.log'));
                    /* translators: %s: size of the debug log file (e.g. "2 MB") */
                    printf(esc_html__('Current log file size: %s', 'subscription-system'), $log_size); 
                    ?>
                </p>
                <form method="post" action="">
                    <?php wp_nonce_field('subscription_clear_log', 'subscription_clear_log_nonce'); ?>
                    <p>
                        <input type="submit" name="clear_debug_log" class="button button-secondary" value="<?php esc_attr_e('Clear Debug Log', 'subscription-system'); ?>">
                        <a href="<?php echo esc_url(admin_url('admin-ajax.php?action=download_subscription_log&nonce=' . wp_create_nonce('download_subscription_log'))); ?>" class="button button-secondary"><?php esc_html_e('Download Log', 'subscription-system'); ?></a>
                    </p>
                </form>
            <?php endif; ?>
        </div>
    </div>
    
    <div class="subscription-system-card">
        <h2><?php esc_html_e('Reset Settings', 'subscription-system'); ?></h2>
        
        <div class="subscription-system-card-content">
            <div class="backup-warning">
                <h2><?php esc_html_e('⚠️ BACKUP REQUIRED ⚠️', 'subscription-system'); ?></h2>
                <p><?php esc_html_e('You MUST create a complete database backup before using any reset options.', 'subscription-system'); ?></p>
                <p><?php esc_html_e('These operations permanently delete data and CANNOT be undone.', 'subscription-system'); ?></p>
            </div>
            
            <h3><?php esc_html_e('Reset Plugin Settings', 'subscription-system'); ?></h3>
            <p><?php esc_html_e('Use these options to reset various plugin settings. This can help resolve issues caused by corrupted settings.', 'subscription-system'); ?></p>
            
            <div class="notice notice-error">
                <p><strong><?php esc_html_e('Warning:', 'subscription-system'); ?></strong> <?php esc_html_e('Resetting settings will permanently delete the selected data. This action cannot be undone.', 'subscription-system'); ?></p>
            </div>
            
            <form method="post" action="" onsubmit="return confirm('<?php echo esc_js(esc_html__('FINAL WARNING: Have you backed up your database? Proceeding will permanently delete the selected settings. This action CANNOT be undone.', 'subscription-system')); ?>');">
                <?php wp_nonce_field('subscription_reset_settings', 'subscription_reset_settings_nonce'); ?>
                <table class="form-table">
                    <tr>
                        <th scope="row"><?php esc_html_e('General Settings', 'subscription-system'); ?></th>
                        <td>
                            <label>
                                <input type="checkbox" name="reset_general_settings" value="1">
                                <?php esc_html_e('Reset all general plugin settings', 'subscription-system'); ?>
                            </label>
                            <p class="description"><?php esc_html_e('This will reset currency, pricing layouts, and other general settings.', 'subscription-system'); ?></p>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row"><?php esc_html_e('Payment Settings', 'subscription-system'); ?></th>
                        <td>
                            <label>
                                <input type="checkbox" name="reset_payment_settings" value="1">
                                <?php esc_html_e('Reset all payment gateway settings', 'subscription-system'); ?>
                            </label>
                            <p class="description"><?php esc_html_e('This will reset Stripe API keys and other payment-related settings.', 'subscription-system'); ?></p>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row"><?php esc_html_e('Email Settings', 'subscription-system'); ?></th>
                        <td>
                            <label>
                                <input type="checkbox" name="reset_email_settings" value="1">
                                <?php esc_html_e('Reset all email notification settings', 'subscription-system'); ?>
                            </label>
                            <p class="description"><?php esc_html_e('This will reset email templates and notification settings.', 'subscription-system'); ?></p>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row"><?php esc_html_e('Page Settings', 'subscription-system'); ?></th>
                        <td>
                            <label>
                                <input type="checkbox" name="reset_page_settings" value="1">
                                <?php esc_html_e('Reset all page assignments', 'subscription-system'); ?>
                            </label>
                            <p class="description"><?php esc_html_e('This will reset all page assignments for login, account, etc.', 'subscription-system'); ?></p>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row"><?php esc_html_e('All Plugin Options', 'subscription-system'); ?></th>
                        <td>
                            <label>
                                <input type="checkbox" name="reset_all_options" value="1">
                                <?php esc_html_e('Reset ALL plugin options', 'subscription-system'); ?>
                            </label>
                            <p class="description"><?php esc_html_e('This will reset all plugin options to default values. Use with caution!', 'subscription-system'); ?></p>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row"><?php esc_html_e('User Metadata', 'subscription-system'); ?></th>
                        <td>
                            <label>
                                <input type="checkbox" name="reset_user_metadata" value="1">
                                <?php esc_html_e('Reset all subscription-related user metadata', 'subscription-system'); ?>
                            </label>
                            <p class="description"><?php esc_html_e('This will remove all subscription metadata from user accounts.', 'subscription-system'); ?></p>
                        </td>
                    </tr>
                </table>
                
                <p class="submit">
                    <input type="submit" name="reset_settings" class="button button-secondary" value="<?php esc_attr_e('Reset Selected Settings', 'subscription-system'); ?>">
                </p>
            </form>
        </div>
    </div>
</div>

<style>
.subscription-system-card {
    background: #fff;
    border: 1px solid #ccd0d4;
    box-shadow: 0 1px 1px rgba(0,0,0,.04);
    margin-top: 20px;
    padding: 0;
}

.subscription-system-card h2 {
    border-bottom: 1px solid #ccd0d4;
    margin: 0;
    padding: 15px;
    font-size: 14px;
}

.subscription-system-card-content {
    padding: 15px;
}

.status-ok {
    color: #46b450;
    font-weight: bold;
}

.status-error {
    color: #dc3232;
    font-weight: bold;
}

.backup-warning {
    background-color: #f44336;
    color: white;
    padding: 15px;
    margin-bottom: 20px;
    border-radius: 4px;
    text-align: center;
}

.backup-warning h2 {
    color: white;
    margin-top: 0;
    font-size: 24px;
    border-bottom: none;
    padding: 0;
}

.backup-warning p {
    font-size: 16px;
    margin: 10px 0;
}
</style> 