<?php

namespace App\Controllers;

use App\Core\Auth;
use App\Core\Request;
use App\Core\Response;
use App\Core\Validator;
use App\Repositories\TournamentRepository;
use App\Services\DemoScoresheetSeeder;

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

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

    public function demoScoresheet(array $params): void
    {
        Auth::requireRole(['admin']);
        $data = Request::json();
        Response::json((new DemoScoresheetSeeder())->seed((int) $params['id'], (bool) ($data['force'] ?? false)), 201);
    }
}
