<?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('companies', function (Blueprint $table) {
            if (!Schema::hasColumn('companies', 'cash_balance')) {
                $table->decimal('cash_balance', 20, 2)->nullable()->default(null);
            }
            if (!Schema::hasColumn('companies', 'total_assets')) {
                $table->decimal('total_assets', 20, 2)->nullable()->default(null);
            }
            if (!Schema::hasColumn('companies', 'total_liabilities')) {
                $table->decimal('total_liabilities', 20, 2)->nullable()->default(null);
            }
            if (!Schema::hasColumn('companies', 'revenue')) {
                $table->decimal('revenue', 20, 2)->nullable()->default(null);
            }
            if (!Schema::hasColumn('companies', 'expenses_manual')) {
                $table->decimal('expenses_manual', 20, 2)->nullable()->default(null);
            }
            if (!Schema::hasColumn('companies', 'credit_score')) {
                $table->integer('credit_score')->nullable()->default(null);
            }
            if (!Schema::hasColumn('companies', 'runway_days')) {
                $table->integer('runway_days')->nullable()->default(null);
            }
            if (!Schema::hasColumn('companies', 'is_holding_company')) {
                $table->boolean('is_holding_company')->default(false);
            }
            if (!Schema::hasColumn('companies', 'parent_company_id')) {
                $table->unsignedBigInteger('parent_company_id')->nullable()->default(null);
            }
            if (!Schema::hasColumn('companies', 'health_status')) {
                $table->string('health_status', 30)->default('good');
            }
            if (!Schema::hasColumn('companies', 'last_health_check')) {
                $table->date('last_health_check')->nullable()->default(null);
            }
        });

        // If columns already exist with default 0, change them to nullable so we can
        // distinguish "never set" (NULL) from "explicitly set to 0" (0).
        $numericCols = ['cash_balance', 'total_assets', 'total_liabilities', 'revenue', 'expenses_manual'];
        foreach ($numericCols as $col) {
            if (Schema::hasColumn('companies', $col)) {
                // Convert 0-default rows to NULL so the dashboard knows they haven't been set
                \DB::statement("ALTER TABLE companies MODIFY `{$col}` DECIMAL(20,2) NULL DEFAULT NULL");
                \DB::statement("UPDATE companies SET `{$col}` = NULL WHERE `{$col}` = 0");
            }
        }
    }

    public function down(): void
    {
        // Restore non-nullable defaults
        $numericCols = ['cash_balance', 'total_assets', 'total_liabilities', 'revenue', 'expenses_manual'];
        foreach ($numericCols as $col) {
            if (Schema::hasColumn('companies', $col)) {
                \DB::statement("ALTER TABLE companies MODIFY `{$col}` DECIMAL(20,2) NOT NULL DEFAULT 0");
                \DB::statement("UPDATE companies SET `{$col}` = 0 WHERE `{$col}` IS NULL");
            }
        }
    }
};
