<?php

/**
 * One-time LIVE SERVER fix for:
 *   SQLSTATE[42S22] Unknown column 'net_amount' ... after `net_amount`
 *
 * Upload this file to the Laravel project ROOT (same folder as artisan), then run:
 *   php fix-sales-migrate.php
 *
 * What it does:
 * 1. Overwrites the broken paid_amount migration on disk
 * 2. Writes the safe 140001 migration if missing
 * 3. Adds sales amount columns without AFTER
 * 4. Runs php artisan migrate --force
 * 5. Deletes itself
 */

use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

require __DIR__.'/vendor/autoload.php';

$app = require __DIR__.'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();

$migrationsPath = __DIR__.'/database/migrations';

$noop140000 = <<<'PHP'
<?php

use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
    public function up(): void
    {
        // No-op. Columns are handled by 2026_06_27_140001_ensure_sales_amount_and_paid_columns.
    }

    public function down(): void
    {
        //
    }
};

PHP;

$safe140001 = <<<'PHP'
<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('sales')) {
            return;
        }

        foreach ([
            'total_amount' => 'decimal(12,2) not null default 0',
            'discount' => 'decimal(12,2) not null default 0',
            'net_amount' => 'decimal(12,2) not null default 0',
            'paid_amount' => 'decimal(12,2) not null default 0',
        ] as $column => $definition) {
            if (! Schema::hasColumn('sales', $column)) {
                DB::statement("alter table `sales` add `{$column}` {$definition}");
            }
        }
    }

    public function down(): void
    {
        if (! Schema::hasTable('sales') || ! Schema::hasColumn('sales', 'paid_amount')) {
            return;
        }

        Schema::table('sales', function (Blueprint $table): void {
            $table->dropColumn('paid_amount');
        });
    }
};

PHP;

$safe135000 = <<<'PHP'
<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('sales')) {
            return;
        }

        foreach ([
            'total_amount' => 'decimal(12,2) not null default 0',
            'discount' => 'decimal(12,2) not null default 0',
            'net_amount' => 'decimal(12,2) not null default 0',
        ] as $column => $definition) {
            if (! Schema::hasColumn('sales', $column)) {
                DB::statement("alter table `sales` add `{$column}` {$definition}");
            }
        }
    }

    public function down(): void
    {
        //
    }
};

PHP;

echo "Rewriting migration files on disk...\n";

file_put_contents($migrationsPath.'/2026_06_27_140000_add_paid_amount_to_sales_table.php', $noop140000);
file_put_contents($migrationsPath.'/2026_06_27_140001_ensure_sales_amount_and_paid_columns.php', $safe140001);
file_put_contents($migrationsPath.'/2026_06_27_135000_add_amount_fields_to_sales_table.php', $safe135000);

echo "Adding sales columns if missing...\n";

if (Schema::hasTable('sales')) {
    foreach (['total_amount', 'discount', 'net_amount', 'paid_amount'] as $column) {
        if (! Schema::hasColumn('sales', $column)) {
            DB::statement("alter table `sales` add `{$column}` decimal(12,2) not null default 0");
            echo "  + added {$column}\n";
        } else {
            echo "  = {$column} already exists\n";
        }
    }
}

echo "Running migrate --force...\n";
$exitCode = Artisan::call('migrate', ['--force' => true]);
echo Artisan::output();

@unlink(__FILE__);
echo $exitCode === 0
    ? "Done. This fix script deleted itself.\n"
    : "Migrate finished with exit code {$exitCode}. Fix script deleted itself; check errors above.\n";

exit($exitCode);
