<?php

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

class WPDD_Install {
    
    public static function activate() {
        // Load required files if not already loaded
        if (!class_exists('WPDD_Post_Types')) {
            require_once WPDD_PLUGIN_PATH . 'includes/class-wpdd-post-types.php';
        }
        if (!class_exists('WPDD_Roles')) {
            require_once WPDD_PLUGIN_PATH . 'includes/class-wpdd-roles.php';
        }
        
        // Register post types and taxonomies before flushing rules
        WPDD_Post_Types::register_post_types();
        WPDD_Post_Types::register_taxonomies();
        
        self::create_tables();
        self::create_pages();
        self::create_upload_protection();
        
        WPDD_Roles::create_roles();
        
        // Flush rewrite rules after post types are registered
        flush_rewrite_rules();
    }
    
    public static function deactivate() {
        flush_rewrite_rules();
    }
    
    private static function create_tables() {
        global $wpdb;
        
        $charset_collate = $wpdb->get_charset_collate();
        
        $sql = array();
        
        $sql[] = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wpdd_orders (
            id bigint(20) NOT NULL AUTO_INCREMENT,
            order_number varchar(50) NOT NULL,
            product_id bigint(20) NOT NULL,
            customer_id bigint(20) NOT NULL,
            creator_id bigint(20) NOT NULL,
            status varchar(20) NOT NULL DEFAULT 'pending',
            payment_method varchar(50) DEFAULT NULL,
            transaction_id varchar(100) DEFAULT NULL,
            amount decimal(10,2) NOT NULL,
            currency varchar(10) NOT NULL DEFAULT 'USD',
            customer_email varchar(100) NOT NULL,
            customer_name varchar(100) DEFAULT NULL,
            purchase_date datetime DEFAULT CURRENT_TIMESTAMP,
            download_count int(11) DEFAULT 0,
            notes text DEFAULT NULL,
            PRIMARY KEY (id),
            KEY order_number (order_number),
            KEY product_id (product_id),
            KEY customer_id (customer_id),
            KEY creator_id (creator_id),
            KEY status (status)
        ) $charset_collate;";
        
        $sql[] = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wpdd_downloads (
            id bigint(20) NOT NULL AUTO_INCREMENT,
            order_id bigint(20) NOT NULL,
            product_id bigint(20) NOT NULL,
            customer_id bigint(20) NOT NULL,
            file_id varchar(100) NOT NULL,
            download_date datetime DEFAULT CURRENT_TIMESTAMP,
            ip_address varchar(45) DEFAULT NULL,
            user_agent text DEFAULT NULL,
            PRIMARY KEY (id),
            KEY order_id (order_id),
            KEY product_id (product_id),
            KEY customer_id (customer_id)
        ) $charset_collate;";
        
        $sql[] = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wpdd_download_links (
            id bigint(20) NOT NULL AUTO_INCREMENT,
            order_id bigint(20) NOT NULL,
            token varchar(64) NOT NULL,
            expires_at datetime NOT NULL,
            download_count int(11) DEFAULT 0,
            max_downloads int(11) DEFAULT 5,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            UNIQUE KEY token (token),
            KEY order_id (order_id)
        ) $charset_collate;";
        
        $sql[] = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wpdd_creator_earnings (
            id bigint(20) NOT NULL AUTO_INCREMENT,
            creator_id bigint(20) NOT NULL,
            order_id bigint(20) NOT NULL,
            product_id bigint(20) NOT NULL,
            sale_amount decimal(10,2) NOT NULL,
            commission_rate decimal(5,2) NOT NULL,
            creator_earning decimal(10,2) NOT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY creator_id (creator_id),
            KEY order_id (order_id),
            KEY product_id (product_id)
        ) $charset_collate;";
        
        $sql[] = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wpdd_payouts (
            id bigint(20) NOT NULL AUTO_INCREMENT,
            creator_id bigint(20) NOT NULL,
            amount decimal(10,2) NOT NULL,
            currency varchar(10) NOT NULL,
            paypal_email varchar(100) NOT NULL,
            transaction_id varchar(100) DEFAULT NULL,
            status varchar(20) NOT NULL DEFAULT 'pending',
            payout_method varchar(20) NOT NULL DEFAULT 'manual',
            notes text DEFAULT NULL,
            processed_by bigint(20) DEFAULT NULL,
            processed_at datetime DEFAULT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY creator_id (creator_id),
            KEY status (status),
            KEY transaction_id (transaction_id)
        ) $charset_collate;";
        
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        
        foreach ($sql as $query) {
            dbDelta($query);
        }
        
        update_option('wpdd_db_version', WPDD_VERSION);
    }
    
    private static function create_pages() {
        $pages = array(
            'shop' => array(
                'title' => __('Shop', 'wp-digital-download'),
                'content' => '[wpdd_shop]',
                'option' => 'wpdd_shop_page_id'
            ),
            'my-purchases' => array(
                'title' => __('My Purchases', 'wp-digital-download'),
                'content' => '[wpdd_customer_purchases]',
                'option' => 'wpdd_purchases_page_id'
            ),
            'checkout' => array(
                'title' => __('Checkout', 'wp-digital-download'),
                'content' => '[wpdd_checkout]',
                'option' => 'wpdd_checkout_page_id'
            ),
            'thank-you' => array(
                'title' => __('Thank You', 'wp-digital-download'),
                'content' => '[wpdd_thank_you]',
                'option' => 'wpdd_thank_you_page_id'
            )
        );
        
        foreach ($pages as $slug => $page) {
            // Check if page already exists
            $existing_page_id = get_option($page['option']);
            if ($existing_page_id && get_post($existing_page_id)) {
                continue; // Page already exists, skip creation
            }
            
            // Check if a page with this slug already exists
            $existing_page = get_page_by_path($slug);
            if ($existing_page) {
                update_option($page['option'], $existing_page->ID);
                continue;
            }
            
            // Create the page
            $page_id = wp_insert_post(array(
                'post_title' => $page['title'],
                'post_content' => $page['content'],
                'post_status' => 'publish',
                'post_type' => 'page',
                'post_name' => $slug
            ));
            
            if ($page_id && !is_wp_error($page_id)) {
                update_option($page['option'], $page_id);
            }
        }
    }
    
    private static function create_upload_protection() {
        $upload_dir = wp_upload_dir();
        $protection_dir = trailingslashit($upload_dir['basedir']) . WPDD_UPLOADS_DIR;
        
        if (!file_exists($protection_dir)) {
            wp_mkdir_p($protection_dir);
        }
        
        $htaccess_content = "Options -Indexes\n";
        $htaccess_content .= "deny from all\n";
        
        $htaccess_file = trailingslashit($protection_dir) . '.htaccess';
        
        if (!file_exists($htaccess_file)) {
            file_put_contents($htaccess_file, $htaccess_content);
        }
        
        $index_content = "<?php\n// Silence is golden.\n";
        $index_file = trailingslashit($protection_dir) . 'index.php';
        
        if (!file_exists($index_file)) {
            file_put_contents($index_file, $index_content);
        }
    }
}