<?php

namespace App\Http\Resources\Api;

use App\Enums\TeamMemberInvitationStatusEnum;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ChampionshipTeamResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'championship_id' => $this->championship_id,
            'group_id' => $this->group_id,
            'group_name' => $this->group?->name,
            'name' => $this->name,
            'logo' => $this->logo ? viewImage($this->logo) : null,
            'is_confirmed' => (bool) $this->is_confirmed,
            'created_by_member_id' => $this->created_by_member_id,
            'members_count' => $this->whenCounted('members'),
            'members' => $this->whenLoaded('members', function () {
                return $this->members->map(fn ($m) => [
                    'id' => $m->id,
                    'name' => $m->name,
                    'phone' => $m->phone,
                    'is_creator' => (int) $m->id === (int) $this->created_by_member_id,
                    'invitation_status' => (int) ($m->pivot->status ?? TeamMemberInvitationStatusEnum::ACCEPTED->value),
                    'invitation_status_label' => TeamMemberInvitationStatusEnum::label((int) ($m->pivot->status ?? TeamMemberInvitationStatusEnum::ACCEPTED->value)),
                    'responded_at' => $m->pivot->responded_at ?? null,
                ]);
            }),
        ];
    }
}

