/**
 * @ai-summary Fastify routes for stations API
 * @ai-context Route definitions with Fastify plugin pattern and authentication
 */

import { FastifyInstance, FastifyPluginOptions } from 'fastify';
import { FastifyPluginAsync } from 'fastify';
import {
  StationSearchBody,
  SaveStationBody,
  StationParams,
  UpdateSavedStationBody
} from '../domain/stations.types';
import { StationsController } from './stations.controller';

export const stationsRoutes: FastifyPluginAsync = async (
  fastify: FastifyInstance,
  _opts: FastifyPluginOptions
) => {
  const stationsController = new StationsController();

  // POST /api/stations/search - Search nearby stations
  fastify.post<{ Body: StationSearchBody }>('/stations/search', {
    preHandler: [fastify.authenticate],
    handler: stationsController.searchStations.bind(stationsController)
  });

  // POST /api/stations/save - Save a station to user's favorites
  fastify.post<{ Body: SaveStationBody }>('/stations/save', {
    preHandler: [fastify.authenticate],
    handler: stationsController.saveStation.bind(stationsController)
  });

  // PATCH /api/stations/saved/:placeId - Update saved station metadata
  fastify.patch<{ Params: StationParams; Body: UpdateSavedStationBody }>(
    '/stations/saved/:placeId',
    {
      preHandler: [fastify.authenticate],
      handler: stationsController.updateSavedStation.bind(stationsController)
    }
  );

  // GET /api/stations/saved - Get user's saved stations
  fastify.get('/stations/saved', {
    preHandler: [fastify.authenticate],
    handler: stationsController.getSavedStations.bind(stationsController)
  });

  // DELETE /api/stations/saved/:placeId - Remove saved station
  fastify.delete<{ Params: StationParams }>('/stations/saved/:placeId', {
    preHandler: [fastify.authenticate],
    handler: stationsController.removeSavedStation.bind(stationsController)
  });

  // GET /api/stations/photo/:reference - Proxy for Google Maps photos
  fastify.get<{ Params: { reference: string } }>('/stations/photo/:reference', {
    preHandler: [fastify.authenticate],
    handler: stationsController.getStationPhoto.bind(stationsController)
  });
};

// For backward compatibility during migration
export function registerStationsRoutes() {
  throw new Error('registerStationsRoutes is deprecated - use stationsRoutes Fastify plugin instead');
}
