<?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
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('announcements', function (Blueprint $table) {
            $table->unsignedBigInteger('sponsor_id')->nullable()->after('employer_id');

            // Only add FK constraint on MySQL (SQLite in-memory DB used in tests
            // does not reliably support named foreign keys via constrained()).
            if (DB::getDriverName() !== 'sqlite') {
                $table->foreign('sponsor_id')->references('id')->on('sponsors')->onDelete('cascade');
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('announcements', function (Blueprint $table) {
            if (DB::getDriverName() !== 'sqlite') {
                $table->dropForeign(['sponsor_id']);
            }
            $table->dropColumn('sponsor_id');
        });
    }
};
