<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('consignments', function (Blueprint $table) {
            $table->string('investor_share_type')->default('percentage')->after('investor_custom_account_id');
            $table->decimal('investor_share_value', 15, 2)->default(0.00)->after('investor_share_type');
        });

        // Migrate existing data from investor_share_pct to investor_share_value
        DB::statement("UPDATE consignments SET investor_share_value = investor_share_pct");

        Schema::table('consignments', function (Blueprint $table) {
            $table->dropColumn('investor_share_pct');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('consignments', function (Blueprint $table) {
            $table->decimal('investor_share_pct', 5, 2)->default(50.00)->after('investor_custom_account_id');
        });

        DB::statement("UPDATE consignments SET investor_share_pct = investor_share_value WHERE investor_share_type = 'percentage'");

        Schema::table('consignments', function (Blueprint $table) {
            $table->dropColumn('investor_share_type');
            $table->dropColumn('investor_share_value');
        });
    }
};
