<?php

declare(strict_types=1);

namespace App\Http\Resources;

use App\Models\Group;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * @mixin Group
 */
class GroupResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'branch' => $this->whenLoaded('branch', fn () => $this->branch ? [
                'id' => $this->branch->id,
                'name' => $this->branch->name,
                'code' => $this->branch->code,
            ] : null),
            'name' => $this->name,
            'type' => $this->type,
            'description' => $this->description,
            'leader_member' => $this->whenLoaded('leaderMember', fn () => $this->leaderMember ? [
                'id' => $this->leaderMember->id,
                'full_name' => trim("{$this->leaderMember->first_name} {$this->leaderMember->last_name}"),
            ] : null),
            'is_active' => $this->is_active,
            'members_count' => $this->whenCounted('members'),
            'created_at' => $this->created_at?->toIso8601String(),
            'updated_at' => $this->updated_at?->toIso8601String(),
        ];
    }
}
