<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // 1. Eliminar la tabla tickets ya que ahora usamos llaves y cofres
        Schema::dropIfExists('tickets');

        // 2. Crear tabla chest_prizes (Premios del cofre de cada sorteo)
        Schema::create('chest_prizes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('raffle_id')->constrained('raffles')->onDelete('cascade');
            $table->string('name');
            $table->string('type'); // action, extra_key, wallet_credit, physical_prize
            $table->decimal('value', 15, 2)->default(0.00);
            $table->decimal('probability', 5, 2); // e.g. 10.00 para 10%
            $table->integer('total_quantity')->default(0);
            $table->integer('remaining_quantity')->default(0);
            $table->string('image_path')->nullable();
            $table->timestamps();
        });

        // 3. Crear tabla user_keys (Llaves del usuario por cada sorteo)
        Schema::create('user_keys', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
            $table->foreignId('raffle_id')->constrained('raffles')->onDelete('cascade');
            $table->integer('quantity')->default(0);
            $table->timestamps();

            $table->unique(['user_id', 'raffle_id']);
        });

        // 4. Crear tabla action_tickets (Tiquetes/Acciones acumulados para participar en sorteo mayor)
        Schema::create('action_tickets', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
            $table->foreignId('raffle_id')->constrained('raffles')->onDelete('cascade');
            $table->string('ticket_number')->unique();
            $table->foreignId('chest_prize_id')->nullable()->constrained('chest_prizes')->onDelete('set null');
            $table->timestamps();
        });

        // 5. Crear tabla chest_openings (Registro de aperturas de cofres)
        Schema::create('chest_openings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
            $table->foreignId('raffle_id')->constrained('raffles')->onDelete('cascade');
            $table->foreignId('chest_prize_id')->constrained('chest_prizes')->onDelete('cascade');
            $table->timestamps();
        });

        // 6. Modificar tabla raffles para registrar ganadores del gran premio
        Schema::table('raffles', function (Blueprint $table) {
            $table->foreignId('winner_user_id')->nullable()->constrained('users')->onDelete('set null');
            $table->foreignId('winner_action_ticket_id')->nullable()->constrained('action_tickets')->onDelete('set null');
        });

        // 7. Modificar tabla transactions para asociar rifa, revendedor y cantidad de llaves compradas
        Schema::table('transactions', function (Blueprint $table) {
            $table->foreignId('raffle_id')->nullable()->constrained('raffles')->onDelete('set null');
            $table->foreignId('reseller_id')->nullable()->constrained('reseller_profiles')->onDelete('set null');
            $table->integer('quantity')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('transactions', function (Blueprint $table) {
            $table->dropForeign(['raffle_id']);
            $table->dropColumn('raffle_id');
            $table->dropForeign(['reseller_id']);
            $table->dropColumn('reseller_id');
            $table->dropColumn('quantity');
        });

        Schema::table('raffles', function (Blueprint $table) {
            $table->dropForeign(['winner_user_id']);
            $table->dropColumn('winner_user_id');
            $table->dropForeign(['winner_action_ticket_id']);
            $table->dropColumn('winner_action_ticket_id');
        });

        Schema::dropIfExists('chest_openings');
        Schema::dropIfExists('action_tickets');
        Schema::dropIfExists('user_keys');
        Schema::dropIfExists('chest_prizes');

        // Recrear la tabla tickets original
        Schema::create('tickets', function (Blueprint $table) {
            $table->id();
            $table->foreignId('raffle_id')->constrained('raffles')->onDelete('cascade');
            $table->string('ticket_number');
            $table->enum('status', ['available', 'reserved', 'sold'])->default('available');
            $table->foreignId('user_id')->nullable()->constrained('users')->onDelete('set null');
            $table->foreignId('reseller_id')->nullable()->constrained('reseller_profiles')->onDelete('set null');
            $table->foreignId('transaction_id')->nullable()->constrained('transactions')->onDelete('set null');
            $table->timestamp('reserved_at')->nullable();
            $table->timestamp('expires_at')->nullable();
            $table->timestamp('sold_at')->nullable();
            $table->timestamps();

            $table->unique(['raffle_id', 'ticket_number']);
        });
    }
};
