<?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::create('care_practitioners', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained('care_branches')->nullOnDelete();
            $table->foreignId('department_id')->nullable()->constrained('care_departments')->nullOnDelete();
            $table->foreignId('member_id')->nullable()->constrained('care_members')->nullOnDelete();
            $table->string('user_ref')->nullable()->index();
            $table->string('name');
            $table->string('specialty')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('care_practitioner_schedules', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('practitioner_id')->constrained('care_practitioners')->cascadeOnDelete();
            $table->unsignedTinyInteger('day_of_week'); // 0=Sunday … 6=Saturday
            $table->time('start_time');
            $table->time('end_time');
            $table->timestamps();
        });

        Schema::create('care_visits', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('branch_id')->constrained('care_branches')->cascadeOnDelete();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->string('status')->default('open'); // open, in_progress, completed
            $table->timestamp('checked_in_at')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->string('checked_in_by')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->index(['owner_ref', 'status']);
        });

        Schema::create('care_appointments', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('branch_id')->constrained('care_branches')->cascadeOnDelete();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->foreignId('practitioner_id')->nullable()->constrained('care_practitioners')->nullOnDelete();
            $table->foreignId('department_id')->nullable()->constrained('care_departments')->nullOnDelete();
            $table->foreignId('visit_id')->nullable()->constrained('care_visits')->nullOnDelete();
            $table->string('type')->default('scheduled'); // scheduled, walk_in
            $table->string('status')->default('scheduled');
            $table->timestamp('scheduled_at')->nullable();
            $table->timestamp('checked_in_at')->nullable();
            $table->timestamp('waiting_at')->nullable();
            $table->timestamp('started_at')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->timestamp('cancelled_at')->nullable();
            $table->unsignedInteger('queue_position')->nullable();
            $table->text('reason')->nullable();
            $table->text('notes')->nullable();
            $table->string('created_by')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->index(['branch_id', 'status', 'scheduled_at']);
        });

        Schema::create('care_consultations', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('visit_id')->constrained('care_visits')->cascadeOnDelete();
            $table->foreignId('appointment_id')->nullable()->constrained('care_appointments')->nullOnDelete();
            $table->foreignId('practitioner_id')->nullable()->constrained('care_practitioners')->nullOnDelete();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->string('status')->default('draft'); // draft, completed
            $table->text('symptoms')->nullable();
            $table->text('clinical_notes')->nullable();
            $table->timestamp('started_at')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->string('completed_by')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('care_vital_signs', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('consultation_id')->constrained('care_consultations')->cascadeOnDelete();
            $table->unsignedSmallInteger('bp_systolic')->nullable();
            $table->unsignedSmallInteger('bp_diastolic')->nullable();
            $table->unsignedSmallInteger('pulse')->nullable();
            $table->decimal('temperature', 4, 1)->nullable();
            $table->decimal('weight_kg', 5, 2)->nullable();
            $table->decimal('height_cm', 5, 1)->nullable();
            $table->unsignedSmallInteger('spo2')->nullable();
            $table->unsignedSmallInteger('respiratory_rate')->nullable();
            $table->string('recorded_by')->nullable();
            $table->timestamp('recorded_at');
            $table->timestamps();
        });

        Schema::create('care_diagnoses', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('consultation_id')->constrained('care_consultations')->cascadeOnDelete();
            $table->string('code')->nullable();
            $table->string('description');
            $table->boolean('is_primary')->default(false);
            $table->text('notes')->nullable();
            $table->timestamps();
        });

        Schema::create('care_consultation_documents', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('consultation_id')->constrained('care_consultations')->cascadeOnDelete();
            $table->string('file_path');
            $table->string('original_name');
            $table->string('mime_type')->nullable();
            $table->string('uploaded_by')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('care_consultation_documents');
        Schema::dropIfExists('care_diagnoses');
        Schema::dropIfExists('care_vital_signs');
        Schema::dropIfExists('care_consultations');
        Schema::dropIfExists('care_appointments');
        Schema::dropIfExists('care_visits');
        Schema::dropIfExists('care_practitioner_schedules');
        Schema::dropIfExists('care_practitioners');
    }
};
