<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * What a guest/staff picked in the New Booking wizard's Add Services step
 * — deliberately NOT posted straight to folio_line_items, because no
 * folio exists until check-in (see CheckInController's class doc
 * comment). These rows are the pending record of what was selected;
 * CheckInController::postNightlyCharges() (renamed/extended) converts
 * them into real folio_line_items the moment the folio actually opens,
 * then they've served their purpose and are left as booking history.
 *
 * service_name/unit_price are snapshotted at selection time rather than
 * always joined live from hotel_services — so editing or deleting a
 * catalog service later never rewrites what a guest was already quoted
 * and agreed to pay.
 */
class CreateBookingServiceItemsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'booking_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'service_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'service_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'unit_price' => [
                'type'       => 'DECIMAL',
                'constraint' => '10,2',
            ],
            'quantity' => [
                'type'       => 'INT',
                'constraint' => 5,
                'default'    => 1,
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('booking_id');
        $this->forge->createTable('booking_service_items');
    }

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