<?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('room_types', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->string('name', 50);
            $table->decimal('price', 12, 2)->nullable()->comment('日单价');
            $table->text('description')->nullable();
            $table->json('images')->nullable();
            $table->json('facilities')->nullable();
            $table->integer('sort')->default(0);
            $table->tinyInteger('status')->default(1);
            $table->timestamps();
        });

        // 房间
        Schema::create('rooms', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->foreignId('room_type_id')->constrained();
            $table->string('floor', 10)->nullable();
            $table->string('number', 20);
            $table->tinyInteger('status')->default(1)->comment('1空房 2已预定 3入住 4维修 5清洁');
            $table->integer('sort')->default(0);
            $table->timestamps();
            $table->unique(['store_id', 'number']);
        });

        // 预定/入住记录
        Schema::create('reservations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->foreignId('customer_id')->constrained();
            $table->unsignedBigInteger('contract_id')->nullable();
            $table->foreignId('room_id')->constrained();
            $table->date('check_in_date');
            $table->date('check_out_date');
            $table->timestamp('actual_check_in')->nullable();
            $table->timestamp('actual_check_out')->nullable();
            $table->tinyInteger('status')->default(0)->comment('0预定 1已入住 2已退房 3已取消');
            $table->text('remark')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->timestamps();
            $table->index(['room_id', 'check_in_date', 'check_out_date']);
            $table->index('customer_id');
        });

        // 客户外出记录
        Schema::create('customer_outings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('reservation_id')->constrained();
            $table->foreignId('customer_id')->constrained();
            $table->timestamp('out_at');
            $table->timestamp('expected_back_at')->nullable();
            $table->timestamp('actual_back_at')->nullable();
            $table->text('reason')->nullable();
            $table->text('risk_note')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->timestamps();
        });

        // 呼叫记录
        Schema::create('call_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->foreignId('room_id')->constrained();
            $table->unsignedBigInteger('customer_id')->nullable();
            $table->tinyInteger('type')->nullable()->comment('1护理 2清洁 3送水 4其他');
            $table->tinyInteger('status')->default(0)->comment('0待处理 1处理中 2已完成');
            $table->unsignedBigInteger('handler_id')->nullable();
            $table->timestamp('handle_at')->nullable();
            $table->string('remark', 255)->nullable();
            $table->timestamps();
            $table->index(['store_id', 'status']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('call_records');
        Schema::dropIfExists('customer_outings');
        Schema::dropIfExists('reservations');
        Schema::dropIfExists('rooms');
        Schema::dropIfExists('room_types');
    }
};
