<?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('visits_raw_staging', function (Blueprint $table) {
            $table->foreignId('resolved_mr_id')->nullable()->after('validation_errors')->constrained('mrs')->nullOnDelete();
            $table->date('resolved_visit_date')->nullable()->after('resolved_mr_id');
            $table->foreignId('suggested_doctor_id')->nullable()->after('resolved_visit_date')->constrained('doctors')->nullOnDelete();
            $table->unsignedTinyInteger('match_confidence')->nullable()->after('suggested_doctor_id')->comment('0-100');
            $table->json('match_candidates')->nullable()->after('match_confidence')->comment('Top N candidates with scores and explanations');
        });
    }

    public function down(): void
    {
        Schema::table('visits_raw_staging', function (Blueprint $table) {
            $table->dropForeign(['resolved_mr_id']);
            $table->dropForeign(['suggested_doctor_id']);
            $table->dropColumn(['resolved_mr_id', 'resolved_visit_date', 'suggested_doctor_id', 'match_confidence', 'match_candidates']);
        });
    }
};
