<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Complaint & service recovery tracking — was entirely missing (see the
 * requirements audit). Workflow mirrors MaintenanceWorkOrderModel's
 * open/in_progress/completed shape as closely as a 4-stage flow allows:
 * open -> assigned -> resolved -> followed_up. `assigned_to` is who's
 * handling it; `reported_by` is which staff member logged it (could be
 * the same person, doesn't have to be). Always tied to a real guest —
 * "who has a complaint on file" only makes sense against a known guest
 * record, same reasoning folios.guest_id being NOT NULL follows.
 */
class CreateComplaintsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'guest_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'category' => [
                'type'       => 'VARCHAR',
                'constraint' => 30,
                'comment'    => 'room|fnb|housekeeping|maintenance|billing|other — validated against Config\Lookups.',
            ],
            'description' => [
                'type' => 'TEXT',
            ],
            'priority' => [
                'type'       => 'VARCHAR',
                'constraint' => 10,
                'default'    => 'medium',
            ],
            'status' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'open',
                'comment'    => 'open|assigned|resolved|followed_up',
            ],
            'assigned_to' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'reported_by' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'resolution_notes' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'resolved_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'follow_up_at' => [
                'type'    => 'DATETIME',
                'null'    => true,
                'comment' => 'When a follow-up contact happened (or is scheduled) after resolution.',
            ],
            'follow_up_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('guest_id');
        $this->forge->addKey('status');
        $this->forge->addForeignKey('guest_id', 'guests', 'id', '', 'CASCADE');
        $this->forge->addForeignKey('assigned_to', 'users', 'id', '', 'SET NULL');
        $this->forge->addForeignKey('reported_by', 'users', 'id', '', 'SET NULL');
        $this->forge->createTable('complaints');
    }

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