<?php
    
    namespace common\modules\product\models;
    
    use common\behaviors\ImageBehavior;
    use Yii;
    
    /**
     * This is the model class for table "product_image".
     * @property integer        $product_image_id
     * @property integer        $product_id
     * @property integer        $product_variant_id
     * @property string         $image
     * @property string         $alt
     * @property string         $title
     * @property Product        $product
     * @property ProductVariant $productVariant
     */
    class ProductImage extends \yii\db\ActiveRecord
    {
        
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'product_image';
        }
        
        public function behaviors()
        {
            return [
                [
                    'class'     => ImageBehavior::className(),
                    'link'      => 'image',
                    'directory' => 'products',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [ 'product_id' ],
                    'required',
                ],
                [
                    [
                        'product_image_id',
                        'product_id',
                        'product_variant_id',
                    ],
                    'integer',
                ],
                [
                    [
                        'alt',
                        'title',
                        'image',
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                    [ 'product_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => Product::className(),
                    'targetAttribute' => [ 'product_id' => 'product_id' ],
                ],
                [
                    [ 'product_variant_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => ProductVariant::className(),
                    'targetAttribute' => [ 'product_variant_id' => 'product_variant_id' ],
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'product_image_id'   => Yii::t('product', 'Product Image ID'),
                'product_id'         => Yii::t('product', 'Product ID'),
                'product_variant_id' => Yii::t('product', 'Product Variant ID'),
                'product'            => Yii::t('product', 'Product'),
                'product_variant'    => Yii::t('product', 'Product Variant'),
                'image'              => Yii::t('product', 'Image'),
                'alt'                => Yii::t('product', 'Alt'),
                'title'              => Yii::t('product', 'Title'),
            ];
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getProduct()
        {
            $return = $this->hasOne(Product::className(), [ 'product_id' => 'product_id' ]);
            if(empty( $return )) {
                $return = $this->productVariant->product_id;
            }
            return $return;
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getProductVariant()
        {
            return $this->hasOne(Product::className(), [ 'product_variant_id' => 'product_variant_id' ]);
        }
    }
