<?php

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

return new class extends Migration
{
    public function up(): void
    {
        $now = now();

        // ── 1. Delete old plans that are being replaced ────────────────────
        // Remove 'pro' — it will be replaced by 'professional' and 'business'
        DB::table('subscription_plans')->where('name', 'pro')->delete();

        // ── 2. Update STARTER plan ─────────────────────────────────────────
        DB::table('subscription_plans')
            ->where('name', 'starter')
            ->update([
                'label'         => 'Starter',
                'description'   => 'Perfect for individuals and small teams just getting started.',
                'monthly_price' => 5000,
                'yearly_price'  => 50000,
                'features'      => json_encode([
                    'max_users'      => 3,
                    'max_products'   => 100,
                    'max_branches'   => 1,
                    'reports_access' => false,
                    'api_access'     => false,
                    'multi_branch'   => false,
                    'stock_transfer' => false,
                    'audit_trail'    => false,
                ]),
                'is_active'   => true,
                'sort_order'  => 1,
                'trial_days'  => 0,
                'is_featured' => false,
                'updated_at'  => $now,
            ]);

        // ── 3. Insert PROFESSIONAL plan ────────────────────────────────────
        DB::table('subscription_plans')->insertOrIgnore([
            'name'          => 'professional',
            'label'         => 'Professional',
            'description'   => 'For growing businesses that need advanced tracking and audit visibility.',
            'monthly_price' => 11000,
            'yearly_price'  => 110000,
            'features'      => json_encode([
                'max_users'      => 5,
                'max_products'   => 1000,
                'max_branches'   => 1,
                'reports_access' => true,
                'api_access'     => false,
                'multi_branch'   => false,
                'stock_transfer' => false,
                'audit_trail'    => true,
            ]),
            'is_active'   => true,
            'sort_order'  => 2,
            'trial_days'  => 0,
            'is_featured' => true,
            'created_at'  => $now,
            'updated_at'  => $now,
        ]);

        // ── 4. Insert BUSINESS plan ────────────────────────────────────────
        DB::table('subscription_plans')->insertOrIgnore([
            'name'          => 'business',
            'label'         => 'Business',
            'description'   => 'Multi-location businesses with stock transfers and full audit trail.',
            'monthly_price' => 20000,
            'yearly_price'  => 200000,
            'features'      => json_encode([
                'max_users'      => 20,
                'max_products'   => 2000,
                'max_branches'   => 2,
                'reports_access' => true,
                'api_access'     => false,
                'multi_branch'   => true,
                'stock_transfer' => true,
                'audit_trail'    => true,
            ]),
            'is_active'   => true,
            'sort_order'  => 3,
            'trial_days'  => 0,
            'is_featured' => false,
            'created_at'  => $now,
            'updated_at'  => $now,
        ]);

        // ── 5. Update ENTERPRISE plan ──────────────────────────────────────
        DB::table('subscription_plans')
            ->where('name', 'enterprise')
            ->update([
                'label'         => 'Enterprise',
                'description'   => 'Unlimited scale. Every feature. Priority support.',
                'monthly_price' => 40000,
                'yearly_price'  => 400000,
                'features'      => json_encode([
                    'max_users'      => -1,
                    'max_products'   => -1,
                    'max_branches'   => -1,
                    'reports_access' => true,
                    'api_access'     => true,
                    'multi_branch'   => true,
                    'stock_transfer' => true,
                    'audit_trail'    => true,
                ]),
                'is_active'   => true,
                'sort_order'  => 4,
                'is_featured' => false,
                'updated_at'  => $now,
            ]);

        // ── 6. Update FREE plan defaults ───────────────────────────────────
        DB::table('subscription_plans')
            ->where('name', 'free')
            ->update([
                'features'   => json_encode([
                    'max_users'      => 2,
                    'max_products'   => 50,
                    'max_branches'   => 1,
                    'reports_access' => false,
                    'api_access'     => false,
                    'multi_branch'   => false,
                    'stock_transfer' => false,
                    'audit_trail'    => false,
                ]),
                'updated_at' => $now,
            ]);

        // ── 7. Migrate existing subscriptions from old plan names ──────────
        // Any subscription that was on 'pro' maps to 'business' (closest match)
        DB::table('subscriptions')->where('plan', 'pro')->update(['plan' => 'business']);
    }

    public function down(): void
    {
        // Restore old 'pro' plan and revert other changes is complex — truncate and
        // rely on re-running the original seeder migration if a rollback is needed.
        DB::table('subscription_plans')
            ->whereIn('name', ['professional', 'business'])
            ->delete();

        DB::table('subscriptions')->where('plan', 'business')->update(['plan' => 'pro']);
    }
};
