<?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('expenses', function (Blueprint $table) {
            $table->string('status', 20)->default('active')->after('notes');
            $table->foreignId('cancelled_by')->nullable()->after('status')->constrained('users');
            $table->timestamp('cancelled_at')->nullable()->after('cancelled_by');
            $table->string('cancellation_reason', 500)->nullable()->after('cancelled_at');
        });

        DB::statement("ALTER TABLE expenses ADD CONSTRAINT expenses_status_check CHECK (status IN ('active', 'cancelled'))");
    }

    public function down(): void
    {
        DB::statement("ALTER TABLE expenses DROP CONSTRAINT IF EXISTS expenses_status_check");

        Schema::table('expenses', function (Blueprint $table) {
            $table->dropConstrainedForeignId('cancelled_by');
            $table->dropColumn(['status', 'cancelled_at', 'cancellation_reason']);
        });
    }
};
