<?php

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

return new class extends Migration
{
    public function up(): void
    {
        // Packages table
        if (! Schema::hasTable('packages')) {
            Schema::create('packages', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->enum('type', ['endcustomer', 'reseller']);
                $table->decimal('price', 8, 2);
                $table->integer('max_photos')->nullable();
                $table->integer('max_guests')->nullable();
                $table->integer('gallery_days')->nullable();
                $table->integer('max_tasks')->nullable();
                $table->boolean('watermark_allowed')->default(true);
                $table->boolean('branding_allowed')->default(false);
                $table->integer('max_events_per_year')->nullable();
                $table->timestamp('expires_after')->nullable();
                $table->json('features')->nullable();
                $table->timestamps();
                $table->index(['type', 'price']);
            });

            // Seed standard packages if empty
            /*if (DB::table('packages')->count() == 0) {
                DB::table('packages')->insert([
                    [
                        'name' => 'Free/Test',
                        'type' => 'endcustomer',
                        'price' => 0.00,
                        'max_photos' => 30,
                        'max_guests' => 10,
                        'gallery_days' => 3,
                        'max_tasks' => 1,
                        'watermark_allowed' => true,
                        'branding_allowed' => false,
                        'max_events_per_year' => null,
                        'expires_after' => null,
                        'features' => json_encode([]),
                        'created_at' => now(),
                        'updated_at' => now(),
                    ],
                    [
                        'name' => 'Starter',
                        'type' => 'endcustomer',
                        'price' => 19.00,
                        'max_photos' => 300,
                        'max_guests' => 50,
                        'gallery_days' => 14,
                        'max_tasks' => 5,
                        'watermark_allowed' => true,
                        'branding_allowed' => false,
                        'max_events_per_year' => null,
                        'expires_after' => null,
                        'features' => json_encode([]),
                        'created_at' => now(),
                        'updated_at' => now(),
                    ],
                    [
                        'name' => 'Reseller S',
                        'type' => 'reseller',
                        'price' => 149.00,
                        'max_photos' => null,
                        'max_guests' => null,
                        'gallery_days' => null,
                        'max_tasks' => null,
                        'watermark_allowed' => true,
                        'branding_allowed' => true,
                        'max_events_per_year' => 5,
                        'expires_after' => now()->addYear(),
                        'features' => json_encode(['limited_branding']),
                        'created_at' => now(),
                        'updated_at' => now(),
                    ],
                    // Add more as needed
                ]);
            }*/
        }

        // Event Packages
        if (! Schema::hasTable('event_packages')) {
            Schema::create('event_packages', function (Blueprint $table) {
                $table->id();
                $table->foreignId('event_id')->constrained()->cascadeOnDelete();
                $table->foreignId('package_id')->constrained()->cascadeOnDelete();
                $table->decimal('purchased_price', 8, 2);
                $table->timestamp('purchased_at');
                $table->integer('used_photos')->default(0);
                $table->timestamps();
                $table->index('event_id');
            });
        }

        // Tenant Packages
        if (! Schema::hasTable('tenant_packages')) {
            Schema::create('tenant_packages', function (Blueprint $table) {
                $table->id();
                $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
                $table->foreignId('package_id')->constrained()->cascadeOnDelete();
                $table->decimal('price', 8, 2);
                $table->dateTime('purchased_at');
                $table->dateTime('expires_at')->nullable();
                $table->integer('used_events')->default(0);
                $table->boolean('active')->default(true);
                $table->timestamps();
                $table->index(['tenant_id', 'active']);
            });
        }

        // Package Purchases
        if (! Schema::hasTable('package_purchases')) {
            Schema::create('package_purchases', function (Blueprint $table) {
                $table->id();
                $table->foreignId('tenant_id')->nullable()->constrained();
                $table->foreignId('event_id')->nullable()->constrained();
                $table->foreignId('package_id')->constrained();
                $table->string('provider_id');
                $table->decimal('price', 8, 2);
                $table->dateTime('purchased_at');
                $table->enum('type', ['endcustomer_event', 'reseller_subscription']);
                $table->json('metadata')->nullable();
                $table->string('ip_address')->nullable();
                $table->string('user_agent')->nullable();
                $table->boolean('refunded')->default(false);
                $table->timestamps();
                $table->index(['tenant_id', 'created_at']);
            });
        }

        // Purchase History
        if (! Schema::hasTable('purchase_history')) {
            Schema::create('purchase_history', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->foreignId('tenant_id')->constrained('tenants');
                $table->unsignedBigInteger('package_id');
                $table->decimal('price', 10, 2)->default(0);
                $table->string('currency', 3)->default('EUR');
                $table->string('platform', 50);
                $table->string('transaction_id', 255)->nullable();
                $table->timestamp('purchased_at')->useCurrent();
                $table->timestamp('created_at')->useCurrent();
                $table->index('purchased_at');
                $table->index('transaction_id');
                $table->foreign('package_id')->references('id')->on('packages');
            });
        }

        // Add subscription fields to tenants if missing
        if (Schema::hasTable('tenants')) {
            if (! Schema::hasColumn('tenants', 'subscription_tier')) {
                Schema::table('tenants', function (Blueprint $table) {
                    $table->string('subscription_tier')->default('free')->after('email');
                });
            }
            if (! Schema::hasColumn('tenants', 'subscription_status')) {
                Schema::table('tenants', function (Blueprint $table) {
                    $table->enum('subscription_status', ['free', 'active', 'suspended', 'expired'])->default('free')->after('subscription_tier');
                });
            }
            if (! Schema::hasColumn('tenants', 'subscription_expires_at')) {
                Schema::table('tenants', function (Blueprint $table) {
                    $table->timestamp('subscription_expires_at')->nullable()->after('subscription_status');
                });
            }
            if (! Schema::hasColumn('tenants', 'total_revenue')) {
                Schema::table('tenants', function (Blueprint $table) {
                    $table->decimal('total_revenue', 10, 2)->default(0.00)->after('subscription_expires_at');
                });
            }
        }

        Schema::dropIfExists('event_credits_ledger');
        if (Schema::hasTable('purchase_history') && DB::table('package_purchases')->count() > 0) {
            Schema::dropIfExists('purchase_history');
        }

        // Drop old fields from tenants if new system is in place
        if (Schema::hasTable('tenants')) {
            $oldFields = ['event_credits_balance', 'free_event_granted_at'];
            foreach ($oldFields as $field) {
                if (Schema::hasColumn('tenants', $field) && DB::table('tenant_packages')->count() > 0) {
                    Schema::table('tenants', function (Blueprint $table) use ($field) {
                        $table->dropColumn($field);
                    });
                }
            }
        }
    }

    public function down(): void
    {
        if (app()->environment('local', 'testing')) {
            // Reverse drops and adds
            if (! Schema::hasTable('purchase_history')) {
                Schema::create('purchase_history', function (Blueprint $table) {
                    $table->id();
                    $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
                    $table->string('package_id', 255);
                    $table->decimal('price', 10, 2)->default(0);
                    $table->string('provider_id');
                    $table->timestamps();
                });
            }
            if (! Schema::hasTable('event_purchases')) {
                Schema::create('event_purchases', function (Blueprint $table) {
                    $table->id();
                    $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
                    $table->unsignedInteger('events_purchased')->default(1);
                    $table->decimal('amount', 10, 2);
                    $table->string('currency', 3)->default('EUR');
                    $table->string('provider', 32);
                    $table->string('external_receipt_id')->nullable();
                    $table->string('status', 16)->default('pending');
                    $table->timestamp('purchased_at')->nullable();
                    $table->timestamps();
                    $table->index(['tenant_id', 'purchased_at']);
                });
            }
            if (! Schema::hasTable('event_credits_ledger')) {
                Schema::create('event_credits_ledger', function (Blueprint $table) {
                    $table->id();
                    $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
                    $table->integer('delta');
                    $table->string('reason', 32);
                    $table->foreignId('related_purchase_id')->nullable()->constrained('event_purchases')->nullOnDelete();
                    $table->text('note')->nullable();
                    $table->timestamps();
                    $table->index(['tenant_id', 'created_at']);
                });
            }

            // Re-add old fields to tenants
            Schema::table('tenants', function (Blueprint $table) {
                if (! Schema::hasColumn('tenants', 'event_credits_balance')) {
                    $table->integer('event_credits_balance')->default(1);
                }
                if (! Schema::hasColumn('tenants', 'free_event_granted_at')) {
                    $table->timestamp('free_event_granted_at')->nullable();
                }
            });

            // Drop new tables
            Schema::dropIfExists('package_purchases');
            Schema::dropIfExists('tenant_packages');
            Schema::dropIfExists('event_packages');
            Schema::dropIfExists('packages');
        }
    }
};
