<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    public $timestamps = true;

    protected $fillable = [
        'caption',
        'category_id',
        'subcategory_id',
        'user_id',
        'description',
        'pictures',
    ];

    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function subcategory()
    {
        return $this->belongsTo('App\Models\Subcategory');
    }

    public function category()
    {
        return $this->belongsTo('App\Models\Category');
    }

    public function picture()
    {
        return $this->hasMany(Picture::class);
    }

    public function getPictures(): Collection
    {
        return Picture::where('item_id', '=', $this->id)->get();
    }

    public function getPicturesPath(): array
    {
        if ($this->picturePath){
            return $this->picturePath;
        }

        $picturesPath = [];
        foreach ($this->getPictures() as $picture) {
            $picturesPath[] = $picture->path;
        };
        return $this->picturePath = $picturesPath;
    }
}
