<?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_library_books', function (Blueprint $table) {
            $table->id();
            $table->string('isbn', 20)->unique()->comment('ISBN 号');
            $table->string('title', 200)->comment('书名');
            $table->string('author', 100)->comment('作者');
            $table->string('publisher', 100)->nullable()->comment('出版社');
            $table->date('publish_date')->nullable()->comment('出版日期');
            $table->string('category', 50)->nullable()->comment('分类');
            $table->string('call_number', 50)->nullable()->comment('索书号');
            $table->unsignedInteger('total_copies')->default(1)->comment('总馆藏数');
            $table->unsignedInteger('available_copies')->default(1)->comment('可借数');
            $table->string('status', 20)->default('available')->comment('状态');
            $table->timestamps();
            $table->softDeletes();

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

        // 读者表
        Schema::create('kylin_library_readers', function (Blueprint $table) {
            $table->id();
            $table->string('name', 50)->comment('姓名');
            $table->string('reader_no', 50)->unique()->comment('借阅证号');
            $table->string('reader_type', 30)->default('undergraduate')->comment('读者类型');
            $table->text('phone')->nullable()->comment('手机号（加密存储）');
            $table->string('email', 100)->nullable()->comment('邮箱');
            $table->text('id_card')->nullable()->comment('身份证号（加密存储）');
            $table->string('status', 20)->default('active')->comment('状态');
            $table->dateTime('expired_at')->nullable()->comment('借阅证到期日');
            $table->timestamps();
            $table->softDeletes();

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

        // 借阅记录表
        Schema::create('kylin_library_borrow_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('reader_id')->constrained('kylin_library_readers')->cascadeOnDelete()->comment('读者 ID');
            $table->foreignId('book_id')->constrained('kylin_library_books')->cascadeOnDelete()->comment('图书 ID');
            $table->dateTime('borrowed_at')->comment('借阅日期');
            $table->dateTime('due_at')->comment('应还日期');
            $table->dateTime('returned_at')->nullable()->comment('实际归还日期');
            $table->unsignedTinyInteger('renew_count')->default(0)->comment('续借次数');
            $table->decimal('fine_amount', 8, 2)->default(0)->comment('罚款金额');
            $table->string('status', 20)->default('borrowed')->comment('状态');
            $table->timestamps();

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

        // 座位表
        Schema::create('kylin_library_seats', function (Blueprint $table) {
            $table->id();
            $table->string('seat_no', 20)->comment('座位编号');
            $table->string('area', 50)->comment('区域');
            $table->string('floor', 10)->nullable()->comment('楼层');
            $table->string('status', 20)->default('available')->comment('状态');
            $table->timestamps();

            $table->unique(['seat_no', 'area']);
            $table->index('status');
        });

        // 座位预约表
        Schema::create('kylin_library_seat_reservations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('reader_id')->constrained('kylin_library_readers')->cascadeOnDelete()->comment('读者 ID');
            $table->foreignId('seat_id')->constrained('kylin_library_seats')->cascadeOnDelete()->comment('座位 ID');
            $table->date('reserved_date')->comment('预约日期');
            $table->time('start_time')->comment('开始时间');
            $table->time('end_time')->comment('结束时间');
            $table->dateTime('checked_in_at')->nullable()->comment('签到时间');
            $table->string('status', 20)->default('reserved')->comment('状态');
            $table->timestamps();

            $table->index(['reader_id', 'reserved_date']);
            $table->index(['seat_id', 'reserved_date']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('kylin_library_seat_reservations');
        Schema::dropIfExists('kylin_library_seats');
        Schema::dropIfExists('kylin_library_borrow_records');
        Schema::dropIfExists('kylin_library_readers');
        Schema::dropIfExists('kylin_library_books');
    }
};