<?php
    
    namespace common\modules\product\models;
    
    use yii\base\Model;
    use yii\data\ActiveDataProvider;
    use yii\db\ActiveQuery;
    
    /**
     * ProductSearch represents the model behind the search form about
     * `common\modules\product\models\Product`.
     */
    class ProductSearch extends Product
    {
        
        public $brand_id;
        
        public $category_id;
        
        public $product_name;
        
        public $variant_count;
        
        public function behaviors()
        {
            $behaviors = parent::behaviors();
            if(isset( $behaviors[ 'language' ] )) {
                unset( $behaviors[ 'language' ] );
            }
            return $behaviors;
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'product_name',
                    ],
                    'safe',
                ],
                [
                    [
                        'brand_id',
                        'product_id',
                        'category_id',
                    ],
                    'integer',
                ],
                [
                    [
                        'is_top',
                        'is_new',
                        'akciya',
                    ],
                    'boolean',
                ],
            ];
        }
        
        public function attributeLabels()
        {
            $labels = parent::attributeLabels();
            $new_labels = [
                'category_id'   => Yii::t('app', 'pr_category_id'),
                'brand_id'      => Yii::t('app', 'pr_brand_id'),
                'product_name'  => Yii::t('app', 'pr_product_name'),
                'variant_count' => Yii::t('app', 'pr_variant_count'),
            ];
            return array_merge($labels, $new_labels);
        }
        
        /**
         * @inheritdoc
         */
        public function scenarios()
        {
            // bypass scenarios() implementation in the parent class
            return Model::scenarios();
        }
        
        /**
         * Creates data provider instance with search query applied
         *
         * @param array $params
         *
         * @return ActiveDataProvider
         */
        public function search($params)
        {
            
            $query = Product::find();
            $query->select([
                'product.*',
                'COUNT(product_variant.product_variant_id) as count',
            ]);
            
            $query->joinWith([
                'categories',
                'lang',
            ])
                  ->joinWith([
                      'brand' => function($query) {
                          /**
                           * @var ActiveQuery $query
                           */
                          $query->joinWith('lang');
                      },
                  ])
                  ->joinWith('variants');
            
            $query->groupBy([
                'product.product_id',
                'brand_lang.name',
                'product_lang.name',
            ]);
            
            $dataProvider = new ActiveDataProvider([
                'query' => $query,
            ]);
            
            $dataProvider->setSort([
                'attributes' => [
                    'product_id',
                    'product_name'  => [
                        'asc'  => [ 'product_lang.name' => SORT_ASC ],
                        'desc' => [ 'product_lang.name' => SORT_DESC ],
                    ],
                    'brand_id'      => [
                        'asc'     => [ 'brand_lang.name' => SORT_ASC ],
                        'desc'    => [ 'brand_lang.name' => SORT_DESC ],
                        'default' => SORT_DESC,
                    ],
                    'variant_count' => [
                        'asc'  => [ 'count' => SORT_ASC ],
                        'desc' => [ 'count' => SORT_DESC ],
                    ],
                ],
            ]);
            
            $this->load($params);
            
            if(isset( $this->is_top )) {
                $query->andWhere([
                    'is_top' => (bool) $this->is_top,
                ]);
            }
            if(isset( $this->is_new )) {
                $query->andWhere([
                    'is_new' => (bool) $this->is_new,
                ]);
            }
            if(isset( $this->akciya )) {
                $query->andWhere([
                    'akciya' => (bool) $this->akciya,
                ]);
            }
            $query->andFilterWhere([
                'product.brand_id'             => $this->brand_id,
                'product.product_id'           => $this->product_id,
                'product_category.category_id' => $this->category_id,
            ]);
            $query->andFilterWhere([
                'like',
                'product_lang.name',
                $this->product_name,
            ]);
            
            return $dataProvider;
        }
    }
