<?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
    {
        Schema::table('lots', function (Blueprint $table) {
            $table->bigInteger('price_cents')->default(0)->after('price');
        });

        Schema::table('prospects', function (Blueprint $table) {
            $table->bigInteger('budget_cents')->nullable()->after('budget');
        });

        DB::table('lots')->update([
            'price_cents' => DB::raw('CAST(ROUND(COALESCE(price, 0) * 100) AS INTEGER)'),
        ]);
        DB::table('prospects')->whereNotNull('budget')->update([
            'budget_cents' => DB::raw('CAST(ROUND(budget * 100) AS INTEGER)'),
        ]);

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

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

    public function down(): void
    {
        Schema::table('lots', function (Blueprint $table) {
            $table->decimal('price', 14, 2)->default(0)->after('area');
        });

        Schema::table('prospects', function (Blueprint $table) {
            $table->decimal('budget', 14, 2)->nullable()->after('lot_id');
        });

        DB::table('lots')->update(['price' => DB::raw('price_cents / 100.0')]);
        DB::table('prospects')->whereNotNull('budget_cents')->update([
            'budget' => DB::raw('budget_cents / 100.0'),
        ]);

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

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