<?php

namespace RainLab\Blog\Classes;

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

class PostResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $locale = $request->get('locale');
        $path = Config::get('app.url').Config::get('cms.storage.media.path');
        
        return [
            'id' => $this->id,
            'title' => $this->getAttributeTranslated('title', $locale),
            'slug' => $this->getAttributeTranslated('slug', $locale),
            'excerpt' => $this->getAttributeTranslated('excerpt', $locale),
            'published_at' => $this->published_at->format('d.m.Y'),
            'type' => $this->type,
            'featured_images' => ImageResource::collection($this->featured_images),
            'video' => $path.$this->video,
            'content_html' => $this->getAttributeTranslated('content_html', $locale),
            'categories' => CategoryResource::collection($this->categories),
            'powerseo_title' => $this->getAttributeTranslated('powerseo_title', $locale),
            'powerseo_description' => $this->getAttributeTranslated('powerseo_description', $locale),
            'powerseo_keywords' => $this->getAttributeTranslated('powerseo_keywords', $locale),
        ];
    }
}
