<?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
    {
        Schema::create('referrals', function (Blueprint $table) {
            $table->id();
            $table->string('hashed')->unique();
            $table->string('raw')->unique();
            $table->unsignedBigInteger('owner_id');
            $table->decimal('saldo', 15, 2)->default(0);
            $table->unsignedBigInteger('used_by')->nullable();
            $table->boolean('is_used')->default(false);
            $table->timestamps();

            $table->foreign('owner_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('used_by')->references('id')->on('users')->onDelete('set null');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('referrals');
    }
};
