<?php

namespace App\Http\Resources\Api;

class CoachSessionResource extends BaseResource
{
    protected function defaultData(): array
    {
        return [
            'id'               => $this->id,
            'session_date'     => $this->session_date?->format('Y-m-d'),
            'start_time'       => $this->start_time ? substr($this->start_time, 0, 5) : null,
            'end_time'         => $this->end_time ? substr($this->end_time, 0, 5) : null,
            'status'           => $this->status,
            'status_label'     => $this->statusLabel(),
            'status_color'     => $this->statusColor(),
            'registered_count' => $this->registered_count,
            'game'             => $this->whenLoaded('game', fn () => [
                'id'   => $this->game->id,
                'name' => $this->game->name,
            ]),
            'court'            => $this->whenLoaded('court', fn () => [
                'id'   => $this->court->id,
                'name' => $this->court->name,
            ]),
            'academy'          => $this->whenLoaded('academy', fn () => [
                'id'   => $this->academy->id,
                'name' => $this->academy->name,
            ]),
        ];
    }

    protected function forShowData(): array
    {
        $data = $this->defaultData();

        $data['attendances'] = $this->whenLoaded('attendances', function () {
            return SessionAttendanceResource::collection($this->attendances);
        });

        return $data;
    }
}
