<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('incomes', function (Blueprint $table) {
            $table->id();

            // Aislamiento Multi-Tenant
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();

            // Relación con el Crédito (Añadido)
            $table->foreignId('credit_id')->nullable()->constrained('credits')->nullOnDelete();
            // Relación con el Pago (Añadido)
            $table->foreignId('payment_id')->nullable()->constrained('payments')->nullOnDelete();

            // Auditoría y Cobranza
            $table->foreignId('user_id')->constrained()->cascadeOnDelete()->comment('Usuario que registra el ingreso.');
            $table->foreignId('collector_user_id')->nullable()->constrained('users')->nullOnDelete()->comment('Cobrador al que se le asigna el ingreso.'); // Añadido

            $table->decimal('amount', 12, 2);
            $table->string('category'); // Reemplaza 'description'
            $table->string('method')->nullable(); // Añadido
            $table->date('operation_date')->nullable(); // Reemplaza 'date'
            $table->string('reference')->nullable(); // Añadido
            $table->text('notes')->nullable(); // Añadido
            $table->json('metadata')->nullable(); // Añadido

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('incomes');
    }
};
