<?php

namespace App\Http\Resources;

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

class BlogResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'slug' => $this->slug,
            'excerpt' => $this->excerpt,
            'content' => $this->content,
            'featured_image' => $this->featured_image,
            'status' => $this->status,
            'status_label' => ucfirst($this->status),
            'is_featured' => $this->is_featured,
            'is_featured_label' => $this->is_featured ? 'Featured' : 'Not Featured',
            'view_count' => $this->view_count,
            'published_at' => $this->published_at,
            'category' => [
                'id' => $this->category->id ?? null,
                'name' => $this->category->name ?? null,
                'slug' => $this->category->slug ?? null,
            ],
            'author' => [
                'id' => $this->author->id ?? null,
                'name' => ($this->author->firstname ?? '') . ' ' . ($this->author->lastname ?? ''),
                'email' => $this->author->email ?? null,
            ],
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}
