<?php

use yii\db\Migration;

/**
 * Creates plans table for subscription and one-time plans.
 */
class m240001_000003_create_plans_table extends Migration
{
    public function safeUp(): void
    {
        $this->createTable('{{%plans}}', [
            'id'              => $this->primaryKey()->unsigned()->comment('Primary key'),
            'name'            => $this->string(150)->notNull()->comment('Plan name'),
            'description'     => $this->text()->null()->comment('Plan description'),
            'duration_months' => $this->tinyInteger()->unsigned()->null()->comment('Duration in months; NULL for one-time'),
            'price'           => $this->decimal(10, 2)->notNull()->defaultValue(0.00)->comment('Plan price in INR'),
            'plan_type'       => "ENUM('subscription','one_time') NOT NULL DEFAULT 'subscription' COMMENT 'Plan billing type'",
            'is_active'       => $this->tinyInteger(1)->unsigned()->notNull()->defaultValue(1)->comment('1=active'),
            'created_at'      => $this->integer()->unsigned()->notNull()->comment('Unix timestamp'),
            'updated_at'      => $this->integer()->unsigned()->notNull()->comment('Unix timestamp'),
        ], 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT="Nutrition plans / subscriptions"');

        $this->createIndex('idx_plans_type', '{{%plans}}', 'plan_type');
        $this->createIndex('idx_plans_active', '{{%plans}}', 'is_active');
    }

    public function safeDown(): void
    {
        $this->dropTable('{{%plans}}');
    }
}
