<?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('credit_transactions', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('user_id');
            $table->integer('amount');
            $table->enum('type', [
                'onboarding',
                'affiliate',
                'admin_gift',
                'usage',
                'refund',
                'subscription',
            ]);
            $table->string('description');
            $table->uuid('reference_id')->nullable();
            $table->string('reference_type')->nullable();
            $table->uuid('created_by')->nullable();
            $table->timestamps();

            $table->foreign('user_id')
                  ->references('id')->on('users')
                  ->cascadeOnDelete();
            $table->foreign('created_by')
                  ->references('id')->on('users')
                  ->nullOnDelete();
            $table->index(['user_id', 'type']);
            $table->index(['user_id', 'created_at']);
        });
    }

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