<?php

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

return new class extends Migration
{
    /**
     * ASM = Manager of the MIO (MR). HOS = Manager of the ASM.
     * mr_doctor_route = row-wise order of doctors per employee for map path drawing.
     */
    public function up(): void
    {
        Schema::table('mrs', function (Blueprint $table) {
            $table->string('asm_name', 100)->nullable()->after('team_name')->comment('ASM = Manager of MR');
            $table->string('hos_name', 100)->nullable()->after('asm_name')->comment('HOS = Manager of ASM');
        });

        Schema::create('mr_doctor_route', function (Blueprint $table) {
            $table->id();
            $table->foreignId('mr_id')->constrained('mrs')->cascadeOnDelete();
            $table->foreignId('doctor_id')->constrained('doctors')->cascadeOnDelete();
            $table->unsignedInteger('sequence')->comment('Order of doctor in route (row order per employee)');
            $table->timestamps();
            $table->unique(['mr_id', 'sequence']);
        });
    }

    public function down(): void
    {
        Schema::table('mrs', function (Blueprint $table) {
            $table->dropColumn(['asm_name', 'hos_name']);
        });
        Schema::dropIfExists('mr_doctor_route');
    }
};
