<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('purchases', function (Blueprint $table) {
            $table->enum('payment_status', ['unpaid', 'partial', 'paid'])
                  ->default('unpaid')
                  ->after('status');
            $table->decimal('amount_paid', 12, 2)->default(0)->after('total');
            $table->decimal('amount_due', 12, 2)->default(0)->after('amount_paid');
        });

        // Backfill amount_due = total for existing rows
        DB::statement('UPDATE purchases SET amount_due = total WHERE deleted_at IS NULL');
    }

    public function down(): void
    {
        Schema::table('purchases', function (Blueprint $table) {
            $table->dropColumn(['payment_status', 'amount_paid', 'amount_due']);
        });
    }
};
