<?php

namespace App\Controller\Api\Stations;

use App\Entity;
use App\Exception\StationUnsupportedException;
use App\Http\Router;
use App\Http\ServerRequest;
use OpenApi\Annotations as OA;

class MountsController extends AbstractStationApiCrudController
{
    protected string $entityClass = Entity\StationMount::class;
    protected string $resourceRouteName = 'api:stations:mount';

    /**
     * @OA\Get(path="/station/{station_id}/mounts",
     *   tags={"Stations: Mount Points"},
     *   description="List all current mount points.",
     *   @OA\Parameter(ref="#/components/parameters/station_id_required"),
     *   @OA\Response(response=200, description="Success",
     *     @OA\JsonContent(type="array", @OA\Items(ref="#/components/schemas/StationMount"))
     *   ),
     *   @OA\Response(response=403, description="Access denied"),
     *   security={{"api_key": {}}},
     * )
     *
     * @OA\Post(path="/station/{station_id}/mounts",
     *   tags={"Stations: Mount Points"},
     *   description="Create a new mount point.",
     *   @OA\Parameter(ref="#/components/parameters/station_id_required"),
     *   @OA\RequestBody(
     *     @OA\JsonContent(ref="#/components/schemas/StationMount")
     *   ),
     *   @OA\Response(response=200, description="Success",
     *     @OA\JsonContent(ref="#/components/schemas/StationMount")
     *   ),
     *   @OA\Response(response=403, description="Access denied"),
     *   security={{"api_key": {}}},
     * )
     *
     * @OA\Get(path="/station/{station_id}/mount/{id}",
     *   tags={"Stations: Mount Points"},
     *   description="Retrieve details for a single mount point.",
     *   @OA\Parameter(ref="#/components/parameters/station_id_required"),
     *   @OA\Parameter(
     *     name="id",
     *     in="path",
     *     description="Streamer ID",
     *     required=true,
     *     @OA\Schema(type="integer", format="int64")
     *   ),
     *   @OA\Response(response=200, description="Success",
     *     @OA\JsonContent(ref="#/components/schemas/StationMount")
     *   ),
     *   @OA\Response(response=403, description="Access denied"),
     *   security={{"api_key": {}}},
     * )
     *
     * @OA\Put(path="/station/{station_id}/mount/{id}",
     *   tags={"Stations: Mount Points"},
     *   description="Update details of a single mount point.",
     *   @OA\RequestBody(
     *     @OA\JsonContent(ref="#/components/schemas/StationMount")
     *   ),
     *   @OA\Parameter(ref="#/components/parameters/station_id_required"),
     *   @OA\Parameter(
     *     name="id",
     *     in="path",
     *     description="Streamer ID",
     *     required=true,
     *     @OA\Schema(type="integer", format="int64")
     *   ),
     *   @OA\Response(response=200, description="Success",
     *     @OA\JsonContent(ref="#/components/schemas/Api_Status")
     *   ),
     *   @OA\Response(response=403, description="Access denied"),
     *   security={{"api_key": {}}},
     * )
     *
     * @OA\Delete(path="/station/{station_id}/mount/{id}",
     *   tags={"Stations: Mount Points"},
     *   description="Delete a single mount point.",
     *   @OA\Parameter(ref="#/components/parameters/station_id_required"),
     *   @OA\Parameter(
     *     name="id",
     *     in="path",
     *     description="StationMount ID",
     *     required=true,
     *     @OA\Schema(type="integer", format="int64")
     *   ),
     *   @OA\Response(response=200, description="Success",
     *     @OA\JsonContent(ref="#/components/schemas/Api_Status")
     *   ),
     *   @OA\Response(response=403, description="Access denied"),
     *   security={{"api_key": {}}},
     * )
     */

    protected function viewRecord(object $record, ServerRequest $request): mixed
    {
        $return = parent::viewRecord($record, $request);

        $station = $request->getStation();
        $frontend = $request->getStationFrontend();
        $router = $request->getRouter();

        $return['links']['listen'] = (string)Router::resolveUri(
            $router->getBaseUrl(),
            $frontend->getUrlForMount($station, $record),
            true
        );

        return $return;
    }

    /**
     * @inheritDoc
     */
    protected function getStation(ServerRequest $request): Entity\Station
    {
        $station = parent::getStation($request);

        $frontend = $request->getStationFrontend();
        if (!$frontend->supportsMounts()) {
            throw new StationUnsupportedException();
        }

        return $station;
    }
}
