<?php

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

/**
 * Ladill Care core schema — multi-branch healthcare facilities.
 *
 * Hierarchy: owner_ref → Organization → Branch → Department
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('care_organizations', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->string('name');
            $table->string('slug')->index();
            $table->string('logo_path')->nullable();
            $table->string('timezone')->default('UTC');
            $table->json('settings')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->unique(['owner_ref', 'slug']);
        });

        Schema::create('care_branches', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->string('name');
            $table->string('code')->nullable();
            $table->string('address')->nullable();
            $table->string('phone')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('care_departments', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('branch_id')->constrained('care_branches')->cascadeOnDelete();
            $table->string('name');
            $table->string('type')->default('general'); // general, outpatient, laboratory, pharmacy, radiology, etc.
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('care_members', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->string('user_ref')->index();
            $table->string('role');
            $table->foreignId('branch_id')->nullable()->constrained('care_branches')->nullOnDelete();
            $table->timestamps();
            $table->unique(['organization_id', 'user_ref']);
        });

        Schema::create('care_audit_logs', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->nullable()->constrained('care_organizations')->nullOnDelete();
            $table->string('actor_ref')->nullable();
            $table->string('action');
            $table->string('subject_type')->nullable();
            $table->unsignedBigInteger('subject_id')->nullable();
            $table->json('metadata')->nullable();
            $table->string('ip_address')->nullable();
            $table->timestamp('created_at')->useCurrent();
            $table->index(['owner_ref', 'created_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('care_audit_logs');
        Schema::dropIfExists('care_members');
        Schema::dropIfExists('care_departments');
        Schema::dropIfExists('care_branches');
        Schema::dropIfExists('care_organizations');
    }
};
