<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

/**
 * Class Product.
 *
 * @package namespace App\Models;
 */
class Product extends Model implements Transformable, HasMedia
{
    use TransformableTrait;
    use InteractsWithMedia;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'price',
        'description',
        'category_id',
        'colour_ids',
        'stock'
    ];
    protected $casts = [
        'colour_ids' => 'array',
    ];


    public function productImage($mediaType)
    {
        $media = $this->getMedia($mediaType)->first();
        if ($media) {
            return $media->getUrl();
        }
        return asset('upload/No-Image.png');
    }


    public function category()
    {
        return $this->BelongsTo(Category::class);
    }
    public function orderItems()
    {
        return $this->hasMany(OrderItem::class);
    }
    public function colors()
    {
        return $this->belongsToMany(Colour::class, 'product_colors', 'product_id', 'colour_id');  
    }


}
