<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * `status` is VARCHAR, not a native SQL ENUM, even though Phase 1 only
 * ever writes clean/dirty/occupied/out_of_service (see App\Config\RoomStatus
 * for the authoritative list used by validation and the UI). Kept
 * consistent with how booking status and folio line-item category are
 * stored, so a future status (e.g. a maintenance hold) is a config change,
 * not an ALTER TABLE against every tenant database.
 */
class CreateRoomsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'room_number' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
            ],
            'room_type_id' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
            ],
            'floor' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'null'       => true,
            ],
            'status' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'clean',
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('room_number');
        $this->forge->addKey('room_type_id');
        $this->forge->addKey('status');
        $this->forge->addForeignKey('room_type_id', 'room_types', 'id', '', 'RESTRICT');
        $this->forge->createTable('rooms');
    }

    public function down()
    {
        $this->forge->dropTable('rooms', true);
    }
}
