<?php

namespace Modules\HR\Transformers;

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

class AttendanceResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,

            'employee_id' => $this->employee_id,
            'shift_id' => $this->shift_id,

            'date' => $this->date,
            'check_in' => $this->check_in,
            'check_out' => $this->check_out,

            'worked_minutes' => $this->worked_minutes,
            'late_minutes' => $this->late_minutes,
            'early_exit_minutes' => $this->early_exit_minutes,
            'overtime_minutes' => $this->overtime_minutes,

            'status' => $this->status,
            'is_locked' => (bool) $this->is_locked,

            'fiscal_year_id' => $this->fiscal_year_id,
            'accounting_period_id' => $this->accounting_period_id,

            'employee' => $this->whenLoaded('employee', function () {
                return [
                    'id' => $this->employee->id,
                    'name' => $this->employee->name,
                    'employee_code' => $this->employee->emp_number,
                    'status' => $this->employee->status,
                ];
            }),

            'shift' => $this->whenLoaded('shift', function () {
                return [
                    'id' => $this->shift->id,
                    'name' => $this->shift->name,
                ];
            }),

            'fiscal_year' => $this->whenLoaded('fiscalYear', function () {
                return [
                    'id' => $this->fiscalYear->id,
                    'name' => $this->fiscalYear->name,
                ];
            }),

            'accounting_period' => $this->whenLoaded('accountingPeriod', function () {
                return [
                    'id' => $this->accountingPeriod->id,
                    'name' => $this->accountingPeriod->name,
                ];
            }),

        ];
    }
}
