<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Group/bulk bookings — an organization or informal group (a conference,
 * a wedding party) reserving several rooms together for the same stay.
 * One booking_groups row is the umbrella; each individual room is still
 * a completely normal row in `bookings` (own guest, own folio, own
 * check-in/check-out lifecycle) — this only adds an optional
 * booking_group_id link so those rooms can be found/managed together.
 * Every existing booking query that doesn't know about groups keeps
 * working exactly as before; booking_group_id is null for every booking
 * made through the regular single-room wizard.
 *
 * Billing: if corporate_account_id is set, every room's folio is routed
 * to that account the same way an individual corporate booking already
 * is (FolioModel::setCorporateAccount) — no new billing mechanism.
 * organizer_name/phone/email exist for a group with no registered
 * corporate account (a wedding party, a family reunion) — purely
 * informational in that case, billing stays per-guest as normal.
 */
class CreateBookingGroupsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'group_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'corporate_account_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'organizer_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
                'null'       => true,
            ],
            'organizer_phone' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'null'       => true,
            ],
            'organizer_email' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
                'null'       => true,
            ],
            'check_in_date' => [
                'type' => 'DATE',
            ],
            'check_out_date' => [
                'type' => 'DATE',
            ],
            'status' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'confirmed',
                'comment'    => 'confirmed, cancelled',
            ],
            'notes' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'created_by' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('status');
        $this->forge->addForeignKey('corporate_account_id', 'corporate_accounts', 'id', '', 'SET NULL');
        $this->forge->addForeignKey('created_by', 'users', 'id', '', 'SET NULL');
        $this->forge->createTable('booking_groups');

        $this->forge->addColumn('bookings', [
            'booking_group_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
                'after'      => 'corporate_account_id',
            ],
        ]);

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

    public function down()
    {
        $this->forge->dropForeignKey('bookings', 'bookings_booking_group_id_foreign');
        $this->forge->dropColumn('bookings', 'booking_group_id');
        $this->forge->dropTable('booking_groups', true);
    }
}
