<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Create default plan features for legacy functionality
        $this->createDefaultPlanFeatures();

        // Migrate existing plans to new system
        $this->migrateExistingPlans();

        // Update legacy plan structure
        $this->updateLegacyPlanStructure();
    }

    /**
     * Create default plan features that match legacy functionality
     */
    private function createDefaultPlanFeatures(): void
    {
        $features = [
            [
                'name' => 'mailbox_accounts',
                'display_name' => 'Mailbox Accounts',
                'description' => 'Number of mailbox accounts allowed',
                'category' => 'core',
                'type' => 'numeric',
                'is_active' => true,
                'sort_order' => 1,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => 'advanced_filters',
                'display_name' => 'Advanced Filters',
                'description' => 'Advanced email filtering and rules',
                'category' => 'advanced',
                'type' => 'boolean',
                'is_active' => true,
                'sort_order' => 10,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => 'api_access',
                'display_name' => 'API Access',
                'description' => 'Access to developer API',
                'category' => 'advanced',
                'type' => 'boolean',
                'is_active' => true,
                'sort_order' => 11,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => 'priority_support',
                'display_name' => 'Priority Support',
                'description' => 'Priority customer support',
                'category' => 'premium',
                'type' => 'boolean',
                'is_active' => true,
                'sort_order' => 20,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ];

        // Use insertOrIgnore to avoid conflicts with existing features
        foreach ($features as $feature) {
            DB::table('plan_features')->insertOrIgnore([$feature]);
        }

        // Ensure email_forwarding exists with correct sort order
        DB::table('plan_features')
            ->where('name', 'email_forwarding')
            ->update([
                'sort_order' => 2,
                'updated_at' => now(),
            ]);
    }

    /**
     * Migrate existing plans to use the new feature limits system
     */
    private function migrateExistingPlans(): void
    {
        $plans = DB::table('plans')->get();

        foreach ($plans as $plan) {
            // Get feature IDs for mapping
            $mailboxFeature = DB::table('plan_features')->where('name', 'mailbox_accounts')->first();
            $emailForwardingFeature = DB::table('plan_features')->where('name', 'email_forwarding')->first();
            $advancedFiltersFeature = DB::table('plan_features')->where('name', 'advanced_filters')->first();
            $apiAccessFeature = DB::table('plan_features')->where('name', 'api_access')->first();
            $prioritySupportFeature = DB::table('plan_features')->where('name', 'priority_support')->first();

            // Create feature limits for each plan (only if they don't exist)
            $existingLimitIds = DB::table('plan_feature_limits')
                ->where('plan_id', $plan->id)
                ->pluck('plan_feature_id')
                ->toArray();

            $featureLimits = [];

            // Mailbox limit (from legacy mailbox_limit) - only if not already exists
            if ($mailboxFeature && ! in_array($mailboxFeature->id, $existingLimitIds)) {
                $featureLimits[] = [
                    'plan_id' => $plan->id,
                    'plan_feature_id' => $mailboxFeature->id,
                    'limit_type' => 'total',
                    'limit_value' => $plan->mailbox_limit,
                    'is_enabled' => true,
                    'applies_during_trial' => true,
                    'created_at' => now(),
                    'updated_at' => now(),
                ];
            }

            // Email forwarding (enabled for all plans) - only if not already exists
            if ($emailForwardingFeature && ! in_array($emailForwardingFeature->id, $existingLimitIds)) {
                $featureLimits[] = [
                    'plan_id' => $plan->id,
                    'plan_feature_id' => $emailForwardingFeature->id,
                    'limit_type' => 'boolean',
                    'limit_value' => 1, // true
                    'is_enabled' => true,
                    'applies_during_trial' => true,
                    'created_at' => now(),
                    'updated_at' => now(),
                ];
            }

            // Advanced features based on plan tier/price
            $isPremiumPlan = $plan->price >= 20;

            if ($advancedFiltersFeature && ! in_array($advancedFiltersFeature->id, $existingLimitIds)) {
                $featureLimits[] = [
                    'plan_id' => $plan->id,
                    'plan_feature_id' => $advancedFiltersFeature->id,
                    'limit_type' => 'boolean',
                    'limit_value' => $isPremiumPlan ? 1 : 0,
                    'is_enabled' => true,
                    'applies_during_trial' => false, // Not available during trial for advanced features
                    'created_at' => now(),
                    'updated_at' => now(),
                ];
            }

            if ($apiAccessFeature && ! in_array($apiAccessFeature->id, $existingLimitIds)) {
                $featureLimits[] = [
                    'plan_id' => $plan->id,
                    'plan_feature_id' => $apiAccessFeature->id,
                    'limit_type' => 'boolean',
                    'limit_value' => $isPremiumPlan ? 1 : 0,
                    'is_enabled' => true,
                    'applies_during_trial' => false,
                    'created_at' => now(),
                    'updated_at' => now(),
                ];
            }

            if ($prioritySupportFeature && ! in_array($prioritySupportFeature->id, $existingLimitIds)) {
                $featureLimits[] = [
                    'plan_id' => $plan->id,
                    'plan_feature_id' => $prioritySupportFeature->id,
                    'limit_type' => 'boolean',
                    'limit_value' => $plan->price >= 25 ? 1 : 0,
                    'is_enabled' => true,
                    'applies_during_trial' => false,
                    'created_at' => now(),
                    'updated_at' => now(),
                ];
            }

            // Insert only new feature limits
            if (! empty($featureLimits)) {
                DB::table('plan_feature_limits')->insert($featureLimits);
            }

            // Create plan provider entries based on legacy accept_* columns
            $this->createPlanProviders($plan);

            // Create trial configuration (disabled for legacy plans)
            $this->createTrialConfiguration($plan);
        }
    }

    /**
     * Create plan provider entries based on legacy provider acceptance columns
     */
    private function createPlanProviders($plan): void
    {
        // Check if providers already exist for this plan
        $existingProviders = DB::table('plan_providers')
            ->where('plan_id', $plan->id)
            ->exists();

        if ($existingProviders) {
            return; // Skip if providers already exist
        }

        $providers = [];

        // Stripe
        if ($plan->accept_stripe) {
            $providers[] = [
                'plan_id' => $plan->id,
                'provider' => 'stripe',
                'provider_price_id' => $plan->pricing_id,
                'price' => $plan->price,
                'currency' => 'USD',
                'is_enabled' => true,
                'sort_order' => 1,
                'created_at' => now(),
                'updated_at' => now(),
            ];
        }

        // OxaPay
        if ($plan->accept_oxapay && $plan->oxapay_link) {
            $providers[] = [
                'plan_id' => $plan->id,
                'provider' => 'oxapay',
                'provider_price_id' => $plan->oxapay_link,
                'price' => $plan->price,
                'currency' => 'USD',
                'is_enabled' => true,
                'sort_order' => 5,
                'created_at' => now(),
                'updated_at' => now(),
            ];
        }

        // Shoppy (if it exists)
        if ($plan->accept_shoppy && $plan->shoppy_product_id) {
            $providers[] = [
                'plan_id' => $plan->id,
                'provider' => 'shoppy',
                'provider_price_id' => $plan->shoppy_product_id,
                'price' => $plan->price,
                'currency' => 'USD',
                'is_enabled' => true,
                'sort_order' => 3,
                'created_at' => now(),
                'updated_at' => now(),
            ];
        }

        // If no providers were explicitly enabled, enable Stripe by default
        if (empty($providers) && $plan->pricing_id) {
            $providers[] = [
                'plan_id' => $plan->id,
                'provider' => 'stripe',
                'provider_price_id' => $plan->pricing_id,
                'price' => $plan->price,
                'currency' => 'USD',
                'is_enabled' => true,
                'sort_order' => 1,
                'created_at' => now(),
                'updated_at' => now(),
            ];
        }

        if (! empty($providers)) {
            DB::table('plan_providers')->insert($providers);
        }
    }

    /**
     * Create trial configuration for legacy plans (disabled by default)
     */
    private function createTrialConfiguration($plan): void
    {
        // Check if trial configuration already exists
        $existingTrial = DB::table('trial_configurations')
            ->where('plan_id', $plan->id)
            ->exists();

        if ($existingTrial) {
            return; // Skip if trial config already exists
        }

        DB::table('trial_configurations')->insert([
            'plan_id' => $plan->id,
            'trial_enabled' => false, // Disabled for legacy plans
            'trial_duration_days' => 14,
            'trial_requires_payment_method' => true,
            'trial_auto_converts' => true,
            'trial_extension_limit' => 0,
            'trial_conversion_action' => 'upgrade_to_paid',
            'trial_welcome_message' => null,
            'trial_expiry_message' => null,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    /**
     * Update legacy plan structure - mark old columns as deprecated but keep them for rollback
     */
    private function updateLegacyPlanStructure(): void
    {
        // Note: We're keeping the legacy columns for now to ensure rollback capability
        // In a future migration, we can remove these columns:
        // - mailbox_limit (replaced by plan_feature_limits)
        // - monthly_billing (replaced by billing_cycle_days)
        // - accept_stripe, accept_shoppy, accept_oxapay (replaced by plan_providers)
        // - shoppy_product_id, oxapay_link (moved to plan_providers)

        // For now, just ensure billing_cycle_days is properly set based on monthly_billing
        DB::statement('
            UPDATE plans
            SET billing_cycle_days = CASE
                WHEN monthly_billing = 1 THEN 30
                ELSE 365
            END
            WHERE billing_cycle_days IS NULL OR billing_cycle_days = 0
        ');
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Remove created plan features (except email_forwarding which existed before)
        DB::table('plan_features')->whereIn('name', [
            'mailbox_accounts',
            'advanced_filters',
            'api_access',
            'priority_support',
        ])->delete();

        // Remove feature limits, providers, and trial configurations for migrated plans
        $planIds = DB::table('plans')->pluck('id');

        DB::table('plan_feature_limits')->whereIn('plan_id', $planIds)->delete();
        DB::table('plan_providers')->whereIn('plan_id', $planIds)->delete();
        DB::table('trial_configurations')->whereIn('plan_id', $planIds)->delete();

        // Restore mailbox_limit from feature limits if possible
        $mailboxFeature = DB::table('plan_features')->where('name', 'mailbox_accounts')->first();
        if ($mailboxFeature) {
            $limits = DB::table('plan_feature_limits')
                ->where('plan_feature_id', $mailboxFeature->id)
                ->get();

            foreach ($limits as $limit) {
                DB::table('plans')
                    ->where('id', $limit->plan_id)
                    ->update(['mailbox_limit' => $limit->limit_value]);
            }
        }
    }
};
