<?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('organizations', function (Blueprint $table) {
            $table->string('company_name')->nullable();
            $table->string('street')->nullable();
            $table->string('house_number')->nullable();
            $table->string('website')->nullable();
            $table->string('phone')->nullable();
            $table->string('email')->nullable();
            $table->string('invoice_email')->nullable();
            $table->string('kvk_number')->nullable();
            $table->string('vat_number')->nullable();
            $table->string('iban')->nullable();
            $table->string('contact_name')->nullable();
            $table->string('country')->nullable();
        });

        if (Schema::hasTable('organizations')) {
            if (Schema::hasColumn('organizations', 'company_name')) {
                DB::statement('UPDATE organizations SET company_name = name WHERE company_name IS NULL OR company_name = \'\' ');
            }
            if (Schema::hasColumn('organizations', 'street') && Schema::hasColumn('organizations', 'address_line_1')) {
                DB::statement('UPDATE organizations SET street = address_line_1 WHERE street IS NULL OR street = \'\' ');
            }
        }
    }

    public function down(): void
    {
        Schema::table('organizations', function (Blueprint $table) {
            $table->dropColumn([
                'company_name',
                'street',
                'house_number',
                'website',
                'phone',
                'email',
                'invoice_email',
                'kvk_number',
                'vat_number',
                'iban',
                'contact_name',
                'country',
            ]);
        });
    }
};
