<?php

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

return new class extends Migration {
    public function up(): void
    {
        Schema::table('purchases', function (Blueprint $table) {
            if (!Schema::hasColumn('purchases', 'date')) {
                $table->date('date')->nullable()->after('purchase_date')->comment('매입일(MNG용)');
            }
            if (!Schema::hasColumn('purchases', 'vendor')) {
                $table->string('vendor', 100)->nullable()->after('date')->comment('공급자명(MNG용)');
            }
            if (!Schema::hasColumn('purchases', 'item')) {
                $table->string('item', 200)->nullable()->after('vendor')->comment('품목명(MNG용)');
            }
            if (!Schema::hasColumn('purchases', 'category')) {
                $table->string('category', 50)->default('운영비')->after('item')->comment('분류(MNG용)');
            }
            if (!Schema::hasColumn('purchases', 'amount')) {
                $table->bigInteger('amount')->default(0)->after('category')->comment('금액(MNG용)');
            }
            if (!Schema::hasColumn('purchases', 'vat')) {
                $table->bigInteger('vat')->default(0)->after('amount')->comment('부가세(MNG용)');
            }
            if (!Schema::hasColumn('purchases', 'invoice_no')) {
                $table->string('invoice_no', 50)->nullable()->after('vat')->comment('인보이스번호(MNG용)');
            }
            if (!Schema::hasColumn('purchases', 'memo')) {
                $table->text('memo')->nullable()->after('invoice_no')->comment('메모(MNG용)');
            }
        });
    }

    public function down(): void
    {
        Schema::table('purchases', function (Blueprint $table) {
            $columns = ['date', 'vendor', 'item', 'category', 'amount', 'vat', 'invoice_no', 'memo'];
            foreach ($columns as $col) {
                if (Schema::hasColumn('purchases', $col)) {
                    $table->dropColumn($col);
                }
            }
        });
    }
};
