<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * A rate plan is a date-range rule, not an occupancy-based algorithm
 * (explicitly out of scope per the Phase 3 brief). `adjustment_type` flat
 * means `adjustment_value` overrides the room type's base_rate outright;
 * percentage means base_rate is adjusted by that percent (positive = markup,
 * negative = discount).
 */
class CreateRatePlansTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'name' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'date_start' => [
                'type' => 'DATE',
            ],
            'date_end' => [
                'type' => 'DATE',
            ],
            'adjustment_type' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'comment'    => 'flat, percentage',
            ],
            'adjustment_value' => [
                'type'       => 'DECIMAL',
                'constraint' => '10,2',
                'comment'    => 'flat: GHS override rate. percentage: e.g. -10 for 10% off, 15 for 15% markup',
            ],
            'is_active' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'default'    => 1,
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey(['date_start', 'date_end'], false, false, 'idx_rate_plan_dates');
        $this->forge->createTable('rate_plans');
    }

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