<?php

declare(strict_types=1);

namespace common\models;

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

/**
 * Lead ActiveRecord model.
 *
 * @property int         $id
 * @property string      $name
 * @property string      $phone
 * @property string|null $email
 * @property string|null $source
 * @property string|null $interest
 * @property string|null $message
 * @property string      $status   new|contacted|converted|lost
 * @property int         $created_at
 * @property int         $updated_at
 *
 * @property Customer[]  $customers
 */
class Lead extends ActiveRecord
{
    public const STATUS_NEW       = 'new';
    public const STATUS_CONTACTED = 'contacted';
    public const STATUS_CONVERTED = 'converted';
    public const STATUS_LOST      = 'lost';

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

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

    public function rules(): array
    {
        return [
            [['name', 'phone'], 'required'],
            [['name'], 'string', 'max' => 100],
            [['phone'], 'string', 'max' => 20],
            [['email'], 'email'],
            [['email'], 'string', 'max' => 150],
            [['source', 'interest'], 'string', 'max' => 100],
            [['message'], 'string'],
            [['status'], 'in', 'range' => [self::STATUS_NEW, self::STATUS_CONTACTED, self::STATUS_CONVERTED, self::STATUS_LOST]],
            [['status'], 'default', 'value' => self::STATUS_NEW],
        ];
    }

    public function attributeLabels(): array
    {
        return [
            'id'         => 'ID',
            'name'       => 'Name',
            'phone'      => 'Phone',
            'email'      => 'Email',
            'source'     => 'Source',
            'interest'   => 'Interest',
            'message'    => 'Message',
            'status'     => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

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

    public static function statusLabels(): array
    {
        return [
            self::STATUS_NEW       => 'New',
            self::STATUS_CONTACTED => 'Contacted',
            self::STATUS_CONVERTED => 'Converted',
            self::STATUS_LOST      => 'Lost',
        ];
    }

    public function getStatusLabel(): string
    {
        return self::statusLabels()[$this->status] ?? $this->status;
    }
}
