<?php

declare(strict_types=1);

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('affiliates', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->unique()->constrained()->cascadeOnDelete();
            $table->string('referral_code')->unique();
            $table->enum('status', ['pending', 'active', 'suspended'])->default('pending');
            $table->enum('commission_type', ['percentage', 'flat'])->default('percentage');
            $table->decimal('commission_rate', 8, 2)->default(10.00);
            $table->boolean('recurring_commissions')->default(false);
            $table->decimal('minimum_payout', 8, 2)->default(50.00);
            $table->decimal('total_earned', 10, 2)->default(0);
            $table->decimal('total_paid', 10, 2)->default(0);
            $table->decimal('pending_balance', 10, 2)->default(0);
            $table->timestamp('approved_at')->nullable();
            $table->timestamps();

            $table->index('status');
            $table->index('referral_code');
        });
    }

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