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

import { FastifyInstance, FastifyPluginOptions } from 'fastify';
import { FastifyPluginAsync } from 'fastify';
import { CommunityStationsController } from './community-stations.controller';
import {
  SubmitCommunityStationInput,
  NearbyStationsInput,
  StationIdInput,
  PaginationInput,
  BoundsStationsInput,
  RemovalReportInput
} from './community-stations.validation';

export const communityStationsRoutes: FastifyPluginAsync = async (
  fastify: FastifyInstance,
  _opts: FastifyPluginOptions
) => {
  const controller = new CommunityStationsController();

  // User endpoints (require JWT authentication)

  // POST /api/stations/community - Submit new station
  fastify.post<{ Body: SubmitCommunityStationInput }>('/stations/community', {
    preHandler: [fastify.authenticate],
    handler: controller.submitStation.bind(controller)
  });

  // GET /api/stations/community/mine - Get user's submissions
  fastify.get<{ Querystring: PaginationInput }>('/stations/community/mine', {
    preHandler: [fastify.authenticate],
    handler: controller.getMySubmissions.bind(controller)
  });

  // DELETE /api/stations/community/:id - Withdraw pending submission
  fastify.delete<{ Params: StationIdInput }>('/stations/community/:id', {
    preHandler: [fastify.authenticate],
    handler: controller.withdrawSubmission.bind(controller)
  });

  // GET /api/stations/community/approved - List approved stations (public)
  fastify.get<{ Querystring: PaginationInput }>('/stations/community/approved', {
    preHandler: [fastify.authenticate],
    handler: controller.getApprovedStations.bind(controller)
  });

  // POST /api/stations/community/nearby - Find nearby approved stations
  fastify.post<{ Body: NearbyStationsInput }>('/stations/community/nearby', {
    preHandler: [fastify.authenticate],
    handler: controller.getNearbyStations.bind(controller)
  });

  // POST /api/stations/community/bounds - Find approved stations within map bounds
  fastify.post<{ Body: BoundsStationsInput }>('/stations/community/bounds', {
    preHandler: [fastify.authenticate],
    handler: controller.getStationsInBounds.bind(controller)
  });

  // POST /api/stations/community/:id/report-removal - Report station no longer has 93
  fastify.post<{ Params: StationIdInput; Body: RemovalReportInput }>('/stations/community/:id/report-removal', {
    preHandler: [fastify.authenticate],
    handler: controller.reportRemoval.bind(controller)
  });

  // Admin endpoints are registered in admin.routes.ts to avoid duplication
};

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