<?php

namespace App\Models;

use App\Helpers\EntryHelpers;
use App\Traits\HasGallery;
use App\Traits\HasXenforoUserId;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use League\CommonMark\GithubFlavoredMarkdownConverter;

/**
 * @property int $id
 * @property string $title
 * @property string $slug
 * @property int|null $category_id
 * @property string $description
 * @property string $state
 * @property string|null $staff_comment
 * @property \Illuminate\Support\Carbon|null $rejected_at
 * @property int|null $entry_id
 * @property string|null $relevant_link
 * @property string|null $youtube_link
 * @property int $user_id
 * @property int|null $comments_thread_id
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\Category|null $category
 * @property-read \App\Models\Entry|null $entry
 * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Gallery> $gallery
 * @property-read int|null $gallery_count
 * @method static Builder<static>|News inQueue(int $daysRejected = 7)
 * @method static Builder<static>|News newModelQuery()
 * @method static Builder<static>|News newQuery()
 * @method static Builder<static>|News onlyTrashed()
 * @method static Builder<static>|News published()
 * @method static Builder<static>|News query()
 * @method static Builder<static>|News whereCategoryId($value)
 * @method static Builder<static>|News whereCommentsThreadId($value)
 * @method static Builder<static>|News whereCreatedAt($value)
 * @method static Builder<static>|News whereDeletedAt($value)
 * @method static Builder<static>|News whereDescription($value)
 * @method static Builder<static>|News whereEntryId($value)
 * @method static Builder<static>|News whereId($value)
 * @method static Builder<static>|News whereRejectedAt($value)
 * @method static Builder<static>|News whereRelevantLink($value)
 * @method static Builder<static>|News whereSlug($value)
 * @method static Builder<static>|News whereStaffComment($value)
 * @method static Builder<static>|News whereState($value)
 * @method static Builder<static>|News whereTitle($value)
 * @method static Builder<static>|News whereUpdatedAt($value)
 * @method static Builder<static>|News whereUserId($value)
 * @method static Builder<static>|News whereYoutubeLink($value)
 * @method static Builder<static>|News withTrashed(bool $withTrashed = true)
 * @method static Builder<static>|News withoutTrashed()
 * @mixin \Eloquent
 */
class News extends Model
{

    use SoftDeletes, HasGallery, HasXenforoUserId;

    protected $table = 'news';

    protected $fillable = [
        'title',
        'slug',
        'description',
        'state',
        'category_id',
        'entry_id',
        'relevant_link',
        'youtube_link',
        'user_id',
        'comments_thread_id',
        'staff_comment',
        'rejected_at',
        'created_at'
    ];


    protected $casts = [
        'rejected_at' => 'datetime',
    ];

    public function scopePublished(Builder $query): Builder
    {
        return $query->where('state', 'published' );
    }

    public function scopeInQueue( Builder $query, int $daysRejected = 7 ): Builder
    {
        return $query->withTrashed()->where(function($q) use($daysRejected) {
            $q->where('state', 'pending')->whereNull('deleted_at');
        })->orWhere(function($q) use($daysRejected) {
            $q->where('state', 'rejected')->whereNotNull('rejected_at')->where('rejected_at', '>=', now()->subDays($daysRejected) );
        });
    }

    public function entry(): BelongsTo
    {
        return $this->belongsTo(Entry::class);
    }

    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class);
    }

    public function getDescriptionHtmlAttribute(): string
    {
        $converter = new GithubFlavoredMarkdownConverter([
            'html_input'         => 'strip',
            'allow_unsafe_links' => false,
        ]);

        return $converter->convert($this->description)->getContent();
    }

    public function getYoutubeVideoId(): ?string {
        if( !$this->youtube_link )
            return null;

        return EntryHelpers::getYoutubeVideoId( $this->youtube_link );
    }

}
