<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "items".
 *
 * @property int $id
 * @property int $main_category_id
 * @property int|null $sub_category_id
 * @property string $arabic_name
 * @property string $english_name
 * @property string $arabic_description
 * @property string $english_description
 * @property string $preparation_time
 *
 * @property ItemImages[] $itemImages
 * @property ItemVariations[] $itemVariations
 * @property MainCategories $mainCategory
 * @property SubCategories $subCategory
 */
class Items extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'items';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['main_category_id', 'arabic_name', 'english_name', 'arabic_description', 'english_description', 'preparation_time'], 'required'],
            [['main_category_id', 'sub_category_id', 'calories'], 'integer', 'message' => Yii::t('app', 'Invalid Value')],
            [['preparation_time', 'calories'], 'integer', 'min' => 1],
            [['arabic_name', 'english_name'], 'string', 'max' => 200],
            [['preperation_method_en','preperation_method_ar'], 'string', 'max' => 255],
            [['arabic_description', 'english_description'], 'string', 'max' => 1500],
            [['arabic_additional_sauces', 'english_additional_sauces'], 'string'],
            [['status'], 'string'],
            [['status'], 'default','value'=> 'Active'],
            [['is_new'], 'boolean'],
            [['is_new'], 'default','value'=> 0],
            [['main_category_id'], 'exist', 'skipOnError' => true, 'targetClass' => MainCategories::className(), 'targetAttribute' => ['main_category_id' => 'id']],
            [['sub_category_id'], 'exist', 'skipOnError' => true, 'targetClass' => SubCategories::className(), 'targetAttribute' => ['sub_category_id' => 'id']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'main_category_id' => Yii::t('app', 'Main Category'),
            'sub_category_id' => Yii::t('app', 'Sub Category'),
            'arabic_name' => Yii::t('app', 'Arabic Name'),
            'english_name' => Yii::t('app', 'English Name'),
            'arabic_description' => Yii::t('app', 'Arabic Description'),
            'english_description' => Yii::t('app', 'English Description'),
            'calories' => Yii::t('app', 'Calories'),
            'arabic_additional_sauces' => Yii::t('app', 'Arabic Additional Sauces'),
            'english_additional_sauces' => Yii::t('app', 'English Additional Sauces'),
            'preparation_time' => Yii::t('app', 'Preparation Time (minutes)'),
            'status' => Yii::t('app', 'Status'),
            'is_new' => Yii::t('app', 'Is New?'),
            'preperation_method_en' => Yii::t('app', 'Preparation Method English'),
            'preperation_method_ar' => Yii::t('app', 'Preparation Method Arabic'),
        ];
    }

    /**
     * Gets query for [[ItemImages]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getItemImages()
    {
        return $this->hasMany(ItemImages::className(), ['item_id' => 'id']);
    }

    public function getItemFirstImage()
    {
        return $this->hasOne(ItemImages::className(), ['item_id' => 'id'])
        ->orderBy(['sort_order' => SORT_ASC]);
    }

    /**
     * Gets query for [[ItemVariations]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getItemVariations()
    {
        return $this->hasMany(ItemVariations::className(), ['item_id' => 'id']);
    }

    /**
     * Gets query for [[MainCategory]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getMainCategory()
    {
        return $this->hasOne(MainCategories::className(), ['id' => 'main_category_id']);
    }

    /**
     * Gets query for [[SubCategory]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getSubCategory()
    {
        return $this->hasOne(SubCategories::className(), ['id' => 'sub_category_id']);
    }
}
