<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * @property integer $id
 * @property integer $parent_id
 * @property string $sku
 * @property string $type
 * @property string $created_at
 * @property string $updated_at
 * @property Product $product
 * @property CartItem[] $cartItems
 * @property Category[] $categories
 * @property Product[] $products
 * @property ProductImage[] $productImages
 * @property ProductReview[] $productReviews
 * @property Product[] $products
 */
class Product extends Model
{
    /**
     * The "type" of the auto-incrementing ID.
     *
     * @var string
     */
    protected $keyType = 'integer';

    /**
     * @var array
     */
    protected $fillable = ['parent_id', 'sku', 'type','created_at', 'updated_at'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function product()
    {
        return $this->belongsTo('App\Product', 'parent_id');
    }

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

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function categories()
    {
        return $this->belongsToMany('App\Category', 'product_categories');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function productSells()
    {
        return $this->belongsToMany('App\Product', 'product_cross_sells', 'child_id', 'parent_id');
    }

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

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


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

    public function getName(){
        return $this->productAttributes()->where('attribute_id',2)->first()['value'];
    }

    public function getPrice(){
        return $this->productAttributes()->where('attribute_id', 11)->first()['value'];
    }

    public function getSalePrice(){
        return $this->productAttributes()->where('attribute_id', 12)->first()['value'];
    }

    public function calculateDiscount(){
        if($this->getSalePrice() != ''){
            $division = $this->getPrice() - $this->getSalePrice();
            if($division !== 0 && $this->getPrice() != null){
                return ($division / $this->getPrice())*100 .'%';
            }else{
                return 0;
            }
        }

        return 0;

    }

    public function finalPrice(){
        if($this->getSalePrice()){
            return $this->getSalePrice();
        }else{
            return $this->getPrice();
        }
    }

    public function getImage(){
        return isset($this->productImages[0]) ? '/' . $this->productImages[0]->path : '/images/no-image.png';
    }

    public function attributeValue($name){
        $attribute = Attribute::where('code',$name)->first();
        if($attribute){
            return $this->productAttributes()->where('attribute_id', $attribute->id)->first()['value'];
        }else{
            return '';
        }
    }

    public static function getCart(){
        return Cart::find($_COOKIE['cartID']);
    }
}
