<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     * Ticket-based conversations may not have a contact; allow contact_id to be null.
     */
    public function up(): void
    {
        if (!Schema::hasColumn('conversations', 'contact_id')) {
            return;
        }
        $driver = Schema::getConnection()->getDriverName();
        if ($driver === 'mysql') {
            Schema::getConnection()->statement('ALTER TABLE conversations MODIFY contact_id INT UNSIGNED NULL');
        } elseif ($driver === 'pgsql') {
            Schema::getConnection()->statement('ALTER TABLE conversations ALTER COLUMN contact_id DROP NOT NULL');
        }
        // sqlite: column may already allow NULL depending on version; if not, use doctrine/dbal for ->change().
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        if (!Schema::hasColumn('conversations', 'contact_id')) {
            return;
        }
        $driver = Schema::getConnection()->getDriverName();
        if ($driver === 'mysql') {
            Schema::getConnection()->statement('ALTER TABLE conversations MODIFY contact_id INT UNSIGNED NOT NULL');
        }
    }
};
