<?php

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

return new class extends Migration {
    public function up(): void
    {
        $now = now();
        $typeId = DB::table('campaign_types')->where('name', 'General')->value('id');

        if (! $typeId) {
            $typeId = DB::table('campaign_types')->insertGetId([
                'name' => 'General',
                'created_at' => $now,
                'updated_at' => $now,
            ]);
        }

        DB::table('campaigns')
            ->whereNull('campaign_type_id')
            ->update(['campaign_type_id' => $typeId, 'updated_at' => $now]);
    }

    public function down(): void
    {
        // Retain repaired campaign relationships when rolling back.
    }
};
