<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * The negotiated-rate half of "corporate account management — negotiated
 * rates, dining accounts, monthly statement billing." Optional: a company
 * with no special rate still works exactly as before (folio-level billing
 * only). When set, BookingWizardController resolves the room rate through
 * this specific plan instead of the generic date-matched one — see
 * RatePlanModel::resolveWithPlan().
 */
class AddDefaultRatePlanToCorporateAccounts extends Migration
{
    public function up()
    {
        $this->forge->addColumn('corporate_accounts', [
            'default_rate_plan_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
                'after'      => 'email',
            ],
        ]);

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

    public function down()
    {
        $this->forge->dropForeignKey('corporate_accounts', 'corporate_accounts_default_rate_plan_id_foreign');
        $this->forge->dropColumn('corporate_accounts', 'default_rate_plan_id');
    }
}
