<?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('rentals', function (Blueprint $table) {
            $table->decimal('paid_amount', 15, 2)->default(0)->after('total_amount');
            $table->decimal('remaining_amount', 15, 2)->default(0)->after('paid_amount');
        });

        // Update existing rentals
        $rentals = \DB::table('rentals')->get();
        foreach ($rentals as $rental) {
            \DB::table('rentals')->where('id', $rental->id)->update([
                'remaining_amount' => $rental->total_amount
            ]);
        }
    }

    public function down(): void
    {
        Schema::table('rentals', function (Blueprint $table) {
            $table->dropColumn(['paid_amount', 'remaining_amount']);
        });
    }
};
