<?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::table('serenatas', function (Blueprint $table) {
            $table->boolean('is_ceded')->default(false)->after('ceded_amount');
            $table->foreignId('external_partner_id')->nullable()->after('is_ceded')->constrained('external_partners')->nullOnDelete();
            $table->decimal('ceded_total_cost', 12, 2)->default(0)->after('external_partner_id');
            $table->decimal('ceded_artist_amount', 12, 2)->default(0)->after('ceded_total_cost');
            $table->decimal('ceded_gift_amount', 12, 2)->default(0)->after('ceded_artist_amount');
            $table->decimal('ceded_transport_amount', 12, 2)->default(0)->after('ceded_gift_amount');
            $table->decimal('ceded_other_cost_amount', 12, 2)->default(0)->after('ceded_transport_amount');
            $table->decimal('ceded_excess_amount', 12, 2)->default(0)->after('ceded_other_cost_amount');
            $table->string('ceded_excess_status', 20)->default('equilibrado')->after('ceded_excess_amount');
            $table->timestamp('ceded_settled_at')->nullable()->after('ceded_excess_status');
            $table->text('ceded_notes')->nullable()->after('ceded_settled_at');

            $table->index(['organization_id', 'is_ceded']);
            $table->index(['organization_id', 'external_partner_id']);
        });
    }

    public function down(): void
    {
        Schema::table('serenatas', function (Blueprint $table) {
            $table->dropIndex(['organization_id', 'external_partner_id']);
            $table->dropIndex(['organization_id', 'is_ceded']);
            $table->dropConstrainedForeignId('external_partner_id');
            $table->dropColumn([
                'is_ceded',
                'ceded_total_cost',
                'ceded_artist_amount',
                'ceded_gift_amount',
                'ceded_transport_amount',
                'ceded_other_cost_amount',
                'ceded_excess_amount',
                'ceded_excess_status',
                'ceded_settled_at',
                'ceded_notes',
            ]);
        });
    }
};

