<?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::table('dsar_evidence_items', function (Blueprint $table) {
            // Make attachment FK nullable so evidence can be sourced from Document (not just ContactMessageAttachment)
            $table->unsignedBigInteger('contact_message_attachment_id')->nullable()->change();

            // New: evidence sourced from a manually-uploaded Document
            $table->foreignId('document_id')
                ->nullable()
                ->constrained('documents')
                ->nullOnDelete()
                ->after('contact_message_attachment_id');
        });
    }

    public function down(): void
    {
        Schema::table('dsar_evidence_items', function (Blueprint $table) {
            $table->dropForeign(['document_id']);
            $table->dropColumn('document_id');
            $table->unsignedBigInteger('contact_message_attachment_id')->nullable(false)->change();
        });
    }
};
