<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * No persisted `status` column, unlike rooms — a table's occupied/reserved/
 * available state is computed per date+time from restaurant_reservations
 * (and, for "right now", from open pos_orders), not stored, because it's
 * only ever meaningful relative to a specific date/time being viewed (see
 * RestaurantReservationController::floorPlan()).
 */
class CreateDiningTablesTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'table_number' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
            ],
            'dining_area_id' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
            ],
            'seating_capacity' => [
                'type'       => 'INT',
                'constraint' => 5,
                'unsigned'   => true,
                'default'    => 4,
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('table_number');
        $this->forge->addKey('dining_area_id');
        $this->forge->addForeignKey('dining_area_id', 'dining_areas', 'id', '', 'RESTRICT');
        $this->forge->createTable('dining_tables');
    }

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