<?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::table('invoices', function (Blueprint $table) {
            // Only add columns that don't already exist
            if (!Schema::hasColumn('invoices', 'payment_method')) {
                $table->enum('payment_method', ['bank', 'cash', 'mixed'])->default('bank')->after('terms');
            }
            if (!Schema::hasColumn('invoices', 'cash_amount')) {
                $table->decimal('cash_amount', 15, 2)->default(0)->after('payment_method');
            }
            if (!Schema::hasColumn('invoices', 'month_revenue')) {
                $table->decimal('month_revenue', 15, 2)->nullable()->after('allocation_month');
            }
            if (!Schema::hasColumn('invoices', 'month_expenses')) {
                $table->decimal('month_expenses', 15, 2)->nullable()->after('month_revenue');
            }
            if (!Schema::hasColumn('invoices', 'month_net_profit')) {
                $table->decimal('month_net_profit', 15, 2)->nullable()->after('month_expenses');
            }
            if (!Schema::hasColumn('invoices', 'sweep_percentage')) {
                $table->decimal('sweep_percentage', 5, 2)->nullable()->after('month_net_profit');
            }
            if (!Schema::hasColumn('invoices', 'is_loan_repayment')) {
                $table->boolean('is_loan_repayment')->default(false)->after('is_loan_request');
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('invoices', function (Blueprint $table) {
            $table->dropColumn(['payment_method', 'cash_amount', 'month_revenue', 'month_expenses', 'month_net_profit', 'sweep_percentage', 'is_loan_repayment']);
        });
    }
};
