<?php

namespace App\Http\Resources\Api\V1;

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

class ParticipantResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'uuid' => $this->uuid,
            'name_ar' => $this->person?->name_ar,
            'name' => $this->person?->name,
            'photo_url' => $this->photo_path ? asset('storage/' . $this->photo_path) : null,
            'status' => $this->status?->value,
            'age' => $this->age,
            'gender' => $this->person?->gender,
            'branch' => $this->whenLoaded('branch', fn () => [
                'id' => $this->branch->id,
                'name_ar' => $this->branch->name_ar,
            ]),
            'primary_activity' => $this->whenLoaded('primaryActivity', fn () => [
                'id' => $this->primaryActivity->id,
                'name_ar' => $this->primaryActivity->name_ar,
            ]),
            'active_enrollments' => $this->whenLoaded('activeEnrollments', fn () =>
                $this->activeEnrollments->map(fn ($e) => [
                    'id' => $e->id,
                    'program_name_ar' => $e->group?->program?->name_ar,
                    'group_name_ar' => $e->group?->name_ar,
                    'activity_name_ar' => $e->group?->program?->activity?->name_ar,
                ])
            ),
        ];
    }
}
