<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\FootballMatchResource;
use App\Interfaces\FootballMatchRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class FootballMatchController extends Controller
{
    public function __construct(
        private readonly FootballMatchRepositoryInterface $matchRepository
    ) {}

    public function index(Request $request): JsonResponse
    {
        $status = $request->query('status');  // ?status=upcoming|live|finished

        $matches = match ($status) {
            'upcoming' => $this->matchRepository->getUpcoming(),
            'live'     => $this->matchRepository->getLive(),
            'finished' => $this->matchRepository->getFinished(),
            default    => $this->matchRepository->all(),
        };

        return response()->json([
            'matches' => FootballMatchResource::collection($matches),
        ]);
    }

    public function show(int $id): JsonResponse
    {
        $match = $this->matchRepository->findById($id);

        if (! $match) {
            return response()->json(['message' => 'Match not found.'], 404);
        }

        return response()->json(['match' => new FootballMatchResource($match)]);
    }
}
