<?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('affiliates', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->unsignedBigInteger('user_id');
            $table->string('business_name');
            $table->string('business_type', 100)->nullable();
            $table->enum('status', ['active', 'inactive', 'suspended', 'pending_approval'])
                ->default('pending_approval');
            $table->decimal('commission_rate', 5, 2)->nullable();
            $table->integer('total_leads')->default(0);
            $table->integer('total_bookings')->default(0);
            $table->decimal('total_revenue', 12, 2)->default(0);
            $table->decimal('total_commission', 12, 2)->default(0);
            $table->string('website_url', 255)->nullable();
            $table->string('api_key', 255)->nullable();
            $table->string('bank_account_holder', 255)->nullable();
            $table->string('bank_account_number', 50)->nullable();
            $table->string('bank_name', 255)->nullable();
            $table->timestamps();
            
            $table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->index('tenant_id');
            $table->index('status');
        });
    }

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