<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class Medium extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    protected $fillable = [
        'post_id',
        'thumbnail',
        'url'
    ];

    public function post()
    {
        return $this->morphOne('App\Post', 'postable');
    }

    public function scopeNotFeatured(Builder $query) {
        return $query->whereHas('post', function ($q) {
            $q->where('featured', 0);
        });
    }

    public function scopeFeatured(Builder $query) {
        return $query->whereHas('post', function ($q) {
            $q->where('featured', 1);
        });
    }

    public function getPageAttribute()
    {
        return Page::whereHas('post', function($q) {
            $q->where('id', $this->post_id);
        })->first();
    }

    public function getProductAttribute()
    {
        return Product::whereHas('post', function($q) {
            $q->where('id', $this->post_id);
        })->first();
    }

    public function getSectionAttribute()
    {
        return Section::whereHas('post', function($q) {
            $q->where('id', $this->post_id);
        })->first();
    }
}
