<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
use Spatie\Translatable\HasTranslations;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\Table\TableExtension;
use League\CommonMark\Extension\Autolink\AutolinkExtension;
use League\CommonMark\Extension\Strikethrough\StrikethroughExtension;
use League\CommonMark\Extension\TaskList\TaskListExtension;
use League\CommonMark\MarkdownConverter;

class BlogPost extends Model
{
    use HasFactory, SoftDeletes, HasTranslations;

    protected $table = 'blog_posts';

    protected $translatable = [
        'title',
        'excerpt',
        'content',
        'meta_title',
        'meta_description',
    ];

    protected $fillable = [
        'blog_category_id',
        'slug',
        'banner',
        'published_at',
        'is_published',
        'title',
        'content',
        'excerpt',
        'meta_title',
        'meta_description',
        'translations',
    ];

    protected $casts = [
        'published_at' => 'date',
        'is_published' => 'boolean',
    ];

    protected $appends = [
        'banner_url',
        'content_html',
    ];

    public function bannerUrl(): Attribute
    {
        return Attribute::get(function () {
            if (! $this->banner) {
                return '';
            }

            $path = ltrim($this->banner, '/');

            return \URL::temporarySignedRoute(
                'api.v1.branding.asset',
                now()->addMinutes(30),
                ['path' => $path]
            );
        });
    }

    public function contentHtml(): Attribute
    {
        return Attribute::get(function () {
            $markdown = $this->getTranslation('content', app()->getLocale());

            $environment = new Environment();
            $environment->addExtension(new CommonMarkCoreExtension());
            $environment->addExtension(new TableExtension());
            $environment->addExtension(new AutolinkExtension());
            $environment->addExtension(new StrikethroughExtension());
            $environment->addExtension(new TaskListExtension());

            $converter = new MarkdownConverter($environment);
            return (string) $converter->convert($markdown);
        });
    }

    public function scopePublished(Builder $query)
    {
        return $query->whereNotNull('published_at')->where('is_published', true);
    }

    public function scopeDraft(Builder $query)
    {
        return $query->whereNull('published_at');
    }

    public function category(): BelongsTo
    {
        return $this->belongsTo(BlogCategory::class, 'blog_category_id');
    }

    public function author(): BelongsTo
    {
        return $this->belongsTo(BlogAuthor::class, 'blog_author_id');
    }
}
