<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Lets the New Booking wizard tag a corporate account at booking time
 * (Reservation Details step) — independent of folios.corporate_account_id
 * (which tags a folio after the fact, e.g. for a walk-in with no
 * booking). CheckInController copies this onto the folio the moment one
 * actually opens, same "staged on the booking until check-in activates
 * it" pattern already used for booking_service_items and
 * initial_payment_amount.
 */
class AddCorporateAccountToBookings extends Migration
{
    public function up()
    {
        $this->forge->addColumn('bookings', [
            'corporate_account_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
                'after'      => 'source',
            ],
        ]);

        $this->forge->addForeignKey('corporate_account_id', 'corporate_accounts', 'id', '', 'SET NULL');
        $this->forge->processIndexes('bookings');
    }

    public function down()
    {
        $this->forge->dropForeignKey('bookings', 'bookings_corporate_account_id_foreign');
        $this->forge->dropColumn('bookings', 'corporate_account_id');
    }
}
