<?php

declare(strict_types=1);

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

/**
 * 宠物管理系统数据库迁移
 *
 * 依据《动物防疫法》第十七条：强制免疫义务。
 * 依据 GB/T 45204-2024：寄养消毒记录保留 ≥ 730 天。
 * 依据 GB/T 43825-2024：疫苗批号可追溯。
 * 依据 GB/T 45295-2025：BCS 体况评分 1-9 分制。
 */
return new class extends Migration
{
    public function up(): void
    {
        // 宠物
        Schema::create('kylin_pet_pets', function (Blueprint $table) {
            $table->id();
            $table->string('name', 50)->comment('宠物名称');
            $table->string('species', 20)->comment('物种');
            $table->string('breed', 50)->nullable()->comment('品种');
            $table->unsignedTinyInteger('age')->nullable()->comment('年龄');
            $table->decimal('weight', 5, 2)->nullable()->comment('体重（kg）');
            $table->unsignedTinyInteger('bcs_score')->nullable()->comment('BCS 体况评分（1-9）');
            $table->string('owner_name', 50)->comment('主人姓名');
            $table->text('owner_phone')->nullable()->comment('主人手机号（加密）');
            $table->string('status', 20)->default('healthy')->comment('状态');
            $table->timestamps();
            $table->softDeletes();

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

        // 诊疗记录
        Schema::create('kylin_pet_medical_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('pet_id')->constrained('kylin_pet_pets')->cascadeOnDelete()->comment('宠物 ID');
            $table->date('visit_date')->comment('就诊日期');
            $table->string('diagnosis', 500)->nullable()->comment('诊断');
            $table->string('treatment', 500)->nullable()->comment('治疗方案');
            $table->string('doctor_name', 50)->nullable()->comment('医生姓名');
            $table->string('clinic_name', 100)->nullable()->comment('诊所名称');
            $table->text('notes')->nullable()->comment('备注');
            $table->timestamps();

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

        // 疫苗记录
        Schema::create('kylin_pet_vaccines', function (Blueprint $table) {
            $table->id();
            $table->foreignId('pet_id')->constrained('kylin_pet_pets')->cascadeOnDelete()->comment('宠物 ID');
            $table->string('vaccine_name', 100)->comment('疫苗名称');
            $table->string('batch_no', 50)->comment('疫苗批号');
            $table->date('injected_at')->comment('接种日期');
            $table->date('next_due_at')->nullable()->comment('下次到期日期');
            $table->string('clinic_name', 100)->nullable()->comment('诊所名称');
            $table->timestamps();

            $table->index('next_due_at');
            $table->index('batch_no');
        });

        // 寄养记录
        Schema::create('kylin_pet_boarding', function (Blueprint $table) {
            $table->id();
            $table->foreignId('pet_id')->constrained('kylin_pet_pets')->cascadeOnDelete()->comment('宠物 ID');
            $table->dateTime('check_in_at')->comment('入住时间');
            $table->dateTime('check_out_at')->nullable()->comment('退房时间');
            $table->string('zone', 20)->default('healthy')->comment('分区（healthy/observation/isolation）');
            $table->text('health_check')->nullable()->comment('健康检查记录');
            $table->text('daily_log')->nullable()->comment('每日照护日志（加密）');
            $table->string('status', 20)->default('boarding')->comment('状态');
            $table->timestamps();

            $table->index(['zone', 'status']);
        });

        // 消毒记录
        Schema::create('kylin_pet_disinfection_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('boarding_id')->constrained('kylin_pet_boarding')->cascadeOnDelete()->comment('寄养记录 ID');
            $table->dateTime('disinfected_at')->comment('消毒时间');
            $table->string('disinfectant', 100)->comment('消毒剂名称');
            $table->string('operator', 50)->nullable()->comment('操作人');
            $table->string('area', 100)->nullable()->comment('消毒区域');
            $table->timestamps();

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

    public function down(): void
    {
        Schema::dropIfExists('kylin_pet_disinfection_logs');
        Schema::dropIfExists('kylin_pet_boarding');
        Schema::dropIfExists('kylin_pet_vaccines');
        Schema::dropIfExists('kylin_pet_medical_records');
        Schema::dropIfExists('kylin_pet_pets');
    }
};