<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * A third auth realm alongside staff (users) and Super Admin
 * (control DB's admin_users) — external clients who log in to manage
 * their own assigned event(s), not hotel staff. Deliberately its own
 * table rather than bolted onto `guests` or `event_clients`: neither has
 * a unique/validated email or a password_hash/is_active shape, and both
 * already represent something else (a CRM record / a billing contact
 * card). Mirrors `users`' exact shape (email unique+required,
 * password_hash, is_active) since that's the closest real precedent for
 * "a login". `password_hash` is nullable — null until the organizer
 * completes the invite/set-password flow (see
 * EventOrganizerPasswordController).
 */
class CreateEventOrganizersTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'full_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'email' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'phone' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'null'       => true,
            ],
            'password_hash' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => true,
            ],
            'is_active' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'default'    => 1,
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('email');
        $this->forge->createTable('event_organizers');
    }

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