<?php

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

return new class extends Migration
{
    /**
     * Flags invoices that pre-date the system (entered manually from the old
     * paper books). Guarded so the production SQL script can add the column
     * first without this migration failing afterwards.
     */
    public function up(): void
    {
        if (Schema::hasColumn('invoices', 'is_legacy')) {
            return;
        }

        Schema::table('invoices', function (Blueprint $table) {
            $table->boolean('is_legacy')->default(false)->after('channel');
        });
    }

    public function down(): void
    {
        if (! Schema::hasColumn('invoices', 'is_legacy')) {
            return;
        }

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