<?php

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

/**
 * Staff management foundations:
 * Department → Care Unit → Bed (inpatient)
 * Member → Staff Assignment (dated, primary/temporary/specialty)
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('care_care_units', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('department_id')->constrained('care_departments')->cascadeOnDelete();
            $table->string('name');
            $table->string('code')->nullable();
            /** inpatient | outpatient | service | float_pool */
            $table->string('kind')->default('inpatient');
            $table->boolean('is_active')->default(true);
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();
            $table->softDeletes();

            $table->index(['organization_id', 'kind']);
            $table->index(['department_id', 'is_active']);
        });

        Schema::create('care_beds', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('care_unit_id')->constrained('care_care_units')->cascadeOnDelete();
            $table->string('label');
            $table->string('room')->nullable();
            /** available | occupied | reserved | maintenance | closed */
            $table->string('status')->default('available');
            $table->boolean('is_active')->default(true);
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();
            $table->softDeletes();

            $table->index(['care_unit_id', 'status']);
            $table->index(['organization_id', 'is_active']);
        });

        Schema::create('care_staff_assignments', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('member_id')->constrained('care_members')->cascadeOnDelete();
            $table->foreignId('department_id')->nullable()->constrained('care_departments')->nullOnDelete();
            $table->foreignId('care_unit_id')->nullable()->constrained('care_care_units')->nullOnDelete();
            /** primary | temporary | specialty */
            $table->string('kind')->default('primary');
            /** Nursing/post title on this assignment — not RBAC Member.role */
            $table->string('assignment_role')->nullable();
            /** day | evening | night | rotating — full shift entities come later */
            $table->string('shift_code')->nullable();
            $table->date('starts_on');
            $table->date('ends_on')->nullable();
            /** active | scheduled | ended */
            $table->string('status')->default('active');
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['organization_id', 'status']);
            $table->index(['member_id', 'status']);
            $table->index(['care_unit_id', 'status']);
            $table->index(['starts_on', 'ends_on']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('care_staff_assignments');
        Schema::dropIfExists('care_beds');
        Schema::dropIfExists('care_care_units');
    }
};
