<?php

namespace App\Http\Resources;

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

class PostResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'type' => $this->type,
            'title' => $this->title,
            'description' => $this->description,
            'price_type' => $this->price_type,
            'price_min' => $this->price_min,
            'price_max' => $this->price_max,
            'price_label' => $this->price_label,
            'location' => $this->location,
            'is_remote' => $this->is_remote,
            'deadline' => $this->deadline,
            'status' => $this->status,
            'user' => new UserResource($this->whenLoaded('user')),
            'category' => new CategoryResource($this->whenLoaded('category')),
            'images' => $this->whenLoaded('images', fn () => $this->images->map(fn ($img) => [
                'id' => $img->id,
                'url' => asset('storage/' . $img->image_path),
                'order' => $img->order,
            ])),
            'tags' => $this->whenLoaded('tags', fn () => $this->tags->pluck('tag')),
            'applications_count' => $this->whenCounted('applications'),
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}
