<?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('external_partner_movements', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('organization_id');
            $table->unsignedBigInteger('serenata_id')->nullable();
            $table->unsignedBigInteger('external_partner_id');
            $table->unsignedBigInteger('created_by_user_id')->nullable();
            $table->string('movement_type', 40);
            $table->string('settlement_kind', 30)->default('pendiente');
            $table->string('direction', 30);
            $table->decimal('amount', 12, 2)->default(0);
            $table->decimal('signed_amount', 12, 2)->default(0);
            $table->string('counterpart_reference', 120)->nullable();
            $table->date('due_date')->nullable();
            $table->text('notes')->nullable();
            $table->string('status', 20)->default('activo');
            $table->timestamps();

            $table->index(['organization_id', 'external_partner_id'], 'epm_org_partner_idx');
            $table->index(['organization_id', 'serenata_id'], 'epm_org_serenata_idx');
            $table->index(['organization_id', 'status'], 'epm_org_status_idx');
        });
    }

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