<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Admin/organizer-configurable extra questions on an event's public
 * registration form — the built-in full_name/phone/email on
 * event_registrations aren't always enough (e.g. "Dietary restrictions",
 * "T-shirt size"). One row per question, scoped to one event (a field
 * with no event is meaningless, hence CASCADE). `options` is JSON-encoded
 * (only meaningful for select/radio/checkbox), same TEXT-storing-JSON
 * convention as room_types.amenities/campaigns.audience_filter — no real
 * MySQL JSON column type is used anywhere in this schema.
 */
class CreateEventRegistrationFieldsTable 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,
            ],
            'label' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'field_type' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'comment'    => 'text, textarea, select, radio, checkbox, number, date',
            ],
            'options' => [
                'type'    => 'TEXT',
                'null'    => true,
                'comment' => 'JSON-encoded list of choice strings — only meaningful for select/radio/checkbox',
            ],
            'is_required' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'default'    => 0,
            ],
            'sort_order' => [
                'type'       => 'INT',
                'constraint' => 11,
                'default'    => 0,
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('event_id');
        $this->forge->addForeignKey('event_id', 'events', 'id', '', 'CASCADE');
        $this->forge->createTable('event_registration_fields');
    }

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