<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * A reservation's guest is either an existing `guest_id`, or a walk-in
 * name/phone captured inline — mirrors how Phase 1 bookings capture a
 * guest, but restaurant walk-ins don't force a full Guest record the way
 * a room booking does (a reservation may never become a guest record at
 * all, e.g. someone who never shows up).
 */
class CreateRestaurantReservationsTable 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,
                'null'     => true,
            ],
            'walk_in_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
                'null'       => true,
            ],
            'walk_in_phone' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'null'       => true,
            ],
            'party_size' => [
                'type'       => 'INT',
                'constraint' => 5,
                'unsigned'   => true,
            ],
            'reservation_date' => [
                'type' => 'DATE',
            ],
            'reservation_time' => [
                'type' => 'TIME',
            ],
            'dining_table_id' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'null'     => true,
            ],
            'status' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'booked',
                'comment'    => 'booked, seated, completed, no_show, cancelled',
            ],
            '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(['reservation_date', 'reservation_time'], false, false, 'idx_res_date_time');
        $this->forge->addKey('dining_table_id');
        $this->forge->addForeignKey('guest_id', 'guests', 'id', '', 'SET NULL');
        $this->forge->addForeignKey('dining_table_id', 'dining_tables', 'id', '', 'SET NULL');
        $this->forge->createTable('restaurant_reservations');
    }

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