<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Exactly one of `guest_id` / `event_client_id` is set (app-level rule,
 * enforced in EventModel, not a DB constraint — consistent with how the
 * rest of this schema keeps cross-field rules in the model layer). Event
 * history (Phase 2 spec: "show all past events they've held") is just a
 * query filtered by whichever of the two is set — no separate join table.
 */
class CreateEventsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'event_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'event_type' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
                'null'       => true,
                'comment'    => 'e.g. conference, wedding, birthday — free-ish text',
            ],
            'guest_id' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'null'     => true,
            ],
            'event_client_id' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'null'     => true,
            ],
            'event_date' => [
                'type' => 'DATE',
            ],
            'expected_headcount' => [
                'type'       => 'INT',
                'constraint' => 10,
                'unsigned'   => true,
                'null'       => true,
            ],
            'venue_space' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
                'null'       => true,
            ],
            'status' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'inquiry',
                'comment'    => 'inquiry, confirmed, completed, cancelled',
            ],
            'contract_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('event_client_id');
        $this->forge->addKey('event_date');
        $this->forge->addKey('status');
        $this->forge->addForeignKey('guest_id', 'guests', 'id', '', 'SET NULL');
        $this->forge->addForeignKey('event_client_id', 'event_clients', 'id', '', 'SET NULL');
        $this->forge->createTable('events');
    }

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