<?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 or update WooNooW pages
     * Smart detection: reuses existing WooCommerce pages if they exist
     */
    private static function create_pages() {
        $pages = [
            'shop' => [
                'title'     => 'Shop',
                'content'   => '[woonoow_shop]',
                'wc_option' => 'woocommerce_shop_page_id',
            ],
            'cart' => [
                'title'     => 'Cart',
                'content'   => '[woonoow_cart]',
                'wc_option' => 'woocommerce_cart_page_id',
            ],
            'checkout' => [
                'title'     => 'Checkout',
                'content'   => '[woonoow_checkout]',
                'wc_option' => 'woocommerce_checkout_page_id',
            ],
            'account' => [
                'title'     => 'My Account',
                'content'   => '[woonoow_account]',
                'wc_option' => 'woocommerce_myaccount_page_id',
            ],
        ];
        
        foreach ($pages as $key => $page_data) {
            $page_id = null;
            
            // Strategy 1: Check if WooCommerce already has a page set
            if (isset($page_data['wc_option'])) {
                $wc_page_id = get_option($page_data['wc_option']);
                if ($wc_page_id && get_post($wc_page_id)) {
                    $page_id = $wc_page_id;
                    error_log("WooNooW: Found existing WooCommerce {$page_data['title']} page (ID: {$page_id})");
                }
            }
            
            // Strategy 2: Check if WooNooW already created a page
            if (!$page_id) {
                $woonoow_page_id = get_option('woonoow_' . $key . '_page_id');
                if ($woonoow_page_id && get_post($woonoow_page_id)) {
                    $page_id = $woonoow_page_id;
                    error_log("WooNooW: Found existing WooNooW {$page_data['title']} page (ID: {$page_id})");
                }
            }
            
            // Strategy 3: Search for page by title
            if (!$page_id) {
                $existing_page = get_page_by_title($page_data['title'], OBJECT, 'page');
                if ($existing_page) {
                    $page_id = $existing_page->ID;
                    error_log("WooNooW: Found existing {$page_data['title']} page by title (ID: {$page_id})");
                }
            }
            
            // If page exists, update its content with our shortcode
            if ($page_id) {
                $current_post = get_post($page_id);
                
                // Only update if it doesn't already have our shortcode
                if (!has_shortcode($current_post->post_content, 'woonoow_' . $key)) {
                    // Backup original content
                    update_post_meta($page_id, '_woonoow_original_content', $current_post->post_content);
                    
                    // Update with our shortcode
                    wp_update_post([
                        'ID'           => $page_id,
                        'post_content' => $page_data['content'],
                    ]);
                    
                    error_log("WooNooW: Updated {$page_data['title']} page with WooNooW shortcode");
                } else {
                    error_log("WooNooW: {$page_data['title']} page already has WooNooW shortcode");
                }
            } else {
                // No existing page found, create new one
                $page_id = wp_insert_post([
                    'post_title'     => $page_data['title'],
                    'post_content'   => $page_data['content'],
                    'post_status'    => 'publish',
                    'post_type'      => 'page',
                    'post_author'    => get_current_user_id(),
                    'comment_status' => 'closed',
                ]);
                
                if ($page_id && !is_wp_error($page_id)) {
                    error_log("WooNooW: Created new {$page_data['title']} page (ID: {$page_id})");
                }
            }
            
            // Store page ID and update WooCommerce settings
            if ($page_id && !is_wp_error($page_id)) {
                update_option('woonoow_' . $key . '_page_id', $page_id);
                
                if (isset($page_data['wc_option'])) {
                    update_option($page_data['wc_option'], $page_id);
                }
            }
        }
    }
    
    /**
     * 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');
                    
                    error_log("WooNooW: Restored original content for page ID: {$page_id}");
                }
            }
        }
    }
    
    /**
     * 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_%'");
    }
}
