<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Join table: a rate plan can apply to more than one room type ("which
 * room type(s)" — Phase 3 brief), so this is a proper many-to-many rather
 * than a single nullable room_type_id on rate_plans.
 */
class CreateRatePlanRoomTypesTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'rate_plan_id' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
            ],
            'room_type_id' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['rate_plan_id', 'room_type_id'], 'idx_unique_plan_type');
        $this->forge->addForeignKey('rate_plan_id', 'rate_plans', 'id', '', 'CASCADE');
        $this->forge->addForeignKey('room_type_id', 'room_types', 'id', '', 'CASCADE');
        $this->forge->createTable('rate_plan_room_types');
    }

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