<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Housekeeping module (was a disabled "Soon" nav item — user-supplied
 * mockups asked for it to be built for real). A task is the workflow
 * that gets a room from `dirty` to `clean` (see RoomModel's roomStatuses)
 * — completing a task doesn't just record that cleaning happened, it
 * also updates the room's own status (see HousekeepingController), so
 * the existing Room Management board and this module never disagree
 * about a room's current state.
 */
class CreateHousekeepingTasksTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'room_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'task_type' => [
                'type'       => 'VARCHAR',
                'constraint' => 30,
                'comment'    => 'standard_cleaning|deep_cleaning|turn_down|vacate_cleaning — validated against Config\Lookups.',
            ],
            'priority' => [
                'type'       => 'VARCHAR',
                'constraint' => 10,
                'default'    => 'medium',
            ],
            'status' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'not_started',
                'comment'    => 'not_started|in_progress|completed',
            ],
            'assigned_to' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'start_time' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'completed_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'inspected_at' => [
                'type'    => 'DATETIME',
                'null'    => true,
                'comment' => 'Manager sign-off after completion — separate from completed_at so "cleaned" and "verified clean" aren\'t conflated.',
            ],
            'inspected_by' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'notes' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('room_id');
        $this->forge->addKey('status');
        $this->forge->addForeignKey('room_id', 'rooms', 'id', '', 'CASCADE');
        $this->forge->addForeignKey('assigned_to', 'users', 'id', '', 'SET NULL');
        $this->forge->addForeignKey('inspected_by', 'users', 'id', '', 'SET NULL');
        $this->forge->createTable('housekeeping_tasks');
    }

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