<?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->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('code', 20)->unique();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('website')->nullable();
            $table->string('commission_type')->default('revenue_share'); // cpa|revenue_share|hybrid
            $table->decimal('revenue_share_percent', 5, 2)->default(25.00);
            $table->decimal('cpa_amount', 10, 2)->default(0);
            $table->string('status')->default('pending'); // pending|active|suspended|rejected
            $table->integer('total_referrals')->default(0);
            $table->decimal('total_player_deposits', 15, 2)->default(0);
            $table->decimal('total_commissions_earned', 15, 2)->default(0);
            $table->decimal('balance', 15, 2)->default(0);
            $table->text('notes')->nullable();
            $table->timestamps();
        });

        Schema::create('affiliate_referrals', function (Blueprint $table) {
            $table->id();
            $table->foreignId('affiliate_id')->constrained()->cascadeOnDelete();
            $table->foreignId('referred_user_id')->constrained('users')->cascadeOnDelete();
            $table->timestamp('first_deposit_at')->nullable();
            $table->decimal('first_deposit_amount', 15, 2)->default(0);
            $table->decimal('total_deposits', 15, 2)->default(0);
            $table->decimal('commission_earned', 15, 2)->default(0);
            $table->boolean('cpa_paid')->default(false);
            $table->timestamps();
            $table->unique('referred_user_id');
        });

        Schema::create('affiliate_commissions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('affiliate_id')->constrained()->cascadeOnDelete();
            $table->foreignId('referred_user_id')->constrained('users')->cascadeOnDelete();
            $table->string('type'); // cpa|revenue_share|monthly
            $table->decimal('amount', 15, 2);
            $table->string('status')->default('pending'); // pending|paid|cancelled
            $table->timestamp('period_start')->nullable();
            $table->timestamp('period_end')->nullable();
            $table->timestamp('paid_at')->nullable();
            $table->timestamps();
        });

        // Track referral code on users table
        Schema::table('users', function (Blueprint $table) {
            $table->unsignedBigInteger('affiliate_id')->nullable()->after('kyc_approved_at');
            $table->string('referral_code', 20)->nullable()->after('affiliate_id');
        });
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn(['affiliate_id', 'referral_code']);
        });
        Schema::dropIfExists('affiliate_commissions');
        Schema::dropIfExists('affiliate_referrals');
        Schema::dropIfExists('affiliates');
    }
};
