<?php

namespace App\Controllers;

use App\Core\Auth;
use App\Core\Request;
use App\Core\Response;
use App\Core\Validator;
use App\Repositories\TeamRepository;

final class TeamController
{
    public function index(): void
    {
        Response::json((new TeamRepository())->all());
    }

    public function store(): void
    {
        Auth::requireRole(['admin']);
        $data = Request::json();
        $errors = Validator::require($data, ['tournament_id', 'name']);
        if ($errors) {
            Response::error('Datos inválidos', 422, $errors);
            return;
        }
        Response::json((new TeamRepository())->create($data), 201);
    }
}
