<?php

namespace App\Models\GenericBackoffice;

use App\Libs\Database\Eloquent\ContentTypeModel;
use App\Models\GenericBackoffice\Traits\LanguageSupportTrait;
use App\Models\MediaItem;

class Meal extends ContentTypeModel
{
    use LanguageSupportTrait;

    protected $hidden = ['languages', "websites"];
    protected $with = ['languages', "websites"];

    protected $dates = [];
    protected $spatialFields = [];

    protected $casts = [
        'meal_date' => 'date'
    ];

    public function food()
    {
        return $this->belongsToMany(Food::class, "meal_food")
            ->withTimestamps()
            ->withPivot(['meal_food_id', 'position']);
    }

    public function mealFood()
    {
        return $this->hasMany(MealFood::class);
    }

    public function getMealDateAttribute($value)
    {
        return $this->castAttribute('meal_date', $this->title);
    }

    public function getCanBePickedAttribute($value): bool
    {
        $mealDate = clone $this->meal_date;
        $mealDate->shiftTimezone(config('app.meal.timezone'))->startOfDay()->next('08:00');
        return now()->lte($mealDate);
    }

}
