<?php

namespace App\Http\Resources;

use App\Models\Article;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * @mixin Article
 */
class ArticleResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'feed_id' => $this->feed_id,
            'url' => $this->url,
            'title' => $this->title,
            'description' => $this->description,
            'validated_at' => $this->validated_at?->toISOString(),
            'is_published' => $this->relationLoaded('articlePublication') && $this->articlePublication !== null,
            'created_at' => $this->created_at->toISOString(),
            'updated_at' => $this->updated_at->toISOString(),
            'feed' => new FeedResource($this->whenLoaded('feed')),
            'article_publication' => new ArticlePublicationResource($this->whenLoaded('articlePublication')),
        ];
    }
}
