<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Maintenance module (was a disabled "Soon" nav item — now built for
 * real). `asset` is a plain free-text label ("AC Unit — Room 205",
 * "Lobby HVAC") rather than a full asset registry — the mockup implied
 * one (a "Total Assets" count, an asset picker), but a real fixed-asset
 * register (acquisition dates, depreciation, service history per asset)
 * is materially bigger scope than "note what broke and where" needs.
 * `status=overdue` is never stored — it's open/in_progress work whose
 * due_date has passed, computed at query time (see MaintenanceModel) so
 * it can never silently go stale the way a stored flag would.
 */
class CreateMaintenanceWorkOrdersTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'title' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'asset' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
                'null'       => true,
            ],
            'room_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'priority' => [
                'type'       => 'VARCHAR',
                'constraint' => 10,
                'default'    => 'medium',
            ],
            'status' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'open',
                'comment'    => 'open|in_progress|completed',
            ],
            'reported_by' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'assigned_to' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'due_date' => [
                'type' => 'DATE',
                'null' => true,
            ],
            'completed_at' => [
                'type' => 'DATETIME',
                '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('status');
        $this->forge->addForeignKey('room_id', 'rooms', 'id', '', 'SET NULL');
        $this->forge->addForeignKey('reported_by', 'users', 'id', '', 'SET NULL');
        $this->forge->addForeignKey('assigned_to', 'users', 'id', '', 'SET NULL');
        $this->forge->createTable('maintenance_work_orders');
    }

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