<?php

declare(strict_types=1);

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

/**
 * 健身管理系统数据库迁移
 *
 * 依据《体育法》第八十九条、第九十条和《消费者权益保护法》第五十三条，
 * 以及 DB11/T 2277-2024 健身房服务规范、GB/T 35273-2020 个人信息安全规范。
 */
return new class extends Migration
{
    public function up(): void
    {
        // 健身会员表
        Schema::create('kylin_fitness_members', function (Blueprint $table) {
            $table->id();
            $table->string('name', 50)->comment('姓名');
            $table->string('phone', 20)->unique()->comment('手机号');
            $table->string('gender', 10)->nullable()->comment('性别');
            $table->date('birthday')->nullable()->comment('出生日期');
            $table->text('id_card')->nullable()->comment('身份证号（加密存储）');
            $table->string('member_no', 50)->unique()->comment('会员编号');
            $table->string('member_type', 30)->default('regular')->comment('会员类型');
            $table->string('status', 20)->default('active')->comment('状态');
            $table->dateTime('joined_at')->nullable()->comment('入会日期');
            $table->dateTime('expired_at')->nullable()->comment('会员到期日');
            $table->timestamps();
            $table->softDeletes();
        });

        // 体测数据表
        Schema::create('kylin_fitness_tests', function (Blueprint $table) {
            $table->id();
            $table->foreignId('member_id')->constrained('kylin_fitness_members')->cascadeOnDelete()->comment('会员 ID');
            $table->date('test_date')->comment('测试日期');
            $table->decimal('height', 6, 2)->nullable()->comment('身高 (cm)');
            $table->decimal('weight', 6, 2)->nullable()->comment('体重 (kg)');
            $table->decimal('body_fat_rate', 5, 2)->nullable()->comment('体脂率 (%)');
            $table->decimal('muscle_mass', 6, 2)->nullable()->comment('肌肉量 (kg)');
            $table->decimal('basal_metabolic_rate', 8, 2)->nullable()->comment('基础代谢率 (kcal)');
            $table->tinyInteger('visceral_fat_level')->nullable()->comment('内脏脂肪等级');
            $table->decimal('bmi', 5, 2)->nullable()->comment('身体质量指数');
            $table->decimal('body_water', 6, 2)->nullable()->comment('身体水分量 (kg)');
            $table->decimal('bone_mass', 5, 2)->nullable()->comment('骨量 (kg)');
            $table->string('data_source', 30)->default('manual')->comment('数据来源');
            $table->string('device_model', 50)->nullable()->comment('设备型号');
            $table->text('raw_data')->nullable()->comment('原始数据（加密存储）');
            $table->timestamps();

            $table->index('test_date');
            $table->index(['member_id', 'test_date']);
        });

        // 健身课程表
        Schema::create('kylin_fitness_courses', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100)->comment('课程名称');
            $table->string('category', 30)->comment('课程分类');
            $table->text('description')->nullable()->comment('课程描述');
            $table->unsignedInteger('capacity')->default(20)->comment('课程容量');
            $table->unsignedInteger('duration_minutes')->default(60)->comment('时长（分钟）');
            $table->string('difficulty', 20)->default('beginner')->comment('难度等级');
            $table->string('status', 20)->default('active')->comment('状态');
            $table->timestamps();
            $table->softDeletes();
        });

        // 教练表
        Schema::create('kylin_fitness_coaches', function (Blueprint $table) {
            $table->id();
            $table->string('name', 50)->comment('姓名');
            $table->string('phone', 20)->unique()->comment('手机号');
            $table->string('gender', 10)->nullable()->comment('性别');
            $table->string('specialty', 100)->nullable()->comment('专长');
            $table->date('certification_expire_at')->nullable()->comment('资质到期日');
            $table->string('certification_no', 50)->nullable()->comment('资质编号');
            $table->string('status', 20)->default('active')->comment('状态');
            $table->text('bio')->nullable()->comment('简介');
            $table->timestamps();
            $table->softDeletes();
        });

        // 课程预约表
        Schema::create('kylin_fitness_bookings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('course_id')->constrained('kylin_fitness_courses')->cascadeOnDelete()->comment('课程 ID');
            $table->foreignId('member_id')->constrained('kylin_fitness_members')->cascadeOnDelete()->comment('会员 ID');
            $table->foreignId('coach_id')->nullable()->constrained('kylin_fitness_coaches')->nullOnDelete()->comment('教练 ID');
            $table->dateTime('start_time')->comment('开始时间');
            $table->dateTime('end_time')->comment('结束时间');
            $table->string('status', 20)->default('booked')->comment('状态');
            $table->dateTime('cancelled_at')->nullable()->comment('取消时间');
            $table->string('cancel_reason')->nullable()->comment('取消原因');
            $table->timestamps();

            $table->index('start_time');
            $table->index(['member_id', 'start_time']);
        });

        // 退费记录表
        Schema::create('kylin_fitness_refunds', function (Blueprint $table) {
            $table->id();
            $table->foreignId('member_id')->constrained('kylin_fitness_members')->cascadeOnDelete()->comment('会员 ID');
            $table->string('refund_no', 50)->unique()->comment('退费单号');
            $table->decimal('amount', 10, 2)->comment('退费金额');
            $table->decimal('service_fee', 10, 2)->default(0)->comment('手续费');
            $table->string('reason')->nullable()->comment('退费原因');
            $table->string('status', 20)->default('pending')->comment('状态');
            $table->dateTime('processed_at')->nullable()->comment('处理时间');
            $table->string('processor')->nullable()->comment('处理人');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('kylin_fitness_refunds');
        Schema::dropIfExists('kylin_fitness_bookings');
        Schema::dropIfExists('kylin_fitness_coaches');
        Schema::dropIfExists('kylin_fitness_courses');
        Schema::dropIfExists('kylin_fitness_tests');
        Schema::dropIfExists('kylin_fitness_members');
    }
};