<?php

namespace App\Http\Resources;

class ProjectResource extends BaseResource
{
    public function toArray($request): array
    {
        return [
            'id' => $this->id,
            'code' => $this->code,
            'title' => $this->title,
            'status_id' => $this->status_id,
            'type_id' => $this->type_id,
            'status' => $this->whenLoaded('status', fn () => new ProjectStatusResource($this->status)),
            'type' => $this->whenLoaded('type', fn () => new ProjectTypeResource($this->type)),
            'approved_estimate' => $this->formatEstimate($this->approved_estimate),
            'forecasted_effort' => $this->forecasted_effort,
            'start_date' => $this->formatDate($this->start_date),
            'end_date' => $this->formatDate($this->end_date),
            'created_at' => $this->formatDate($this->created_at),
            'updated_at' => $this->formatDate($this->updated_at),
        ];
    }

    private function formatEstimate(?float $value): ?string
    {
        return $value !== null ? number_format((float) $value, 2, '.', '') : null;
    }
}
