<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Session;

/**
 * Class Product
 * @package App
 */
class Product extends Model
{

	/**
	 * The database table the Model is associated to
	 *
	 * @var string
	 */
	protected $table = 'products';


	/**
	 * Mass-assignable attributes of the model
	 *
	 * @var array
	 */
	protected $fillable
		= [
			'category_id',
			'fruit_id',
			'sort',
			'user_id',
			'country',
			'location',
			'info',
			'amount',
			'min_amount',
			'disabled',
			'active'
		];


	/**
	 * @var array
	 */
	protected $hidden = [];


	/**
	 * Get the seller of the product
	 *
	 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
	 */
	public function user()
	{
		return $this->belongsTo('App\User');
	}

	/**p
	 * Get the images of the product
	 *
	 * @return \Illuminate\Database\Eloquent\Relations\HasMany
	 */
	public function pictures()
	{
		return $this->hasMany('App\Product_img');
	}

	public function firstpic()
	{
		return $this->pictures()->first()->small();
	}

	/**
	 * Get the name of the product's category. Eg vegetable or berry
	 *
	 * @return mixed
	 */
	public function category()
	{
		$lang = Session::get('locale');
		return Category::find($this->category_id)->$lang;
	}

	/**
	 * Get the name of the product's fruit. Eg Apple or Strawberry
	 *
	 * @return mixed
	 */
	public function fruit()
	{
		$lang = Session::get('locale');
		return Fruit::find($this->fruit_id)->$lang;
	}


	/**
	 * @return \Illuminate\Database\Eloquent\Relations\HasMany
	 */
	public function allAmounts()
	{
		return $this->hasMany('App\Product_amount');
	}


	/**
	 * @return int
	 */
	public function amountOfPictures()
	{
		return count($this->pictures()->get()->toArray());
	}

	/**
	 * @return \Illuminate\Database\Eloquent\Relations\HasMany
	 */
	public function prices()
	{
		return $this->hasMany('App\Price');
	}

	/**
	 * @return int
	 */
	public function price()
	{
		$price = $this->prices()->orderBy('id', 'desc')->first();
		if ($price == null) {
			return 0.00;
		} else {
			return $price->price;
		}
	}

	public function transactions()
	{
		return $this->hasMany('App\Transaction');
	}

	public function auctions()
	{
		return $this->hasMany('App\Auction');
	}

	public function comments(){
		return $this->hasMany('App\Comment');
	}

	public function transports(){
		return $this->hasMany('App\Transport');
	}
}
