<?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_profiles', function (Blueprint $table) {
            $table->id();
            $table->foreignId('customer_id')->constrained();
            $table->unsignedBigInteger('reservation_id')->nullable();
            $table->tinyInteger('type')->comment('1妈妈 2宝宝');
            $table->string('baby_name', 50)->nullable();
            $table->tinyInteger('baby_gender')->nullable()->comment('1男 2女');
            $table->dateTime('baby_birthday')->nullable();
            $table->decimal('birth_weight', 5, 2)->nullable();
            $table->tinyInteger('birth_method')->nullable()->comment('1顺产 2剖宫产');
            $table->text('allergies')->nullable();
            $table->text('medical_history')->nullable();
            $table->tinyInteger('risk_level')->default(0)->comment('0正常 1低风险 2高风险');
            $table->json('assessment')->nullable();
            $table->timestamps();
            $table->index('customer_id');
        });

        // 护理计划
        Schema::create('care_plans', function (Blueprint $table) {
            $table->id();
            $table->foreignId('care_profile_id')->constrained()->onDelete('cascade');
            $table->date('plan_date');
            $table->string('stage', 50)->nullable()->comment('阶段：产后第X天');
            $table->json('items')->nullable();
            $table->unsignedBigInteger('nurse_id')->nullable();
            $table->tinyInteger('status')->default(0)->comment('0待执行 1执行中 2已完成');
            $table->timestamps();
            $table->index(['care_profile_id', 'plan_date']);
        });

        // 护理记录
        Schema::create('care_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('care_profile_id')->constrained()->onDelete('cascade');
            $table->unsignedBigInteger('care_plan_id')->nullable();
            $table->tinyInteger('type')->nullable()->comment('1妈妈护理 2宝宝护理');
            $table->json('items')->nullable();
            $table->text('remark')->nullable();
            $table->json('images')->nullable();
            $table->unsignedBigInteger('nurse_id');
            $table->timestamp('recorded_at');
            $table->timestamps();
            $table->index('care_profile_id');
            $table->index('recorded_at');
        });

        // 护理异常
        Schema::create('care_exceptions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('care_profile_id')->constrained()->onDelete('cascade');
            $table->unsignedBigInteger('reporter_id');
            $table->tinyInteger('level')->comment('1一般 2重要 3紧急');
            $table->text('description');
            $table->json('images')->nullable();
            $table->tinyInteger('status')->default(0)->comment('0上报 1处理中 2已解决');
            $table->unsignedBigInteger('handler_id')->nullable();
            $table->text('handle_result')->nullable();
            $table->timestamp('handle_at')->nullable();
            $table->timestamps();
            $table->index('care_profile_id');
        });

        // 健康指标记录
        Schema::create('health_metrics', function (Blueprint $table) {
            $table->id();
            $table->foreignId('care_profile_id')->constrained()->onDelete('cascade');
            $table->string('metric_type', 30)->comment('temperature/weight/jaundice/blood_pressure');
            $table->decimal('value', 8, 2);
            $table->string('unit', 10)->nullable();
            $table->timestamp('recorded_at');
            $table->unsignedBigInteger('recorder_id')->nullable();
            $table->string('remark', 255)->nullable();
            $table->timestamp('created_at')->nullable();
            $table->index(['care_profile_id', 'metric_type']);
            $table->index('recorded_at');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('health_metrics');
        Schema::dropIfExists('care_exceptions');
        Schema::dropIfExists('care_records');
        Schema::dropIfExists('care_plans');
        Schema::dropIfExists('care_profiles');
    }
};
