<?php

declare(strict_types=1);

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('departments', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100)->comment('科室名称');
            $table->string('type', 20)->nullable()->comment('科室类型');
            $table->string('description', 500)->nullable()->comment('科室描述');
            $table->boolean('status')->default(true)->comment('状态');
            $table->integer('sort_order')->default(0)->comment('排序');
            $table->timestamps();

            $table->index('status');
        });

        // 医师表
        Schema::create('doctors', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->unique()->constrained()->cascadeOnDelete();
            $table->foreignId('department_id')->nullable()->constrained()->nullOnDelete();
            $table->string('license_no', 50)->comment('执业证书编号');
            $table->string('practice_scope', 200)->comment('执业范围');
            $table->string('title', 20)->nullable()->comment('职称');
            $table->boolean('is_licensed')->default(false)->comment('是否具备互联网诊疗资质');
            $table->unsignedInteger('practice_years')->default(0)->comment('执业年限');
            $table->text('introduction')->nullable()->comment('简介');
            $table->timestamps();

            $table->index('department_id');
            $table->index('is_licensed');
        });

        // 患者表
        Schema::create('patients', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->unique()->constrained()->cascadeOnDelete();
            $table->string('medical_no', 30)->unique()->nullable()->comment('病历号');
            $table->boolean('is_first_visit')->default(true)->comment('是否初诊');
            $table->string('blood_type', 10)->nullable()->comment('血型');
            $table->text('allergy_history')->nullable()->comment('过敏史(加密)');
            $table->text('address')->nullable()->comment('住址(加密)');
            $table->timestamps();

            $table->index('is_first_visit');
        });

        // 排班表
        Schema::create('schedules', function (Blueprint $table) {
            $table->id();
            $table->foreignId('doctor_id')->constrained()->cascadeOnDelete();
            $table->foreignId('department_id')->constrained()->cascadeOnDelete();
            $table->date('date')->comment('排班日期');
            $table->string('time_slot', 50)->comment('时段');
            $table->unsignedInteger('quota')->default(0)->comment('号源总数');
            $table->unsignedInteger('remaining')->default(0)->comment('剩余号源');
            $table->decimal('fee', 10, 2)->default(0)->comment('挂号费');
            $table->boolean('status')->default(true)->comment('状态');
            $table->timestamps();

            $table->index(['doctor_id', 'date']);
            $table->index(['department_id', 'date']);
            $table->unique(['doctor_id', 'date', 'time_slot']);
        });

        // 挂号表
        Schema::create('appointments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('patient_id')->constrained()->cascadeOnDelete();
            $table->foreignId('doctor_id')->constrained()->cascadeOnDelete();
            $table->foreignId('schedule_id')->constrained()->cascadeOnDelete();
            $table->string('appointment_no', 30)->unique()->comment('挂号编号');
            $table->string('status', 20)->default('pending')->comment('状态');
            $table->decimal('fee', 10, 2)->default(0)->comment('挂号费');
            $table->timestamp('cancelled_at')->nullable()->comment('取消时间');
            $table->string('cancel_reason', 500)->nullable()->comment('取消原因');
            $table->timestamps();

            $table->index('patient_id');
            $table->index('doctor_id');
            $table->index('schedule_id');
            $table->index('status');
        });

        // 问诊表
        Schema::create('consultations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('appointment_id')->unique()->constrained()->cascadeOnDelete();
            $table->foreignId('patient_id')->constrained()->cascadeOnDelete();
            $table->foreignId('doctor_id')->constrained()->cascadeOnDelete();
            $table->string('type', 20)->comment('问诊类型');
            $table->string('status', 20)->default('waiting')->comment('状态');
            $table->timestamp('started_at')->nullable()->comment('开始时间');
            $table->timestamp('ended_at')->nullable()->comment('结束时间');
            $table->string('transfer_to', 100)->nullable()->comment('转诊科室');
            $table->string('transfer_reason', 500)->nullable()->comment('转诊原因');
            $table->timestamps();

            $table->index('patient_id');
            $table->index('doctor_id');
            $table->index('status');
        });

        // 问诊消息表
        Schema::create('messages', function (Blueprint $table) {
            $table->id();
            $table->foreignId('consultation_id')->constrained()->cascadeOnDelete();
            $table->string('sender_type', 20)->comment('发送者类型: patient/doctor/system');
            $table->unsignedBigInteger('sender_id')->comment('发送者ID');
            $table->text('content')->comment('消息内容');
            $table->string('type', 20)->default('text')->comment('消息类型');
            $table->timestamp('sent_at')->nullable()->comment('发送时间');

            $table->index('consultation_id');
        });

        // 电子病历表 (WS 445-2014)
        Schema::create('medical_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('consultation_id')->unique()->constrained()->cascadeOnDelete();
            $table->foreignId('patient_id')->constrained()->cascadeOnDelete();
            $table->foreignId('doctor_id')->constrained()->cascadeOnDelete();
            $table->text('chief_complaint')->comment('主诉(WS445必填)');
            $table->text('present_illness')->comment('现病史(WS445必填)');
            $table->text('past_history')->nullable()->comment('既往史');
            $table->text('physical_exam')->nullable()->comment('体格检查');
            $table->string('diagnosis_code', 20)->comment('诊断编码(ICD-10)');
            $table->string('diagnosis_name', 200)->comment('诊断名称');
            $table->text('treatment_plan')->nullable()->comment('处理意见');
            $table->date('visit_date')->comment('就诊日期(WS445必填)');
            $table->string('department', 100)->comment('就诊科室(WS445必填)');
            $table->string('status', 20)->default('draft')->comment('状态: draft/signed/archived');
            $table->timestamp('signed_at')->nullable()->comment('签名时间');
            $table->timestamps();

            $table->index('patient_id');
            $table->index('doctor_id');
            $table->index('diagnosis_code');
            $table->index('visit_date');
        });

        // 药品目录表
        Schema::create('drugs', function (Blueprint $table) {
            $table->id();
            $table->string('name', 200)->comment('药品名称');
            $table->string('generic_name', 200)->nullable()->comment('通用名');
            $table->string('specification', 100)->nullable()->comment('规格');
            $table->string('category', 20)->default('normal')->comment('类别');
            $table->string('unit', 20)->nullable()->comment('单位');
            $table->boolean('is_restricted')->default(false)->comment('是否限制药品');
            $table->boolean('status')->default(true)->comment('状态');
            $table->timestamps();

            $table->index('category');
            $table->index('status');
        });

        // 处方表
        Schema::create('prescriptions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('medical_record_id')->constrained()->cascadeOnDelete();
            $table->foreignId('doctor_id')->constrained()->cascadeOnDelete();
            $table->foreignId('pharmacist_id')->nullable()->constrained('doctors')->nullOnDelete();
            $table->string('status', 20)->default('pending')->comment('状态');
            $table->string('type', 20)->default('normal')->comment('处方类型');
            $table->date('valid_until')->comment('有效期');
            $table->text('reject_reason')->nullable()->comment('退回原因');
            $table->timestamp('reviewed_at')->nullable()->comment('审核时间');
            $table->timestamps();

            $table->index('medical_record_id');
            $table->index('doctor_id');
            $table->index('pharmacist_id');
            $table->index('status');
        });

        // 处方明细表
        Schema::create('prescription_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('prescription_id')->constrained()->cascadeOnDelete();
            $table->foreignId('drug_id')->constrained()->cascadeOnDelete();
            $table->string('dosage', 50)->comment('剂量');
            $table->string('frequency', 50)->comment('频次');
            $table->unsignedInteger('days')->comment('天数');
            $table->string('usage_method', 50)->nullable()->comment('用法');
            $table->string('remark', 500)->nullable()->comment('备注');

            $table->index('prescription_id');
            $table->index('drug_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('prescription_items');
        Schema::dropIfExists('prescriptions');
        Schema::dropIfExists('drugs');
        Schema::dropIfExists('medical_records');
        Schema::dropIfExists('messages');
        Schema::dropIfExists('consultations');
        Schema::dropIfExists('appointments');
        Schema::dropIfExists('schedules');
        Schema::dropIfExists('patients');
        Schema::dropIfExists('doctors');
        Schema::dropIfExists('departments');
    }
};
