<?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
    {
        // Only run if table exists (live server may not have run the first migration yet)
        if (!Schema::hasTable('monthly_company_financials')) {
            return;
        }

        Schema::table('monthly_company_financials', function (Blueprint $table) {
            if (!Schema::hasColumn('monthly_company_financials', 'manual_revenue')) {
                $table->decimal('manual_revenue', 15, 2)->nullable()->after('revenue');
            }
            if (!Schema::hasColumn('monthly_company_financials', 'manual_expenses')) {
                $table->decimal('manual_expenses', 15, 2)->nullable()->after('expenses');
            }
            if (!Schema::hasColumn('monthly_company_financials', 'updated_by')) {
                $table->foreignId('updated_by')->nullable()->after('company_id');
            }
        });
    }

    public function down(): void
    {
        if (!Schema::hasTable('monthly_company_financials')) {
            return;
        }

        Schema::table('monthly_company_financials', function (Blueprint $table) {
            $table->dropColumn(['manual_revenue', 'manual_expenses', 'updated_by']);
        });
    }
};
