<?php

namespace App\Domains\Staff\Resources;

use App\Domains\Auth\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/** @mixin User */
class StaffResource extends JsonResource
{
    /** @return array<string, mixed> */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'nombre_apellido' => $this->nombre_apellido,
            'dni' => $this->dni,
            'email' => $this->email,
            'rol_codigo' => $this->rol_codigo,
            'role' => $this->whenLoaded('role', fn () => [
                'codigo' => $this->role?->codigo,
                'nombre' => $this->role?->nombre,
            ]),
            'categories' => $this->whenLoaded('scanCategories', fn () => $this->scanCategories
                ->map(fn ($category) => [
                    'id' => $category->id,
                    'nombre' => $category->nombre,
                ])->values()),
        ];
    }
}
