<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Spatie\Feed\Feedable;
use Spatie\Feed\FeedItem;

/**
 * Class Post
 *
 * @property int $id
 * @property string|null $title
 * @property string|null $slug
 * @property string|null $type
 * @property string|null $excerpt
 * @property int|null $author_id
 * @property bool $featured
 * @property string|null $featured_image
 * @property string|null $body
 * @property int $views_count
 * @property string $status
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property Author|null $author
 * @property Collection|PostCategory[] $post_categories
 * @property Carbon $published_at
 */
class Post extends Model implements Feedable
{
    protected $table = 'posts';

    protected $casts = [
        'author_id' => 'int',
        'featured' => 'bool',
        'views_count' => 'int',
        'keywords' => 'array',
        'published_at' => 'datetime',
    ];

    protected $fillable = [
        'title',
        'short_title',
        'slug',
        'type',
        'excerpt',
        'author_id',
        'featured',
        'featured_image',
        'body',
        'views_count',
        'status',
        'main_keyword',
        'keywords',
        'published_at',
    ];

    public function getFeaturedImageLqipCdnAttribute()
    {
        if (! is_empty($this->featured_image)) {
            // Get the extension of the original featured image
            $extension = pathinfo($this->featured_image, PATHINFO_EXTENSION);

            // Append "_lqip" before the extension to create the LQIP image URL
            $lqipFeaturedImage = str_replace(".{$extension}", '_lqip.webp', $this->featured_image);

            return 'https://'.Storage::disk('r2')->url($lqipFeaturedImage).'?a=bc';

        }

        return null;
    }

    public function getFeaturedImageCdnAttribute()
    {
        if (! is_empty($this->featured_image)) {
            return 'https://'.Storage::disk('r2')->url($this->featured_image);
        }

        return null;
    }

    public function author()
    {
        return $this->belongsTo(Author::class);
    }

    public function category()
    {
        return $this->hasOneThrough(
            Category::class,      // The target model
            PostCategory::class, // The through model
            'post_id',           // The foreign key on the through model
            'id',                // The local key on the parent model (Post)
            'id',                // The local key on the through model (PostCategory)
            'category_id'        // The foreign key on the target model (Category)
        );
    }

    public function toFeedItem(): FeedItem
    {
        return FeedItem::create([
            'id' => $this->id,
            'title' => $this->title,
            'summary' => $this->excerpt,
            'updated' => $this->updated_at,
            'link' => route('front.post', ['slug' => $this->slug, 'category_slug' => $this->category->slug]),
            'authorName' => optional($this->author)->name,
        ]);
    }

    public static function getFeedItems()
    {
        return self::with('category')->where('status', 'publish')->latest('published_at')->take(20)->get();
    }
}
