<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class BlogPostDetailResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,

            'title' => $this->title,
            'slug' => $this->slug,

            'excerpt' => $this->excerpt,
            'content' => $this->content,

            'cover_image' => $this->cover_image,
            'cover_image_alt' => $this->cover_image_alt,

            'category' => $this->category,
            'tags' => $this->tags,

            'status' => $this->status,
            'is_featured' => $this->is_featured,

            'reading_time' => $this->reading_time,
            'view_count' => $this->view_count,

            'published_at' => $this->published_at?->toISOString(),

            'author' => $this->whenLoaded('author', function () {
                if (!$this->author) {
                    return null;
                }

                return [
                    'id' => $this->author->id,
                    'name' => $this->author->name,
                    'email' => $this->author->email,
                ];
            }),

            'meta' => $this->meta,
        ];
    }
}