<?php
namespace ComptaFlowPro\Admin\Screens;

defined('ABSPATH') || exit;

class BackupScreen
{
    public static function render(): void
    {
        if (!current_user_can('manage_options')) {
            wp_die(esc_html__('You do not have sufficient permissions.', 'comptaflow-by-meetempo'));
        }
        
        global $wpdb;
        
        echo '<div class="wrap">';
        echo '<h1>' . esc_html__('Backup & Restore', 'comptaflow-by-meetempo') . '</h1>';
        
        // Get ComptaFlow tables
        $tables = $wpdb->get_col(
            "SELECT table_name 
             FROM information_schema.tables 
             WHERE table_schema = DATABASE() 
             AND table_name LIKE '{$wpdb->prefix}cfp_%'"
        );
        
        self::renderBackupInstructions($tables);
        self::renderRestoreInstructions($tables);
        self::renderFactoryReset();
        
        echo '</div>';
    }
    
    private static function renderBackupInstructions(array $tables): void
    {
        echo '<div style="background: white; padding: 20px; margin: 20px 0; border: 1px solid #ddd; border-radius: 5px;">';
        echo '<h2>📥 ' . esc_html__('Create a Backup', 'comptaflow-by-meetempo') . '</h2>';
        echo '<p>' . esc_html__('Use phpMyAdmin to create a reliable backup of your ComptaFlow data.', 'comptaflow-by-meetempo') . '</p>';
        
        echo '<div style="background: #e7f3ff; padding: 20px; border-left: 4px solid #0073aa; margin: 20px 0;">';
        echo '<h3>' . esc_html__('Steps to follow:', 'comptaflow-by-meetempo') . '</h3>';
        echo '<ol style="line-height: 1.8;">';
        echo '<li>' . esc_html__('Connect to phpMyAdmin (via your host, cPanel, or Local by Flywheel)', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Select your WordPress database', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Click on the "Export" tab', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Select "Custom method"', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('In the table list, select ONLY the following ComptaFlow tables:', 'comptaflow-by-meetempo') . '</li>';
        echo '</ol>';
        
        echo '<div style="background: white; padding: 15px; border: 1px solid #ddd; margin: 10px 0; max-height: 300px; overflow-y: auto;">';
        echo '<p><strong>' . esc_html__('Tables to backup:', 'comptaflow-by-meetempo') . '</strong></p>';
        echo '<ul style="font-family: monospace; font-size: 13px;">';
        foreach ($tables as $table) {
            echo '<li>✓ ' . esc_html($table) . '</li>';
        }
        echo '</ul>';
        echo '</div>';
        
        echo '<ol start="6" style="line-height: 1.8;">';
        echo '<li>' . esc_html__('Export format: SQL', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Compression: None (or gzip if you prefer)', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Click "Execute"', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('The file will download', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . sprintf(
            /* translators: %s: current date */
            esc_html__('Rename it: backup_ComptaFlow_%s.sql', 'comptaflow-by-meetempo'),
            esc_html(gmdate('Ymd'))
        ) . '</li>';
        echo '</ol>';
        echo '</div>';
        
        echo '<div style="background: #fff3cd; padding: 15px; border-left: 4px solid #856404; margin-top: 15px;">';
        echo '<p><strong>⚠️ ' . esc_html__('Important recommendations:', 'comptaflow-by-meetempo') . '</strong></p>';
        echo '<ul>';
        echo '<li>' . esc_html__('Perform a backup before each month end', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Keep 3 copies on different media (Cloud + hard drive + USB)', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Regularly test that your backups are readable', 'comptaflow-by-meetempo') . '</li>';
        echo '</ul>';
        echo '</div>';
        
        echo '</div>';
    }
    
    private static function renderRestoreInstructions(array $tables): void
    {
        echo '<div style="background: white; padding: 20px; margin: 20px 0; border: 1px solid #ddd; border-radius: 5px;">';
        echo '<h2>📤 ' . esc_html__('Restore a Backup', 'comptaflow-by-meetempo') . '</h2>';
        
        // Giant warning
        echo '<div style="background: #dc3545; color: white; padding: 20px; margin: 20px 0; border-radius: 5px;">';
        echo '<h3 style="color: white; margin-top: 0;">⚠️ ' . esc_html__('WARNING - IRREVERSIBLE OPERATION', 'comptaflow-by-meetempo') . '</h3>';
        echo '<p><strong>' . esc_html__('Restoration will OVERWRITE ALL your current accounting data.', 'comptaflow-by-meetempo') . '</strong></p>';
        echo '<ul style="color: white;">';
        echo '<li>❌ ' . esc_html__('All current entries will be PERMANENTLY lost', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>❌ ' . esc_html__('This action CANNOT be undone', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>✅ ' . esc_html__('Create a backup BEFORE restoring', 'comptaflow-by-meetempo') . '</li>';
        echo '</ul>';
        echo '</div>';
        
        echo '<div style="background: #e7f3ff; padding: 20px; border-left: 4px solid #0073aa; margin: 20px 0;">';
        echo '<h3>' . esc_html__('Steps to follow:', 'comptaflow-by-meetempo') . '</h3>';
        echo '<ol style="line-height: 1.8;">';
        echo '<li><strong style="color: #dc3545;">' . esc_html__('FIRST MAKE A BACKUP of your current state (see above)', 'comptaflow-by-meetempo') . '</strong></li>';
        echo '<li>' . esc_html__('Connect to phpMyAdmin', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Select your WordPress database', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Click on the "Import" tab', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Click "Choose a file"', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Select your backup .sql file', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Leave default options', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Click "Execute"', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Wait for import to complete (may take a few seconds)', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Check in ComptaFlow that your data is properly restored', 'comptaflow-by-meetempo') . '</li>';
        echo '</ol>';
        echo '</div>';
        
        echo '</div>';
    }
    
    private static function renderFactoryReset(): void
    {
        echo '<div style="background: white; padding: 20px; margin: 20px 0; border: 1px solid #ddd; border-radius: 5px;">';
        echo '<h2>🔄 ' . esc_html__('Factory Reset', 'comptaflow-by-meetempo') . '</h2>';
        
        echo '<div style="background: #dc3545; color: white; padding: 20px; margin: 20px 0; border-radius: 5px;">';
        echo '<h3 style="color: white; margin-top: 0;">⚠️ ' . esc_html__('DANGER - TOTAL AND IRREVERSIBLE DATA LOSS', 'comptaflow-by-meetempo') . '</h3>';
        echo '<p><strong>' . esc_html__('To return to a fresh installation:', 'comptaflow-by-meetempo') . '</strong></p>';
        echo '<ol style="color: white;">';
        echo '<li>' . esc_html__('Go to Plugins → Installed Plugins', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Find ComptaFlow', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Click Deactivate', 'comptaflow-by-meetempo') . '</li>';
        echo '<li>' . esc_html__('Then click Delete', 'comptaflow-by-meetempo') . '</li>';
        echo '</ol>';
        echo '<p><strong style="font-size: 16px;">⚠️ ' . esc_html__('This action will PERMANENTLY delete all your accounting data.', 'comptaflow-by-meetempo') . '</strong></p>';
        echo '<p>' . esc_html__('No recovery possible. Make a backup FIRST.', 'comptaflow-by-meetempo') . '</p>';
        echo '</div>';
        
        echo '</div>';
    }
}
