<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;

use App\Events\BlogSaved;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

use App\Traits\HasContentElementsTrait;
use App\Traits\AppendAttributesTrait;
use App\Traits\VersioningTrait;
use App\Traits\SlugTrait;
use App\Traits\TagsTrait;
use App\Traits\HasPermissionsTrait;
use App\Traits\FindByTagsTrait;

use App\Models\Page;
use App\Models\Tag;
use App\Models\ContentElement;

use App\Contracts\SearchResultContract;
use App\Utilities\SearchResult;

class Blog extends Model implements SearchResultContract
{
    use HasFactory;
    use SoftDeletes;
    use AppendAttributesTrait;
    use HasContentElementsTrait;
    use VersioningTrait;
    use SlugTrait;
    use TagsTrait;
    use HasPermissionsTrait;
    use FindByTagsTrait;

    //protected $with = ['tags', 'publishedVersion', 'contentElements', 'pageSlugs'];
    //protected $appends = ['version', 'type', 'actions', 'has_permissions', 'full_slug', 'preview_content_elements', 'published_at'];

    protected $hidden = [
        'contentElements',
        'signed_url',
        'preview_url',
    ];

    public $append_attributes = [
        'actions',
        'full_type',
        'full_slug',
        'resource',
        'type',
        'name',
        'published_at',
        'first_published_at',
        //'version',
        //'can_be_published',
    ];

    protected $casts = [
        'unlisted' => 'boolean',
    ];

    protected $appends = ['type'];

    public function saveBlog(array $input, $id = null)
    {
        if ($id) {
            $blog = Blog::findOrFail($id);
        } else {
            $blog = new Blog();
        }

        $blog->author = Arr::get($input, 'author');
        $blog->save();

        $blog->saveVersion($input);
        $blog->saveContentElements($input);
        $blog->saveTags($input);

        cache()->tags([cache_name($blog)])->flush();

        broadcast(new BlogSaved($blog))->toOthers();

        return $blog;
    }

    public function getFullSlugAttribute()
    {
        return 'blogs/'.$this->getSlug();
    }

    public function getNextBlogAttribute()
    {
        if (!$this->published_at) {
            return null;
        }

        $version = Version::whereHasMorph('versionable', [Blog::class])
            ->whereNotNull('published_at')
            ->where('published_at', '>', $this->published_at)
            ->orderBy('published_at')
            ->first();

        return $version ? $version->versionable : null;
    }

    public function getPreviousBlogAttribute()
    {
        if (!$this->published_at) {
            return null;
        }

        $version = Version::whereHasMorph('versionable', [Blog::class])
            ->whereNotNull('published_at')
            ->where('published_at', '<', $this->published_at)
            ->orderByDesc('published_at')
            ->first();

        return $version ? $version->versionable : null;
    }

    public function getFooterFgPhoto()
    {
        $value = $this->footerFgPhoto;
        if ($value) {
            return $value;
        } else {
            return cache()->tags([cache_name($this)])->rememberForever(cache_name($this).'-footer-fg-photo', function () {
                return Page::find(1)->footerFgPhoto;
            });
        }
    }

    public function getFooterBgPhoto()
    {
        $value = $this->footerBgPhoto;
        if ($value) {
            return $value;
        } else {
            return cache()->tags([cache_name($this)])->rememberForever(cache_name($this).'-footer-bg-photo', function () {
                return Page::find(1)->footerBgPhoto;
            });
        }
    }

    public function getFooterColorAttribute($value)
    {
        if ($value) {
            return $value;
        } else {
            return cache()->tags([cache_name($this)])->rememberForever(cache_name($this).'-footer-color', function () {
                return Page::find(1)->footer_color;
            });
        }
    }
}
