<?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('credit_notes', function (Blueprint $table) {
            if (! Schema::hasColumn('credit_notes', 'payment_method')) {
                $table->string('payment_method', 64)->nullable()->after('currency');
            }
            if (! Schema::hasColumn('credit_notes', 'bank_account_id')) {
                $table->foreignId('bank_account_id')->nullable()->after('payment_method')->constrained('bank_accounts')->onDelete('set null');
            }
            if (! Schema::hasColumn('credit_notes', 'signature_file_path')) {
                $table->string('signature_file_path')->nullable()->after('signature_id');
            }
            if (! Schema::hasColumn('credit_notes', 'typed_signature_text')) {
                $table->string('typed_signature_text')->nullable()->after('signature_file_path');
            }
            if (! Schema::hasColumn('credit_notes', 'typed_signature_font')) {
                $table->string('typed_signature_font', 64)->nullable()->after('typed_signature_text');
            }
        });
    }

    public function down(): void
    {
        Schema::table('credit_notes', function (Blueprint $table) {
            if (Schema::hasColumn('credit_notes', 'typed_signature_font')) {
                $table->dropColumn('typed_signature_font');
            }
            if (Schema::hasColumn('credit_notes', 'typed_signature_text')) {
                $table->dropColumn('typed_signature_text');
            }
            if (Schema::hasColumn('credit_notes', 'signature_file_path')) {
                $table->dropColumn('signature_file_path');
            }
            if (Schema::hasColumn('credit_notes', 'bank_account_id')) {
                $table->dropConstrainedForeignId('bank_account_id');
            }
            if (Schema::hasColumn('credit_notes', 'payment_method')) {
                $table->dropColumn('payment_method');
            }
        });
    }
};
