<?php

namespace App\Models;

use App\Observers\ProductObserver;
use Illuminate\Database\Eloquent\Model;
use Astrotomic\Translatable\Translatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;

class Product extends Model implements TranslatableContract
{
    use Translatable;
    protected $guarded = ['id','created_at','updated_at','deleted_at'];
    public $translatedAttributes = ['title', 'desc', 'serving_desc', 'slug', 'brand'];


    protected static function boot()
    {
        parent::boot();
        Product::observe(ProductObserver::class) ;
    }

    public function getBaseImageAttribute($key)
    {
        $media = $this->media()->where('option', 'base_image')->first();
        $image = $media ? asset('storage/images/products/' . $media->media) : null;
        return $image;
    }

    public function getOtherImagesAttribute($key){
        $media = $this->media()->where('option', 'other_images')->get();
        $images = [];
        foreach($media as $m){
            $images[] = [
                'id' => (int) $m->id,
                'media' => asset('storage/images/products/' . $m->media)
            ];
        }
        return $images;
    }

    public function getIsSavedAttribute($key)
    {
        $user = auth('api')->user();
        if ($user) {
            return $user->savedRecipes()->where('recipe_id', $this->id)->exists();
        }
        return false;
    }

    public function getIsLikedAttribute($key)
    {
        $user = auth('api')->user();
        if ($user) {
            return $user->likedRecipes()->where('recipe_id', $this->id)->exists();
        }
        return false;
    }

    public function getIsSharedAttribute($key)
    {
        $user = auth('api')->user();
        if ($user) {
            return $user->sharedRecipes()->where('recipe_id', $this->id)->exists();
        }
        return false;
    }

    public function getLikesCountAttribute()
    {
        return $this->likedRecipes()->count();
    }

    public function getSharesCountAttribute()
    {
        return $this->sharedRecipes()->count();
    }

    public function getCaloriesPerGramAttribute()
    {
        return $this->calories / $this->calories_per;
    }

    public function getCarbsPerGramAttribute()
    {
        return $this->productCarbs->total_carbs / $this->calories_per;
    }

    public function getFatsPerGramAttribute()
    {
        return $this->productFats->total_fats / $this->calories_per;
    }

    public function getProteinPerGramAttribute()
    {
        return $this->total_protein / $this->calories_per;
    }

    

    // Relations
    // ========================= Image ===================

    public function likedRecipes()
    {
    	return $this->belongsToMany(User::class, 'recipe_like', 'recipe_id', 'user_id');
    }

    public function sharedRecipes()
    {
    	return $this->belongsToMany(User::class, 'recipe_share', 'recipe_id', 'user_id');
    }

    public function user()
    {
    	return $this->belongsTo(User::class);
    }

    public function meal()
    {
    	return $this->belongsTo(Meal::class);
    }

    public function productDirections()
    {
    	return $this->hasMany(ProductDirection::class);
    }

    public function productIngredients()
    {
    	return $this->hasMany(ProductIngredient::class);
    }

    public function productCarbs()
    {
    	return $this->hasOne(ProductCarb::class);
    }

    public function productFats()
    {
    	return $this->hasOne(ProductFat::class);
    }

    public function productOthers()
    {
    	return $this->hasOne(ProductOther::class);
    }

    public function productMicros()
    {
    	return $this->hasOne(ProductMicro::class);
    }

    public function media()
    {
    	return $this->morphOne(AppMedia::class,'app_mediaable');
    }


}