<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\HourRegistration\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Maestro\Core\Modules\HourRegistration\Models\HourTimesheetLine;

/** @mixin HourTimesheetLine */
class HourTimesheetLineResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $durations = [];
        foreach (HourTimesheetLine::WEEKDAY_KEYS as $d) {
            $durations[$d] = round((float) $this->{'duration_'.$d}, 2);
        }

        return [
            'id' => $this->id,
            'registration_type' => $this->registration_type->value,
            'internal_category_id' => $this->internal_category_id,
            'customer_id' => $this->customer_id,
            'hour_project_id' => $this->hour_project_id,
            'hour_task_id' => $this->hour_task_id,
            'durations' => $durations,
            'line_approval_status' => $this->line_approval_status->value,
            'approved_by_id' => $this->approved_by_id,
            'approved_at' => $this->approved_at,
            'reject_reason' => $this->reject_reason,
            'description' => $this->description,
            'task_hour_project_id' => $this->hourTask?->hour_project_id,
        ];
    }
}
