<?php
declare(strict_types=1);

namespace App\Modules\TrainerPortal\Controllers\Api;

use App\Core\ApiController;
use App\Core\App;
use App\Core\Request;
use App\Core\Response;
use App\Modules\TrainerPortal\Services\ReplacementService;

final class ReplacementController extends ApiController
{
    private ReplacementService $replacementService;

    public function __construct(Request $request)
    {
        parent::__construct();
        $this->request = $request;
        $this->replacementService = new ReplacementService();
    }

    public function available(Request $request): Response
    {
        $coach = App::getInstance()->currentCoach();
        $requests = $this->replacementService->getAvailableRequests((int) $coach->id);
        return $this->successResponse($requests);
    }

    public function volunteer(Request $request, string $id): Response
    {
        $coach = App::getInstance()->currentCoach();
        $result = $this->replacementService->volunteer((int) $id, (int) $coach->id);

        if (isset($result['error'])) {
            return $this->errorResponse($result['error'], 422);
        }

        return $this->successResponse($result);
    }
}
