<?php

namespace App\Http\Resources;

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

class TaskResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'description' => $this->description,
            'project_id' => $this->project_id,
            'project' => new ProjectResource($this->whenLoaded('project')),
            'assignee_id' => $this->assignee_id,
            'assignee' => new UserResource($this->whenLoaded('assignee')),
            'reporter_id' => $this->reporter_id,
            'reporter' => new UserResource($this->whenLoaded('reporter')),
            'priority' => $this->priority,
            'status' => $this->status,
            'blocker_type' => $this->blocker_type,
            'blocker_note' => $this->blocker_note,
            'start_date' => $this->start_date?->format('Y-m-d'),
            'due_date' => $this->due_date?->format('Y-m-d'),
            'estimated_time' => $this->estimated_time,
            'actual_time' => $this->actual_time,
            'tags' => $this->tags,
            'sort_order' => $this->sort_order,
            'checklists' => ChecklistResource::collection($this->whenLoaded('checklists')),
            'subtasks' => SubtaskResource::collection($this->whenLoaded('subtasks')),
            'comments_count' => $this->when($this->comments_count !== null, $this->comments_count),
            'files_count' => $this->when($this->files_count !== null, $this->files_count),
            'created_by' => $this->created_by,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}
