<?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::rename('contact_messages', 'inquiries');

        Schema::table('inquiries', function (Blueprint $table) {
            $table->foreignId('service_id')->nullable()->after('company')->constrained('ref_services')->nullOnDelete();
            $table->string('ip')->nullable()->after('message');
            $table->string('browser')->nullable()->after('ip');
            $table->json('location_info')->nullable()->after('browser');
        });

        // Drop the old service text column
        Schema::table('inquiries', function (Blueprint $table) {
            $table->dropColumn('service');
        });
    }

    public function down(): void
    {
        Schema::table('inquiries', function (Blueprint $table) {
            $table->string('service')->nullable()->after('company');
        });

        Schema::table('inquiries', function (Blueprint $table) {
            $table->dropForeign(['service_id']);
            $table->dropColumn(['service_id', 'ip', 'browser', 'location_info']);
        });

        Schema::rename('inquiries', 'contact_messages');
    }
};
