<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Section extends Model
{
    protected $fillable = [
        'page_id',
        'selector',
        'url'
    ];

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

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

    public function getImageAttribute()
    {
        return $this->post->media()->featured()->first() ?? $this->images->first();
    }

    public function getImagesAttribute()
    {
        return $this->post->media()->notFeatured()->get()->sortBy('post.order');
    }

    public function parent_post()
    {
        return $this->belongsTo('App\Post', 'post_id', 'id');
    }

    public function selectors()
    {
        return config('cms.selectors') ?? [];
    }

    public function getSelectorNameAttribute()
    {
        return config('cms.selectors')[$this->attributes['selector']] ?? null;
    }
}
