<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('manual_payment_methods')) {
            Schema::create('manual_payment_methods', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->string('type');
                $table->text('instructions')->nullable();
                $table->string('account_name')->nullable();
                $table->string('account_identifier')->nullable();
                $table->string('logo_path')->nullable();
                $table->boolean('is_active')->default(true);
                $table->unsignedSmallInteger('sort_order')->default(0);
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('events')) {
            Schema::create('events', function (Blueprint $table) {
                $table->id();
                $table->uuid('public_id')->unique();
                $table->foreignId('game_id')->constrained()->cascadeOnDelete();
                $table->foreignId('created_by')->constrained('users')->cascadeOnDelete();
                $table->string('title');
                $table->string('slug')->unique();
                $table->string('short_description')->nullable();
                $table->text('description')->nullable();
                $table->string('cover_image')->nullable();
                $table->string('event_type');
                $table->string('participation_type')->default('individual');
                $table->string('location_type')->default('online');
                $table->string('venue_name')->nullable();
                $table->text('venue_address')->nullable();
                $table->text('online_details')->nullable();
                $table->string('timezone')->default('UTC');
                $table->timestamp('registration_opens_at')->nullable();
                $table->timestamp('registration_closes_at')->nullable();
                $table->timestamp('starts_at');
                $table->timestamp('ends_at');
                $table->unsignedInteger('capacity')->nullable();
                $table->boolean('waitlist_enabled')->default(false);
                $table->string('approval_mode')->default('automatic');
                $table->string('pricing_type')->default('free');
                $table->unsignedBigInteger('fee_amount_minor')->nullable();
                $table->string('currency', 3)->nullable();
                $table->text('payment_instructions')->nullable();
                $table->text('rules')->nullable();
                $table->text('prizes')->nullable();
                $table->text('contact_information')->nullable();
                $table->string('status')->default('draft');
                $table->timestamp('published_at')->nullable();
                $table->timestamps();
                $table->softDeletes();

                $table->index(['game_id', 'status', 'starts_at'], 'events_game_status_starts_idx');
            });
        }

        if (! Schema::hasTable('event_translations')) {
            Schema::create('event_translations', function (Blueprint $table) {
                $table->id();
                $table->foreignId('event_id')->constrained()->cascadeOnDelete();
                $table->string('locale', 5);
                $table->string('title');
                $table->text('description')->nullable();
                $table->timestamps();

                $table->unique(['event_id', 'locale']);
            });
        }

        if (! Schema::hasTable('event_rules')) {
            Schema::create('event_rules', function (Blueprint $table) {
                $table->id();
                $table->foreignId('event_id')->constrained()->cascadeOnDelete();
                $table->string('title');
                $table->text('content');
                $table->unsignedSmallInteger('sort_order')->default(0);
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('event_prizes')) {
            Schema::create('event_prizes', function (Blueprint $table) {
                $table->id();
                $table->foreignId('event_id')->constrained()->cascadeOnDelete();
                $table->string('placement');
                $table->string('amount')->nullable();
                $table->text('description')->nullable();
                $table->unsignedSmallInteger('sort_order')->default(0);
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('event_payment_methods')) {
            Schema::create('event_payment_methods', function (Blueprint $table) {
                $table->id();
                $table->foreignId('event_id')->constrained()->cascadeOnDelete();
                $table->foreignId('manual_payment_method_id')->constrained()->cascadeOnDelete();
                $table->text('instructions_override')->nullable();
                $table->boolean('is_active')->default(true);
                $table->timestamps();

                $table->unique(['event_id', 'manual_payment_method_id'], 'epm_event_method_unique');
            });
        }

        if (! Schema::hasTable('event_registrations')) {
            Schema::create('event_registrations', function (Blueprint $table) {
                $table->id();
                $table->uuid('public_id')->unique();
                $table->foreignId('event_id')->constrained()->cascadeOnDelete();
                $table->foreignId('user_id')->constrained()->cascadeOnDelete();
                $table->foreignId('player_profile_id')->constrained()->cascadeOnDelete();
                $table->foreignId('player_game_profile_id')->constrained()->cascadeOnDelete();
                $table->string('status');
                $table->string('payment_status');
                $table->string('contact_name');
                $table->string('contact_email');
                $table->string('contact_phone_e164');
                $table->boolean('contact_whatsapp')->default(false);
                $table->text('admin_notes')->nullable();
                $table->text('user_notes')->nullable();
                $table->timestamp('registered_at');
                $table->timestamp('confirmed_at')->nullable();
                $table->timestamp('cancelled_at')->nullable();
                $table->foreignId('reviewed_by')->nullable()->constrained('users')->nullOnDelete();
                $table->timestamp('reviewed_at')->nullable();
                $table->unsignedInteger('waitlist_position')->nullable();
                $table->timestamps();

                $table->index(['event_id', 'status'], 'evt_reg_event_status_idx');
                $table->index(['event_id', 'user_id'], 'evt_reg_event_user_idx');
                $table->index('payment_status', 'evt_reg_payment_status_idx');
            });
        }

        if (! Schema::hasTable('event_registration_status_events')) {
            Schema::create('event_registration_status_events', function (Blueprint $table) {
                $table->id();
                $table->foreignId('event_registration_id')->constrained()->cascadeOnDelete();
                $table->string('old_status')->nullable();
                $table->string('new_status');
                $table->foreignId('changed_by')->nullable()->constrained('users')->nullOnDelete();
                $table->text('reason')->nullable();
                $table->timestamps();

                $table->index(['event_registration_id', 'created_at'], 'evt_reg_status_hist_idx');
            });
        }

        if (! Schema::hasTable('event_payment_proofs')) {
            Schema::create('event_payment_proofs', function (Blueprint $table) {
                $table->id();
                $table->uuid('public_id')->unique();
                $table->foreignId('event_registration_id')->constrained()->cascadeOnDelete();
                $table->foreignId('manual_payment_method_id')->constrained()->cascadeOnDelete();
                $table->string('file_path');
                $table->string('original_name');
                $table->string('mime_type');
                $table->unsignedBigInteger('size');
                $table->string('transfer_reference')->nullable();
                $table->unsignedBigInteger('amount_minor')->nullable();
                $table->text('user_notes')->nullable();
                $table->string('review_status')->default('pending');
                $table->foreignId('reviewed_by')->nullable()->constrained('users')->nullOnDelete();
                $table->timestamp('reviewed_at')->nullable();
                $table->text('rejection_reason')->nullable();
                $table->timestamps();

                $table->index('review_status', 'evt_payment_proofs_review_idx');
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('event_payment_proofs');
        Schema::dropIfExists('event_registration_status_events');
        Schema::dropIfExists('event_registrations');
        Schema::dropIfExists('event_payment_methods');
        Schema::dropIfExists('event_prizes');
        Schema::dropIfExists('event_rules');
        Schema::dropIfExists('event_translations');
        Schema::dropIfExists('events');
        Schema::dropIfExists('manual_payment_methods');
    }
};
