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

import { FastifyInstance, FastifyPluginOptions } from 'fastify';
import { FastifyPluginAsync } from 'fastify';
// Types handled in controllers; no explicit generics required here
import { FuelLogsController } from './fuel-logs.controller';
import { FuelGradeController } from './fuel-grade.controller';

export const fuelLogsRoutes: FastifyPluginAsync = async (
  fastify: FastifyInstance,
  _opts: FastifyPluginOptions
) => {
  const fuelLogsController = new FuelLogsController();
  const fuelGradeController = new FuelGradeController();

  // GET /api/fuel-logs - Get user's fuel logs
  fastify.get('/fuel-logs', {
    preHandler: [fastify.authenticate],
    handler: fuelLogsController.getUserFuelLogs.bind(fuelLogsController)
  });

  // POST /api/fuel-logs - Create new fuel log
  fastify.post('/fuel-logs', {
    preHandler: [fastify.authenticate],
    handler: fuelLogsController.createFuelLog.bind(fuelLogsController)
  });

  // GET /api/fuel-logs/:id - Get specific fuel log
  fastify.get('/fuel-logs/:id', {
    preHandler: [fastify.authenticate],
    handler: fuelLogsController.getFuelLog.bind(fuelLogsController)
  });

  // PUT /api/fuel-logs/:id - Update fuel log
  fastify.put('/fuel-logs/:id', {
    preHandler: [fastify.authenticate],
    handler: fuelLogsController.updateFuelLog.bind(fuelLogsController)
  });

  // DELETE /api/fuel-logs/:id - Delete fuel log
  fastify.delete('/fuel-logs/:id', {
    preHandler: [fastify.authenticate],
    handler: fuelLogsController.deleteFuelLog.bind(fuelLogsController)
  });

  // NEW ENDPOINTS under /api/fuel-logs
  fastify.get('/fuel-logs/vehicle/:vehicleId', {
    preHandler: [fastify.authenticate],
    handler: fuelLogsController.getFuelLogsByVehicle.bind(fuelLogsController)
  });

  fastify.get('/fuel-logs/vehicle/:vehicleId/stats', {
    preHandler: [fastify.authenticate],
    handler: fuelLogsController.getFuelStats.bind(fuelLogsController)
  });

  // Fuel type/grade discovery
  fastify.get('/fuel-logs/fuel-types', {
    preHandler: [fastify.authenticate],
    handler: fuelGradeController.getAllFuelTypes.bind(fuelGradeController)
  });

  fastify.get('/fuel-logs/fuel-grades/:fuelType', {
    preHandler: [fastify.authenticate],
    handler: fuelGradeController.getFuelGrades.bind(fuelGradeController)
  });
};

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