<?php
    namespace common\modules\product\behaviors;
    
    use common\modules\language\models\Language;
    use common\modules\product\models\ProductVariantLang;
    use yii\base\Behavior;
    use common\modules\product\models\Product;
    use common\modules\product\models\ProductVariant;
    use common\modules\product\models\ProductUnit;
    use common\modules\product\models\Stock;
    
    /**
     * Class DefaultVariantBehavior
     *
     * @property Product $owner
     * @see     ProductVariant
     */
    class DefaultVariantBehavior extends Behavior
    {
        
        /**
         * @todo add default variant image also
         */
        
        /**
         * Catches product's insert event
         *
         * @return array
         */
        public function events()
        {
            return [
                Product::EVENT_AFTER_INSERT => 'addDefaultVariant',
            ];
        }
        
        /**
         * Creates new default product's variant and sets it's to stock
         * marked as default and sets to it unit also marked as default
         */
        public function addDefaultVariant()
        {
            /**
             * @var Stock       $stock
             * @var ProductUnit $defaultUnit
             */
            $defaultVariant = new ProductVariant();
            $defaultVariant->product_id = $this->owner->product_id;
            
            /**
             * Gets default unit for variant
             */
            $defaultUnit = ProductUnit::find()
                                      ->where(
                                          [
                                              'is_default' => true,
                                          ]
                                      )
                                      ->one();
            $defaultVariant->product_unit_id = $defaultUnit->product_unit_id;
            $defaultVariant->stock = 1;
            
            $defaultVariant->sku = 'default';
            $defaultVariant->remote_id = time();
            $defaultVariant->save();
            
            /**
             * Saving languages
             */
            $activeLanguageIds = Language::find()
                                         ->select('language_id')
                                         ->where(
                                             [
                                                 'status' => true,
                                             ]
                                         )
                                         ->asArray()
                                         ->column();
            foreach ($activeLanguageIds as $languageId) {
                $variantLanguage = new ProductVariantLang();
                $variantLanguage->language_id = $languageId;
                $variantLanguage->product_variant_id = $defaultVariant->product_variant_id;
                $variantLanguage->name = 'default_' . $languageId;
                $variantLanguage->save();
            }
        }
        
    }
    