<?php

namespace app\models;

use Yii;
use dektrium\user\models\Profile as BaseProfile;

class Profile extends BaseProfile
{
    /**
     * Clean parent rules: keep only attributes that exist in table schema.
     * Then merge with custom rules.
     */
    public function rules()
    {
        $parentRules = parent::rules();

        // real columns in DB table
        $columns = array_keys(static::getTableSchema()->columns);

        $filteredParentRules = [];

        foreach ($parentRules as $rule) {
            // if rule not an array (rare), keep as is
            if (!is_array($rule) || !isset($rule[0])) {
                $filteredParentRules[] = $rule;
                continue;
            }

            // normalize attributes list (string "a, b" or array)
            $raw = $rule[0];
            if (is_string($raw)) {
                $attrs = preg_split('/\s*,\s*/', trim($raw));
            } elseif (is_array($raw)) {
                $attrs = $raw;
            } else {
                $attrs = [(string)$raw];
            }

            // keep only attrs that are real columns
            $keep = [];
            foreach ($attrs as $a) {
                if (in_array($a, $columns, true)) {
                    $keep[] = $a;
                }
            }

            if (empty($keep)) {
                // no real attributes left for this rule -> skip it
                continue;
            }

            // replace attribute list in the rule
            $rule[0] = (count($keep) === 1) ? $keep[0] : $keep;
            $filteredParentRules[] = $rule;
        }

        // custom rules (adjusted: firma_full and phone NOT required)
        $custom = [
            // required only those you want required (remove firma_full & phone from required)
            [['inn', 'zkpo', 'u_address', 'f_address'], 'required'],

            // numeric checks
            ['role', 'integer'],
            // ['inn', 'match', 'pattern' => '/^[0-9]+$/'],
            // ['inn', 'string', 'min' => 6, 'max' => 20],
            // ['zkpo', 'match', 'pattern' => '/^[0-9]+$/'],
            // ['zkpo', 'string', 'min' => 6, 'max' => 12],

            // strings
            [['firma_full','u_address','f_address','member','member_phone','member_email','site','fax','files','fio','at_org'], 'string', 'max' => 255],

            // email & phone formats
            ['member_email', 'email'],
            [['phone','member_phone','fax'], 'match', 'pattern' => '/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/'],

            // make fields safe so load() will accept them (even if not required)
            [['inn','zkpo','u_address','f_address','firma_full','member','phone','member_phone','fax','member_email','site','role','files','status','org_type','at_org','fio'], 'safe'],
        ];

        return array_merge($filteredParentRules, $custom);
    }

    /**
     * filter parent attributeLabels to only include real columns,
     * then merge with our labels
     */
    public function attributeLabels()
    {
        $parent = parent::attributeLabels();
        $columns = array_keys(static::getTableSchema()->columns);

        $filtered = [];
        foreach ($parent as $k => $v) {
            if (in_array($k, $columns, true)) {
                $filtered[$k] = $v;
            }
        }

        return array_merge($filtered, [
            'firma_full'    => Yii::t('app','Full organization name'),
            'inn'           => Yii::t('app','INN'),
            'zkpo'          => Yii::t('app','ZKPO'),
            'u_address'     => Yii::t('app','Legal address'),
            'f_address'     => Yii::t('app','Personal address'),
            'member'        => Yii::t('app','Contact Person'),
            'phone'         => Yii::t('app','Phone'),
            'fax'           => Yii::t('app','Fax'),
            'member_phone'  => Yii::t('app','Member phone'),
            'member_email'  => Yii::t('app','E-mail'),
            'site'          => Yii::t('app','Site'),
            'fio'           => Yii::t('app','ФИО'),
            'at_org'        => Yii::t('app','Организация'),
            'org_type'      => Yii::t('app','Тип организации'),
        ]);
    
    }
}
