<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Control DB. Subscription tier definitions the platform sells to hotels
 * (e.g. "Starter", "Growth", "Enterprise") — not to be confused with a
 * tenant's own `rate_plans` table (Phase 3, guest room pricing). Editing
 * a plan here never rewrites existing `subscriptions` rows — a tenant's
 * current price is whatever their subscription was created/renewed at,
 * same reasoning as rate plans not retroactively changing booked rates.
 */
class CreatePlansTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'name' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
            ],
            'slug' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
            ],
            'price' => [
                'type'       => 'DECIMAL',
                'constraint' => '10,2',
            ],
            'billing_interval' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'monthly',
                'comment'    => 'monthly|yearly — validated in PlanModel against Config\Lookups, not a DB enum, same convention as every other status/category field in this app.',
            ],
            'max_rooms' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
                'comment'    => 'NULL = unlimited rooms on this plan.',
            ],
            'description' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'features' => [
                'type'    => 'TEXT',
                'null'    => true,
                'comment' => 'JSON-encoded array of feature-bullet strings shown on the pricing card.',
            ],
            'is_active' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'default'    => 1,
                'comment'    => 'Inactive plans stay visible on existing subscriptions but cannot be newly assigned.',
            ],
            'sort_order' => [
                'type'       => 'INT',
                'constraint' => 11,
                'default'    => 0,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('slug');
        $this->forge->createTable('plans');
    }

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