<?php

namespace App\Http\Resources\Api;

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

class ChampionshipResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'image' => $this->image ? viewImage($this->image) : null,
            'price' => $this->price,
            'game' => [
                'id' => $this->game?->id,
                'name' => $this->game?->name,
            ],
            'players_count' => $this->players_count,
            'status' => $this->status,
            'status_label' => ChampionshipStatusEnum::label($this->status),
            'teams_count' => $this->whenCounted('teams'),
            'teams' => ChampionshipTeamResource::collection($this->whenLoaded('teams')),
            'matches' => ChampionshipMatchResource::collection($this->whenLoaded('matches')),
            'groups' => $this->whenLoaded('groups', fn () => $this->groups->map(fn ($g) => [
                'id' => $g->id,
                'name' => $g->name,
            ])),
        ];
    }
}
