<?php

declare(strict_types=1);

namespace common\forms;

use common\models\Customer;
use yii\base\Model;

/**
 * CustomerForm — validation form for creating/editing a customer.
 */
class CustomerForm extends Model
{
    public string $name          = '';
    public string $phone         = '';
    public ?string $email        = null;
    public ?string $address      = null;
    public string $customer_type = Customer::TYPE_INDIVIDUAL;
    public ?string $company_name = null;
    public ?string $gst_number   = null;
    public ?int $parent_customer_id = null;
    public ?string $notes        = null;
    public int $is_active        = 1;
    public ?int $lead_id         = null;

    public function rules(): array
    {
        return [
            [['name', 'phone'], 'required'],
            [['name'], 'string', 'max' => 150],
            [['phone'], 'string', 'max' => 20],
            [['phone'], 'match', 'pattern' => '/^[+0-9\s\-()]{7,20}$/', 'message' => 'Enter a valid phone number.'],
            [['email'], 'email'],
            [['email'], 'string', 'max' => 150],
            [['address', 'notes'], 'string'],
            [['customer_type'], 'in', 'range' => [Customer::TYPE_INDIVIDUAL, Customer::TYPE_BUSINESS]],
            [['company_name'], 'string', 'max' => 200],
            [['company_name'], 'required', 'when' => function ($model) {
                return $model->customer_type === Customer::TYPE_BUSINESS;
            }],
            [['gst_number'], 'string', 'max' => 20],
            [['parent_customer_id', 'lead_id'], 'integer'],
            [['is_active'], 'boolean'],
        ];
    }

    public function attributeLabels(): array
    {
        return [
            'name'               => 'Full Name',
            'phone'              => 'Phone',
            'email'              => 'Email',
            'address'            => 'Address',
            'customer_type'      => 'Customer Type',
            'company_name'       => 'Company / Gym Name',
            'gst_number'         => 'GST Number',
            'parent_customer_id' => 'Parent (Gym/Company)',
            'notes'              => 'Notes',
            'is_active'          => 'Active',
        ];
    }
}
