<?php

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

/**
 * PRC-1143 — konsolide şema: `affiliate_referrals` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('affiliate_referrals', function (Blueprint $t) {
            $t->id();
            $t->unsignedBigInteger('affiliate_id');
            $t->unsignedBigInteger('referred_client_id')->nullable();
            $t->string('ip_address', 255)->nullable();
            $t->enum('status', ['visited','signup','converted','rejected'])->default('visited');
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->index('affiliate_id', 'affiliate_referrals_affiliate_id_foreign');
            $t->index('referred_client_id', 'affiliate_referrals_referred_client_id_foreign');
            $t->foreign('affiliate_id', 'affiliate_referrals_affiliate_id_foreign')->references('id')->on('affiliates')->onDelete('cascade');
            $t->foreign('referred_client_id', 'affiliate_referrals_referred_client_id_foreign')->references('id')->on('clients')->onDelete('set null');
        });
    }

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