<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Events (see CreateEventsTable) track only the one paying client (a
 * guest or an event_client) who booked the venue — never the individual
 * people who actually attend. This is the first table for that: one row
 * per attendee who registered online for an event, via
 * EventRegistrationController (public, reached through the event's
 * public_token link — see AddPublicTokenToEventsTable).
 *
 * registration_code is unique per event, not globally (composite index
 * below) — the self-check-in kiosk always knows which event it's
 * checking into from the URL, so a code only ever needs to be
 * unambiguous within that one event's roster, same reasoning
 * PasswordResetCodeModel uses for per-user rather than global
 * uniqueness. guest_id is nullable (an attendee may or may not already
 * be a guest in the CRM) — same nullable-FK pattern email_log.guest_id
 * already uses for "this person may not be a tracked guest yet".
 */
class CreateEventRegistrationsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'event_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'guest_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'full_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'phone' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
            ],
            'email' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
                'null'       => true,
            ],
            'registration_code' => [
                'type'       => 'VARCHAR',
                'constraint' => 6,
            ],
            'status' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'registered',
                'comment'    => 'registered, checked_in, cancelled',
            ],
            'checked_in_at' => [
                'type' => 'DATETIME',
                '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->addUniqueKey(['event_id', 'registration_code']);
        // event_id is a plain column here (not addKey'd separately) since
        // the composite unique index above already covers lookups by
        // event_id alone as its leftmost column.
        $this->forge->addForeignKey('event_id', 'events', 'id', '', 'CASCADE');
        $this->forge->addForeignKey('guest_id', 'guests', 'id', '', 'SET NULL');
        $this->forge->createTable('event_registrations');
    }

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