<?php

declare(strict_types=1);

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

return new class extends Migration
{
    public function up(): void
    {
        if (Schema::hasTable('trial_booking_settings') && ! Schema::hasTable('site_settings')) {
            Schema::rename('trial_booking_settings', 'site_settings');
        }

        if (! Schema::hasTable('site_settings')) {
            Schema::create('site_settings', function (Blueprint $table): void {
                $table->id();
                $table->text('admin_emails')->nullable();
                $table->unsignedSmallInteger('duplicate_window_days')->default(30);
                $table->string('site_name', 100)->nullable();
                $table->text('site_description')->nullable();
                $table->string('og_image_path', 255)->nullable();
                $table->timestamps();
            });

            return;
        }

        if (! Schema::hasColumn('site_settings', 'site_name')) {
            Schema::table('site_settings', function (Blueprint $table): void {
                $table->string('site_name', 100)->nullable()->after('duplicate_window_days');
            });
        }

        if (! Schema::hasColumn('site_settings', 'site_description')) {
            Schema::table('site_settings', function (Blueprint $table): void {
                $table->text('site_description')->nullable()->after('site_name');
            });
        }

        if (! Schema::hasColumn('site_settings', 'og_image_path')) {
            Schema::table('site_settings', function (Blueprint $table): void {
                $table->string('og_image_path', 255)->nullable()->after('site_description');
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('site_settings') && ! Schema::hasTable('trial_booking_settings')) {
            Schema::rename('site_settings', 'trial_booking_settings');
        }
    }
};
