<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Public registration/check-in URLs (event-register/{token},
 * event-checkin/{token}) use this instead of the sequential `events.id`
 * — exposing the raw auto-increment publicly would let anyone enumerate
 * every event at this hotel, including ones still `status='inquiry'`
 * that were never meant to be public. Nullable/no backfill: generated
 * lazily on first need via EventModel::ensurePublicToken(), called from
 * EventController::show() — existing events simply get one the next
 * time a staff member opens their page, no migration-time backfill
 * required.
 */
class AddPublicTokenToEventsTable extends Migration
{
    public function up()
    {
        $this->forge->addColumn('events', [
            'public_token' => [
                'type'       => 'VARCHAR',
                'constraint' => 32,
                'null'       => true,
                'after'      => 'id',
            ],
        ]);
        $this->forge->addUniqueKey('public_token', 'events_public_token_unique');
        $this->forge->processIndexes('events');
    }

    public function down()
    {
        $this->forge->dropColumn('events', 'public_token');
    }
}
