<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * `phone` is stored already normalized to +233XXXXXXXXX (see
 * App\Libraries\PhoneNumber) — normalization happens at write time in
 * GuestModel, not just at display time, so this column is safe to match
 * against directly for dedup checks and later for WhatsApp/SMS sending.
 *
 * No DB-level unique constraint on phone or id_number: Phase 1 only warns
 * on a probable duplicate (see GuestModel::findPossibleDuplicates) rather
 * than hard-blocking, since two real guests can legitimately share a
 * household phone number. Flagged as an assumption in the Phase 1 summary.
 */
class CreateGuestsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'full_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'phone' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'comment'    => 'Normalized to +233XXXXXXXXX at write time',
            ],
            'email' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
                'null'       => true,
            ],
            'id_type' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'null'       => true,
                'comment'    => 'ghana_card, passport, other',
            ],
            'id_number' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
                'null'       => true,
            ],
            'nationality' => [
                'type'       => 'VARCHAR',
                'constraint' => 80,
                'null'       => true,
            ],
            'address' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'date_of_birth' => [
                'type' => 'DATE',
                'null' => true,
            ],
            '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('phone');
        $this->forge->addKey('id_number');
        $this->forge->addKey('full_name');
        $this->forge->createTable('guests');
    }

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