<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('tasks', function (Blueprint $table) {
            // The inbound email this task was raised for, when the task was
            // generated automatically because an email landed on a client file.
            // Nullable: hand-created tasks have no originating email. Used to
            // de-duplicate handler notifications and to deep-link back to the
            // email from the task.
            $table->foreignId('contact_message_id')->nullable()->after('owner_id')
                ->constrained('contact_messages')->nullOnDelete();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('tasks', function (Blueprint $table) {
            $table->dropConstrainedForeignId('contact_message_id');
        });
    }
};
