<?php
namespace WooNooW\Core;

/**
 * Plugin Installer
 * Handles plugin activation tasks
 */
class Installer {
    
    /**
     * Run on plugin activation
     */
    public static function activate() {
        // Create WooNooW pages
        self::create_pages();
        
        // Set WooCommerce to use HPOS
        update_option('woocommerce_custom_orders_table_enabled', 'yes');
        update_option('woocommerce_custom_orders_table_migration_enabled', 'yes');
        
        // Flush rewrite rules
        flush_rewrite_rules();
    }
    
    /**
     * Create WooNooW SPA page only
     * No longer modifies WooCommerce pages - we use template overrides instead
     */
    private static function create_pages() {
        // Only create the main SPA page (Store)
        // WooCommerce pages are NOT modified - we use template overrides
        
        $spa_page_id = get_option('woonoow_store_page_id');
        
        // Check if SPA page already exists
        if ($spa_page_id && get_post($spa_page_id)) {
            return; // Already exists
        }
        
        // Search for existing "Store" page
        $existing_page = get_page_by_title('Store', OBJECT, 'page');
        if ($existing_page) {
            $spa_page_id = $existing_page->ID;
            
            // Update with SPA shortcode if needed
            if (!has_shortcode($existing_page->post_content, 'woonoow_spa')) {
                update_post_meta($spa_page_id, '_woonoow_original_content', $existing_page->post_content);
                wp_update_post([
                    'ID'           => $spa_page_id,
                    'post_content' => '[woonoow_spa]',
                ]);
            }
        } else {
            // Create new SPA page
            $spa_page_id = wp_insert_post([
                'post_title'     => 'Store',
                'post_content'   => '[woonoow_spa]',
                'post_status'    => 'publish',
                'post_type'      => 'page',
                'post_author'    => get_current_user_id() ?: 1,
                'comment_status' => 'closed',
            ]);
        }
        
        // Save SPA page ID
        if ($spa_page_id && !is_wp_error($spa_page_id)) {
            update_option('woonoow_store_page_id', $spa_page_id);
            
            // Also set it in appearance settings
            $appearance_settings = get_option('woonoow_appearance_settings', []);
            if (!isset($appearance_settings['general'])) {
                $appearance_settings['general'] = [];
            }
            $appearance_settings['general']['spa_page'] = $spa_page_id;
            update_option('woonoow_appearance_settings', $appearance_settings);
        }
    }
    
    /**
     * Run on plugin deactivation
     */
    public static function deactivate() {
        // Restore original page content
        self::restore_original_content();
        
        // Flush rewrite rules
        flush_rewrite_rules();
        
        // Note: We don't delete pages on deactivation
        // Users might have content on those pages
    }
    
    /**
     * Restore original content to pages that were modified
     */
    private static function restore_original_content() {
        $page_keys = ['shop', 'cart', 'checkout', 'account'];
        
        foreach ($page_keys as $key) {
            $page_id = get_option('woonoow_' . $key . '_page_id');
            
            if ($page_id) {
                $original_content = get_post_meta($page_id, '_woonoow_original_content', true);
                
                if ($original_content) {
                    // Restore original content
                    wp_update_post([
                        'ID'           => $page_id,
                        'post_content' => $original_content,
                    ]);
                    
                    // Remove backup
                    delete_post_meta($page_id, '_woonoow_original_content');
                    
                }
            }
        }
    }
    
    /**
     * Run on plugin uninstall
     */
    public static function uninstall() {
        // Only delete if user explicitly wants to remove all data
        if (get_option('woonoow_remove_data_on_uninstall', false)) {
            self::delete_pages();
            self::delete_options();
        }
    }
    
    /**
     * Delete WooNooW pages
     */
    private static function delete_pages() {
        $page_keys = ['shop', 'cart', 'checkout', 'account'];
        
        foreach ($page_keys as $key) {
            $page_id = get_option('woonoow_' . $key . '_page_id');
            
            if ($page_id) {
                wp_delete_post($page_id, true); // Force delete
                delete_option('woonoow_' . $key . '_page_id');
            }
        }
    }
    
    /**
     * Delete WooNooW options
     */
    private static function delete_options() {
        global $wpdb;
        
        // Delete all options starting with 'woonoow_'
        $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE 'woonoow_%'");
    }
}
