<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class PC_Membership_Activator {

    public static function activate() {
        global $wpdb;

        $charset_collate = $wpdb->get_charset_collate();

        $table_plans = $wpdb->prefix . 'pc_membership_plans';
        $table_subscriptions = $wpdb->prefix . 'pc_membership_subscriptions';
        $table_payments = $wpdb->prefix . 'pc_membership_payments';
        $table_access_rules = $wpdb->prefix . 'pc_membership_access_rules';

        $sql = "CREATE TABLE $table_plans (
            id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
            name VARCHAR(255) NOT NULL,
            description TEXT NULL,
            price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
            is_subscription TINYINT(1) NOT NULL DEFAULT 0,
            billing_interval VARCHAR(20) NULL,
            trial_days INT(11) NOT NULL DEFAULT 0,
            benefits TEXT NULL,
            role VARCHAR(100) NULL,
            stripe_price_id VARCHAR(255) NULL,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY  (id),
            KEY name (name)
        ) $charset_collate;\n";

        $sql .= "CREATE TABLE $table_subscriptions (
            id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
            user_id BIGINT(20) UNSIGNED NOT NULL,
            plan_id BIGINT(20) UNSIGNED NOT NULL,
            stripe_customer_id VARCHAR(255) NULL,
            stripe_subscription_id VARCHAR(255) NULL,
            status VARCHAR(50) NOT NULL DEFAULT 'pending',
            started_at DATETIME NOT NULL,
            expires_at DATETIME NULL,
            cancelled_at DATETIME NULL,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY  (id),
            KEY user_id (user_id),
            KEY plan_id (plan_id),
            KEY status (status),
            KEY stripe_subscription_id (stripe_subscription_id)
        ) $charset_collate;\n";

        $sql .= "CREATE TABLE $table_payments (
            id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
            user_id BIGINT(20) UNSIGNED NOT NULL,
            subscription_id BIGINT(20) UNSIGNED NULL,
            plan_id BIGINT(20) UNSIGNED NOT NULL,
            stripe_payment_intent VARCHAR(255) NULL,
            stripe_invoice_id VARCHAR(255) NULL,
            amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
            currency VARCHAR(10) NOT NULL DEFAULT 'usd',
            status VARCHAR(50) NOT NULL DEFAULT 'pending',
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY  (id),
            KEY user_id (user_id),
            KEY subscription_id (subscription_id),
            KEY plan_id (plan_id)
        ) $charset_collate;\n";

        $sql .= "CREATE TABLE $table_access_rules (
            id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
            content_type VARCHAR(20) NOT NULL,
            content_id BIGINT(20) UNSIGNED NOT NULL,
            plan_ids TEXT NOT NULL,
            redirect_type VARCHAR(20) NOT NULL DEFAULT 'checkout',
            custom_url VARCHAR(500) NULL,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY  (id),
            KEY content_type (content_type),
            KEY content_id (content_id),
            UNIQUE KEY content_rule (content_type, content_id)
        ) $charset_collate;";

        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        dbDelta( $sql );

        self::create_default_plans();
        self::set_plugin_version();
    }

    private static function create_default_plans() {
        global $wpdb;

        $plans_count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}pc_membership_plans" );

        if ( $plans_count > 0 ) {
            return;
        }

        $wpdb->insert( $wpdb->prefix . 'pc_membership_plans', array(
            'name' => __( 'Basic', 'pc-membership-abc123' ),
            'description' => __( 'Get started with basic membership features.', 'pc-membership-abc123' ),
            'price' => 9.99,
            'is_subscription' => 1,
            'billing_interval' => 'month',
            'trial_days' => 0,
            'benefits' => __( "Access to basic content\nEmail support\nMonthly newsletter", 'pc-membership-abc123' ),
            'role' => 'subscriber',
        ) );

        $wpdb->insert( $wpdb->prefix . 'pc_membership_plans', array(
            'name' => __( 'Premium', 'pc-membership-abc123' ),
            'description' => __( 'Unlock all premium features and content.', 'pc-membership-abc123' ),
            'price' => 29.99,
            'is_subscription' => 1,
            'billing_interval' => 'month',
            'trial_days' => 14,
            'benefits' => __( "Access to all content\nPriority support\nExclusive webinars\nDownload resources\n24/7 chat support", 'pc-membership-abc123' ),
            'role' => 'pc_member_1',
        ) );
    }

    private static function set_plugin_version() {
        update_option( 'pc_membership_version', PC_MEMBERSHIP_VERSION );
    }
}
