<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;

class ActualResource extends BaseResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'project_id' => $this->project_id,
            'team_member_id' => $this->team_member_id,
            'month' => $this->month?->format('Y-m'),
            'hours_logged' => $this->formatDecimal($this->hours_logged),
            'notes' => $this->notes,
            'variance' => [
                'allocated_hours' => isset($this->allocated_hours) ? $this->formatDecimal($this->allocated_hours) : null,
                'variance_percentage' => $this->variance_percentage !== null ? round($this->variance_percentage, 1) : null,
                'variance_indicator' => $this->variance_indicator ?? 'gray',
            ],
            'project' => $this->whenLoaded('project', fn () => new ProjectResource($this->project)),
            'team_member' => $this->whenLoaded('teamMember', fn () => new TeamMemberResource($this->teamMember)),
        ];
    }
}
