/**
 * @ai-summary Fastify routes for vehicles API
 * @ai-context Route definitions with TypeBox validation and authentication
 */

import { FastifyInstance, FastifyPluginOptions } from 'fastify';
import { FastifyPluginAsync } from 'fastify';
import {
  CreateVehicleBody,
  UpdateVehicleBody,
  VehicleParams
} from '../domain/vehicles.types';
import { VehiclesController } from './vehicles.controller';

export const vehiclesRoutes: FastifyPluginAsync = async (
  fastify: FastifyInstance,
  _opts: FastifyPluginOptions
) => {
  const vehiclesController = new VehiclesController();

  // GET /api/vehicles - Get user's vehicles
  fastify.get('/vehicles', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.getUserVehicles.bind(vehiclesController)
  });

  // POST /api/vehicles - Create new vehicle
  fastify.post<{ Body: CreateVehicleBody }>('/vehicles', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.createVehicle.bind(vehiclesController)
  });

  // Hierarchical Vehicle API - mirrors MVP Platform Vehicles Service structure
  // IMPORTANT: Register specific routes BEFORE dynamic :id routes to avoid conflicts

  // GET /api/vehicles/dropdown/years - Available model years
  fastify.get('/vehicles/dropdown/years', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.getDropdownYears.bind(vehiclesController)
  });

  // GET /api/vehicles/dropdown/makes?year=2024 - Get makes for year (Level 1)
  fastify.get<{ Querystring: { year: number } }>('/vehicles/dropdown/makes', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.getDropdownMakes.bind(vehiclesController)
  });

  // GET /api/vehicles/dropdown/models?year=2024&make=Ford - Get models for year/make (Level 2)
  fastify.get<{ Querystring: { year: number; make: string } }>('/vehicles/dropdown/models', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.getDropdownModels.bind(vehiclesController)
  });

  // GET /api/vehicles/dropdown/trims?year=2024&make=Ford&model=F-150 - Get trims (Level 3)
  fastify.get<{ Querystring: { year: number; make: string; model: string } }>('/vehicles/dropdown/trims', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.getDropdownTrims.bind(vehiclesController)
  });

  // GET /api/vehicles/dropdown/engines?year=2024&make=Ford&model=F-150&trim=XLT - Get engines (Level 4)
  fastify.get<{ Querystring: { year: number; make: string; model: string; trim: string } }>('/vehicles/dropdown/engines', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.getDropdownEngines.bind(vehiclesController)
  });

  // GET /api/vehicles/dropdown/transmissions?year=2024&make=Ford&model=F-150&trim=XLT - Get transmissions (Level 4, trim-filtered)
  fastify.get<{ Querystring: { year: number; make: string; model: string; trim: string } }>('/vehicles/dropdown/transmissions', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.getDropdownTransmissions.bind(vehiclesController)
  });

  // GET /api/vehicles/dropdown/options?year&make&model&trim[&engine=...][&transmission=...] - Pair-safe options for engine/transmission
  fastify.get<{ Querystring: { year: number; make: string; model: string; trim: string; engine?: string; transmission?: string } }>('/vehicles/dropdown/options', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.getDropdownOptions.bind(vehiclesController)
  });

  // POST /api/vehicles/decode-vin - Decode VIN using NHTSA vPIC API (Pro/Enterprise only)
  fastify.post<{ Body: { vin: string } }>('/vehicles/decode-vin', {
    preHandler: [fastify.authenticate, fastify.requireTier({ featureKey: 'vehicle.vinDecode' })],
    handler: vehiclesController.decodeVin.bind(vehiclesController)
  });

  // Vehicle image routes - must be before :id to avoid conflicts
  // POST /api/vehicles/:id/image - Upload vehicle image
  fastify.post<{ Params: VehicleParams }>('/vehicles/:id/image', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.uploadImage.bind(vehiclesController)
  });

  // GET /api/vehicles/:id/image - Download vehicle image
  fastify.get<{ Params: VehicleParams }>('/vehicles/:id/image', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.downloadImage.bind(vehiclesController)
  });

  // DELETE /api/vehicles/:id/image - Delete vehicle image
  fastify.delete<{ Params: VehicleParams }>('/vehicles/:id/image', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.deleteImage.bind(vehiclesController)
  });

  // GET /api/vehicles/:id/tco - Get vehicle Total Cost of Ownership
  fastify.get<{ Params: VehicleParams }>('/vehicles/:id/tco', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.getTCO.bind(vehiclesController)
  });

  // Dynamic :id routes MUST come last to avoid matching specific paths like "dropdown"
  // GET /api/vehicles/:id - Get specific vehicle
  fastify.get<{ Params: VehicleParams }>('/vehicles/:id', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.getVehicle.bind(vehiclesController)
  });

  // PUT /api/vehicles/:id - Update vehicle
  fastify.put<{ Params: VehicleParams; Body: UpdateVehicleBody }>('/vehicles/:id', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.updateVehicle.bind(vehiclesController)
  });

  // DELETE /api/vehicles/:id - Delete vehicle
  fastify.delete<{ Params: VehicleParams }>('/vehicles/:id', {
    preHandler: [fastify.authenticate],
    handler: vehiclesController.deleteVehicle.bind(vehiclesController)
  });
};

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