<?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('kylin_property_residents', function (Blueprint $table) {
            $table->id();
            $table->string('name', 50)->comment('姓名');
            $table->string('room_no', 30)->comment('房号');
            $table->text('phone')->nullable()->comment('手机号（加密）');
            $table->text('id_card')->nullable()->comment('身份证号（加密）');
            $table->string('type', 20)->default('owner')->comment('类型（owner/tenant）');
            $table->string('status', 20)->default('active')->comment('状态');
            $table->timestamps();
            $table->softDeletes();

            $table->unique('room_no');
        });

        // 报修单
        Schema::create('kylin_property_repair_orders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('resident_id')->constrained('kylin_property_residents')->cascadeOnDelete()->comment('住户 ID');
            $table->string('order_no', 30)->unique()->comment('报修单号');
            $table->string('repair_type', 30)->comment('报修类型');
            $table->string('description', 500)->comment('问题描述');
            $table->dateTime('reported_at')->comment('报修时间');
            $table->dateTime('resolved_at')->nullable()->comment('解决时间');
            $table->string('status', 20)->default('pending')->comment('状态');
            $table->timestamps();

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

        // 费用记录
        Schema::create('kylin_property_fee_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('resident_id')->constrained('kylin_property_residents')->cascadeOnDelete()->comment('住户 ID');
            $table->string('bill_no', 30)->unique()->comment('账单编号');
            $table->string('fee_type', 30)->comment('费用类型');
            $table->unsignedInteger('amount')->comment('金额（分）');
            $table->dateTime('due_at')->comment('缴费截止日期');
            $table->dateTime('paid_at')->nullable()->comment('缴费日期');
            $table->string('status', 20)->default('pending')->comment('状态');
            $table->timestamps();

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

        // 访客记录
        Schema::create('kylin_property_visitor_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('resident_id')->constrained('kylin_property_residents')->cascadeOnDelete()->comment('住户 ID');
            $table->string('visitor_name', 50)->comment('访客姓名');
            $table->text('visitor_phone')->nullable()->comment('访客手机号（加密）');
            $table->string('visitor_type', 30)->comment('访客类型');
            $table->dateTime('visit_at')->comment('访问时间');
            $table->dateTime('leave_at')->nullable()->comment('离开时间');
            $table->string('purpose', 200)->nullable()->comment('访问目的');
            $table->timestamps();

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

    public function down(): void
    {
        Schema::dropIfExists('kylin_property_visitor_records');
        Schema::dropIfExists('kylin_property_fee_records');
        Schema::dropIfExists('kylin_property_repair_orders');
        Schema::dropIfExists('kylin_property_residents');
    }
};