<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Real structured guest preferences — deliberately not built earlier
 * (see AddSegmentAndBlacklistToGuests's doc comment, which left this to
 * the free-text `notes` field). One row per guest (unique guest_id) —
 * GuestPreferenceModel::saveForGuest() upserts, so this is never a
 * one-to-many history, just "what's currently on file." Fields are
 * mostly free text rather than closed lookups (bed type, dietary,
 * seating, allergies genuinely vary too much to enumerate), except
 * smoking_preference which is a real fixed set (Config\Lookups).
 */
class CreateGuestPreferencesTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'guest_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'room_type_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'bed_type' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
                'null'       => true,
            ],
            'dietary_restrictions' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => true,
            ],
            'seating_preference' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
                'null'       => true,
            ],
            'allergies' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => true,
            ],
            'smoking_preference' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'null'       => true,
                'comment'    => 'smoking|non_smoking — validated against Config\Lookups. Null = no preference on file.',
            ],
            'other_notes' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => true,
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('guest_id');
        $this->forge->addForeignKey('guest_id', 'guests', 'id', '', 'CASCADE');
        $this->forge->addForeignKey('room_type_id', 'room_types', 'id', '', 'SET NULL');
        $this->forge->createTable('guest_preferences');
    }

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