<?php

declare(strict_types=1);

namespace common\models;

use yii\db\ActiveRecord;

/**
 * DietPlanMeal — individual food item in a diet plan meal slot.
 *
 * @property int         $id
 * @property int         $diet_plan_id
 * @property string      $meal_type  breakfast|morning_snack|lunch|evening_snack|dinner|bedtime_snack
 * @property int|null    $food_library_id
 * @property string|null $food_name
 * @property float       $quantity
 * @property string      $unit
 * @property float       $calories
 * @property float       $protein
 * @property float       $carbs
 * @property float       $fats
 * @property string|null $notes
 * @property int         $sort_order
 *
 * @property DietPlan        $dietPlan
 * @property FoodLibrary|null $food
 */
class DietPlanMeal extends ActiveRecord
{
    public const MEAL_BREAKFAST     = 'breakfast';
    public const MEAL_MORNING_SNACK = 'morning_snack';
    public const MEAL_LUNCH         = 'lunch';
    public const MEAL_EVENING_SNACK = 'evening_snack';
    public const MEAL_DINNER        = 'dinner';
    public const MEAL_BEDTIME_SNACK = 'bedtime_snack';

    public static function tableName(): string
    {
        return '{{%diet_plan_meals}}';
    }

    public function rules(): array
    {
        return [
            [['diet_plan_id', 'meal_type'], 'required'],
            [['diet_plan_id', 'food_library_id', 'sort_order'], 'integer'],
            [['meal_type'], 'in', 'range' => array_keys(self::mealTypeLabels())],
            [['food_name'], 'string', 'max' => 200],
            [['unit'], 'string', 'max' => 20],
            [['unit'], 'default', 'value' => 'g'],
            [['quantity'], 'number', 'min' => 0],
            [['quantity'], 'default', 'value' => 100],
            [['calories', 'protein', 'carbs', 'fats'], 'number', 'min' => 0],
            [['calories', 'protein', 'carbs', 'fats'], 'default', 'value' => 0],
            [['notes'], 'string', 'max' => 300],
            [['sort_order'], 'default', 'value' => 0],
            // Either food_library_id OR food_name must be set
            [['food_name'], 'required', 'when' => function ($model) {
                return empty($model->food_library_id);
            }],
        ];
    }

    public function attributeLabels(): array
    {
        return [
            'id'              => 'ID',
            'diet_plan_id'    => 'Diet Plan',
            'meal_type'       => 'Meal',
            'food_library_id' => 'Food',
            'food_name'       => 'Food Name',
            'quantity'        => 'Quantity',
            'unit'            => 'Unit',
            'calories'        => 'Calories (kcal)',
            'protein'         => 'Protein (g)',
            'carbs'           => 'Carbs (g)',
            'fats'            => 'Fats (g)',
            'notes'           => 'Notes',
        ];
    }

    public function getDietPlan(): \yii\db\ActiveQuery
    {
        return $this->hasOne(DietPlan::class, ['id' => 'diet_plan_id']);
    }

    public function getFood(): \yii\db\ActiveQuery
    {
        return $this->hasOne(FoodLibrary::class, ['id' => 'food_library_id']);
    }

    public static function mealTypeLabels(): array
    {
        return [
            self::MEAL_BREAKFAST     => 'Breakfast',
            self::MEAL_MORNING_SNACK => 'Morning Snack',
            self::MEAL_LUNCH         => 'Lunch',
            self::MEAL_EVENING_SNACK => 'Evening Snack',
            self::MEAL_DINNER        => 'Dinner',
            self::MEAL_BEDTIME_SNACK => 'Bedtime Snack',
        ];
    }

    public function getDisplayName(): string
    {
        if ($this->food_library_id && $this->food !== null) {
            return $this->food->name;
        }
        return (string)$this->food_name;
    }
}
