<?php

declare(strict_types=1);

namespace common\models;

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

/**
 * Unified Customer ActiveRecord model.
 * Handles: Individual, Business (Gym/Corporate), and Members (linked via parent_customer_id).
 *
 * @property int         $id
 * @property string      $name
 * @property string      $phone
 * @property string|null $email
 * @property string|null $address
 * @property string      $customer_type  individual|business
 * @property string|null $company_name
 * @property string|null $gst_number
 * @property int|null    $parent_customer_id
 * @property string|null $notes
 * @property int         $is_active
 * @property int|null    $lead_id
 * @property int         $created_at
 * @property int         $updated_at
 *
 * @property Lead|null        $lead
 * @property Customer|null    $parentCustomer
 * @property Customer[]       $members
 * @property CustomerPlan[]   $customerPlans
 * @property Invoice[]        $invoices
 * @property DietPlan[]       $dietPlans
 */
class Customer extends ActiveRecord
{
    public const TYPE_INDIVIDUAL = 'individual';
    public const TYPE_BUSINESS   = 'business';

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

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

    public function rules(): array
    {
        return [
            [['name', 'phone'], 'required'],
            [['name'], 'string', 'max' => 150],
            [['phone'], 'string', 'max' => 20],
            [['email'], 'email'],
            [['email'], 'string', 'max' => 150],
            [['address', 'notes'], 'string'],
            [['customer_type'], 'in', 'range' => [self::TYPE_INDIVIDUAL, self::TYPE_BUSINESS]],
            [['customer_type'], 'default', 'value' => self::TYPE_INDIVIDUAL],
            [['company_name'], 'string', 'max' => 200],
            [['gst_number'], 'string', 'max' => 20],
            [['parent_customer_id', 'lead_id'], 'integer'],
            [['is_active'], 'boolean'],
            [['is_active'], 'default', 'value' => 1],
            [['parent_customer_id'], 'exist', 'skipOnError' => true, 'targetClass' => self::class, 'targetAttribute' => ['parent_customer_id' => 'id']],
            [['lead_id'], 'exist', 'skipOnError' => true, 'targetClass' => Lead::class, 'targetAttribute' => ['lead_id' => 'id']],
        ];
    }

    public function attributeLabels(): array
    {
        return [
            'id'                 => 'ID',
            'name'               => 'Name',
            'phone'              => 'Phone',
            'email'              => 'Email',
            'address'            => 'Address',
            'customer_type'      => 'Customer Type',
            'company_name'       => 'Company Name',
            'gst_number'         => 'GST Number',
            'parent_customer_id' => 'Parent Customer',
            'notes'              => 'Notes',
            'is_active'          => 'Active',
            'lead_id'            => 'Lead',
            'created_at'         => 'Created At',
            'updated_at'         => 'Updated At',
        ];
    }

    // --- Relationships ---

    public function getLead(): \yii\db\ActiveQuery
    {
        return $this->hasOne(Lead::class, ['id' => 'lead_id']);
    }

    public function getParentCustomer(): \yii\db\ActiveQuery
    {
        return $this->hasOne(self::class, ['id' => 'parent_customer_id']);
    }

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

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

    public function getActivePlan(): \yii\db\ActiveQuery
    {
        return $this->hasOne(CustomerPlan::class, ['customer_id' => 'id'])
            ->where(['status' => CustomerPlan::STATUS_ACTIVE])
            ->orderBy(['created_at' => SORT_DESC]);
    }

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

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

    // --- Helpers ---

    public function isIndividual(): bool
    {
        return $this->customer_type === self::TYPE_INDIVIDUAL;
    }

    public function isBusiness(): bool
    {
        return $this->customer_type === self::TYPE_BUSINESS;
    }

    public function isMember(): bool
    {
        return $this->parent_customer_id !== null;
    }

    public static function typeLabels(): array
    {
        return [
            self::TYPE_INDIVIDUAL => 'Individual',
            self::TYPE_BUSINESS   => 'Business',
        ];
    }
}
