<?php

declare(strict_types=1);

namespace common\models;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * Plan ActiveRecord model.
 *
 * @property int         $id
 * @property string      $name
 * @property string|null $description
 * @property string|null $who_it_for
 * @property string|null $features
 * @property string|null $icon
 * @property int         $sort_order
 * @property int|null    $duration_months
 * @property float       $price
 * @property string      $plan_type  subscription|one_time
 * @property int         $is_active
 * @property int         $created_at
 * @property int         $updated_at
 *
 * @property CustomerPlan[] $customerPlans
 */
class Plan extends ActiveRecord
{
    public const TYPE_SUBSCRIPTION = 'subscription';
    public const TYPE_ONE_TIME     = 'one_time';

    public static function tableName(): string
    {
        return '{{%plans}}';
    }

    public function behaviors(): array
    {
        return [
            TimestampBehavior::class,
        ];
    }

    public function rules(): array
    {
        return [
            [['name', 'plan_type'], 'required'],
            [['name'], 'string', 'max' => 150],
            [['who_it_for'], 'string', 'max' => 255],
            [['icon'], 'string', 'max' => 60],
            [['description', 'features'], 'string'],
            [['price'], 'number', 'min' => 0],
            [['price'], 'default', 'value' => 0],
            [['sort_order'], 'integer', 'min' => 0, 'max' => 255],
            [['sort_order'], 'default', 'value' => 0],
            [['duration_months'], 'integer', 'min' => 1, 'max' => 120],
            [['plan_type'], 'in', 'range' => [self::TYPE_SUBSCRIPTION, self::TYPE_ONE_TIME]],
            [['is_active'], 'boolean'],
            [['is_active'], 'default', 'value' => 1],
        ];
    }

    public function attributeLabels(): array
    {
        return [
            'id'               => 'ID',
            'name'             => 'Program Name',
            'description'      => 'Short Description',
            'who_it_for'       => 'Who It\'s For',
            'features'         => 'What\'s Included (one per line)',
            'icon'             => 'Icon',
            'sort_order'       => 'Display Order',
            'duration_months'  => 'Duration (Months)',
            'price'            => 'Internal Price (₹)',
            'plan_type'        => 'Program Type',
            'is_active'        => 'Active',
            'created_at'       => 'Created At',
        ];
    }

    /** Returns features as an array of trimmed non-empty lines. */
    public function getFeatureList(): array
    {
        if (!$this->features) {
            return [];
        }
        return array_values(array_filter(array_map('trim', explode("\n", $this->features))));
    }

    public function getCustomerPlans(): \yii\db\ActiveQuery
    {
        return $this->hasMany(CustomerPlan::class, ['plan_id' => 'id']);
    }

    public static function typeLabels(): array
    {
        return [
            self::TYPE_SUBSCRIPTION => 'Subscription',
            self::TYPE_ONE_TIME     => 'One-Time',
        ];
    }

    public function isSubscription(): bool
    {
        return $this->plan_type === self::TYPE_SUBSCRIPTION;
    }
}
