<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "menuItem".
 *
 * @property int $id
 * @property string $name
 * @property string $descr
 * @property int $cooking_time
 * @property int $price
 * @property int $restaurant_id
 *
 * @property Order[] $orders
 * @property Restaurant $restaurant
 */
class MenuItem extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'menuItem';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'descr', 'cooking_time', 'price', 'restaurant_id'], 'required'],
            [['descr'], 'string'],
            [['cooking_time', 'price', 'restaurant_id'], 'integer'],
            [['name'], 'string', 'max' => 256],
            [['restaurant_id'], 'exist', 'skipOnError' => true, 'targetClass' => Restaurant::class, 'targetAttribute' => ['restaurant_id' => 'id']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'descr' => 'Descr',
            'cooking_time' => 'Cooking Time',
            'price' => 'Price',
            'restaurant_id' => 'Restaurant ID',
        ];
    }

    /**
     * Gets query for [[Orders]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getOrders()
    {
        return $this->hasMany(Order::class, ['menuItem_id' => 'id']);
    }

    /**
     * Gets query for [[Restaurant]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getRestaurant()
    {
        return $this->hasOne(Restaurant::class, ['id' => 'restaurant_id']);
    }

    public function getDynamicPrice()
    {
        $currentHour = (int)date('H');
        
        if ($currentHour >= 19 || $currentHour < 6) {
            return round($this->price * 1.05, 2);
        }
        
        return (float)$this->price;
    }

    public function getPriceMultiplier()
    {
        $currentHour = (int)date('H');
        
        if ($currentHour >= 20 || $currentHour < 6) {
            return 1.05;
        }
        
        return 1.0;
    }

    public static function isNightPremium()
    {
        $currentHour = (int)date('H');
        return $currentHour >= 20 || $currentHour < 6;
    }
}