<?php
namespace Atlasly\Admin\Pages;

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

use Exception;

class SetupWizard {
    
    public static function render() {
        $step = isset($_GET['step']) ? sanitize_key(wp_unslash($_GET['step'])) : 'welcome';
        
        echo '<div class="wrap">';
        echo '<h1>Atlasly Setup Wizard</h1>';
        echo '<div id="atlasly-setup-wizard" data-step="' . esc_attr($step) . '"></div>';
        echo '</div>';
    }
    
    public static function handle_setup() {
        if(!current_user_can('manage_options')) {
            wp_send_json_error('Unauthorized');
            return;
        }
        
        // Verify nonce
        $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
        if(!wp_verify_nonce($nonce, 'atlasly_setup')) {
            wp_send_json_error('Invalid nonce');
            return;
        }
        
        $setup_type = isset($_POST['setup_type']) ? sanitize_text_field(wp_unslash($_POST['setup_type'])) : '';
        $project_name = isset($_POST['project_name']) ? sanitize_text_field(wp_unslash($_POST['project_name'])) : '';
        
        if (empty($setup_type) || empty($project_name)) {
            wp_send_json_error('Missing required fields');
            return;
        }
        
        try {
            switch($setup_type) {
                case 'nextjs_forms':
                    self::setup_nextjs_forms($project_name);
                    break;
                case 'headless_cms':
                    self::setup_headless_cms($project_name);
                    break;
                case 'api_backend':
                    self::setup_api_backend($project_name);
                    break;
                case 'blank':
                    // No default content types for blank project
                    break;
                default:
                    wp_send_json_error('Invalid setup type');
                    return;
            }
            
            update_option('atlasly_setup_completed', true);
            update_option('atlasly_setup_type', $setup_type);
            update_option('atlasly_project_name', $project_name);
            
            wp_send_json_success(['redirect' => admin_url('admin.php?page=atlasly')]);
            
        } catch(Exception $e) {
            error_log('Atlasly Setup Error: ' . $e->getMessage());
            wp_send_json_error('Setup failed: ' . $e->getMessage());
        }
    }
    
    public static function handle_reset() {
        if(!current_user_can('manage_options')) {
            wp_send_json_error('Unauthorized');
            return;
        }
        
        // Verify nonce
        $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
        if(!wp_verify_nonce($nonce, 'atlasly_setup')) {
            wp_send_json_error('Invalid nonce');
            return;
        }
        
        try {
            global $wpdb;
            
            // Delete all content types and entries
            $content_types_table = $wpdb->prefix . 'atlasly_content_types';
            $entries_table = $wpdb->prefix . 'atlasly_entries';
            
            if ($wpdb->get_var("SHOW TABLES LIKE '$content_types_table'") == $content_types_table) {
                $wpdb->query("DELETE FROM $content_types_table");
            }
            
            if ($wpdb->get_var("SHOW TABLES LIKE '$entries_table'") == $entries_table) {
                $wpdb->query("DELETE FROM $entries_table");
            }
            
            // Reset setup options
            delete_option('atlasly_setup_completed');
            delete_option('atlasly_setup_type');
            delete_option('atlasly_project_name');
            
            // Clear cache if available
            if (class_exists('\Atlasly\Core\Cache')) {
                \Atlasly\Core\Cache::flush();
            }
            
            wp_send_json_success(['redirect' => admin_url('admin.php?page=atlasly-setup')]);
            
        } catch(Exception $e) {
            error_log('Atlasly Reset Error: ' . $e->getMessage());
            wp_send_json_error('Reset failed: ' . $e->getMessage());
        }
    }
    
    private static function setup_nextjs_forms($project_name) {
        // Ensure table exists
        global $wpdb;
        $table = $wpdb->prefix . 'atlasly_content_types';
        
        if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table) {
            \Atlasly\Core\Installer::install();
        }
        
        // Start with blank content types - users can create their own
    }
    
    private static function setup_headless_cms($project_name) {
        // Ensure table exists
        global $wpdb;
        $table = $wpdb->prefix . 'atlasly_content_types';
        
        if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table) {
            \Atlasly\Core\Installer::install();
        }
        
        // Start with blank content types - users can create their own
    }
    
    private static function setup_api_backend($project_name) {
        // Ensure table exists
        global $wpdb;
        $table = $wpdb->prefix . 'atlasly_content_types';
        
        if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table) {
            \Atlasly\Core\Installer::install();
        }
        
        // Start with blank content types - users can create their own
    }
}
