<?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_patients', 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')->nullable()->constrained('care_branches')->nullOnDelete();
            $table->string('patient_number')->index();
            $table->string('first_name');
            $table->string('last_name');
            $table->string('other_names')->nullable();
            $table->string('gender')->nullable();
            $table->date('date_of_birth')->nullable();
            $table->string('phone')->nullable()->index();
            $table->string('email')->nullable();
            $table->string('national_id')->nullable()->index();
            $table->string('address')->nullable();
            $table->string('city')->nullable();
            $table->string('region')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->unique(['organization_id', 'patient_number']);
            $table->index(['owner_ref', 'last_name', 'first_name']);
        });

        Schema::create('care_patient_allergies', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->string('allergen');
            $table->string('severity')->default('unknown');
            $table->text('notes')->nullable();
            $table->timestamps();
        });

        Schema::create('care_patient_conditions', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->string('condition');
            $table->date('onset_date')->nullable();
            $table->boolean('is_chronic')->default(false);
            $table->text('notes')->nullable();
            $table->timestamps();
        });

        Schema::create('care_patient_family_history', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->string('relation');
            $table->string('condition');
            $table->text('notes')->nullable();
            $table->timestamps();
        });

        Schema::create('care_emergency_contacts', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->string('name');
            $table->string('phone');
            $table->string('relationship')->nullable();
            $table->boolean('is_primary')->default(false);
            $table->timestamps();
        });

        Schema::create('care_insurance_policies', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->string('provider_name');
            $table->string('policy_number')->nullable();
            $table->string('coverage_type')->nullable();
            $table->date('expiry_date')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();
        });

        Schema::create('care_patient_attachments', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->string('file_path');
            $table->string('original_name');
            $table->string('mime_type')->nullable();
            $table->string('document_type')->default('other');
            $table->string('uploaded_by')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('care_patient_attachments');
        Schema::dropIfExists('care_insurance_policies');
        Schema::dropIfExists('care_emergency_contacts');
        Schema::dropIfExists('care_patient_family_history');
        Schema::dropIfExists('care_patient_conditions');
        Schema::dropIfExists('care_patient_allergies');
        Schema::dropIfExists('care_patients');
    }
};
