/**
 * @ai-summary Fastify routes for maintenance API
 */
import { FastifyInstance, FastifyPluginAsync, FastifyPluginOptions } from 'fastify';
import { MaintenanceController } from './maintenance.controller';

export const maintenanceRoutes: FastifyPluginAsync = async (
  fastify: FastifyInstance,
  _opts: FastifyPluginOptions
) => {
  const ctrl = new MaintenanceController();
  const requireAuth = fastify.authenticate.bind(fastify);

  // Maintenance Records
  fastify.get('/maintenance/records', {
    preHandler: [requireAuth],
    handler: ctrl.listRecords.bind(ctrl)
  });

  fastify.get<{ Params: any }>('/maintenance/records/:id', {
    preHandler: [requireAuth],
    handler: ctrl.getRecord.bind(ctrl)
  });

  fastify.get<{ Params: any }>('/maintenance/records/vehicle/:vehicleId', {
    preHandler: [requireAuth],
    handler: ctrl.getRecordsByVehicle.bind(ctrl)
  });

  fastify.post<{ Body: any }>('/maintenance/records', {
    preHandler: [requireAuth],
    handler: ctrl.createRecord.bind(ctrl)
  });

  fastify.put<{ Params: any; Body: any }>('/maintenance/records/:id', {
    preHandler: [requireAuth],
    handler: ctrl.updateRecord.bind(ctrl)
  });

  fastify.delete<{ Params: any }>('/maintenance/records/:id', {
    preHandler: [requireAuth],
    handler: ctrl.deleteRecord.bind(ctrl)
  });

  // Maintenance Schedules
  fastify.get<{ Params: any }>('/maintenance/schedules/vehicle/:vehicleId', {
    preHandler: [requireAuth],
    handler: ctrl.getSchedulesByVehicle.bind(ctrl)
  });

  fastify.post<{ Body: any }>('/maintenance/schedules', {
    preHandler: [requireAuth],
    handler: ctrl.createSchedule.bind(ctrl)
  });

  fastify.put<{ Params: any; Body: any }>('/maintenance/schedules/:id', {
    preHandler: [requireAuth],
    handler: ctrl.updateSchedule.bind(ctrl)
  });

  fastify.delete<{ Params: any }>('/maintenance/schedules/:id', {
    preHandler: [requireAuth],
    handler: ctrl.deleteSchedule.bind(ctrl)
  });

  // Utility Routes
  fastify.get<{ Params: { vehicleId: string }; Querystring: { currentMileage?: string } }>('/maintenance/upcoming/:vehicleId', {
    preHandler: [requireAuth],
    handler: ctrl.getUpcoming.bind(ctrl)
  });

  fastify.get<{ Params: any }>('/maintenance/subtypes/:category', {
    preHandler: [requireAuth],
    handler: ctrl.getSubtypes.bind(ctrl)
  });
};
