import { FastifyReply, FastifyRequest } from 'fastify';
import { FuelGradeService } from '../domain/fuel-grade.service';
import { FuelType } from '../domain/fuel-logs.types';
import { logger } from '../../../core/logging/logger';

export class FuelGradeController {
  async getFuelGrades(
    request: FastifyRequest<{ Params: { fuelType: FuelType } }>,
    reply: FastifyReply
  ) {
    try {
      const { fuelType } = request.params;
      if (!Object.values(FuelType).includes(fuelType)) {
        return reply.code(400).send({ error: 'Bad Request', message: `Invalid fuel type: ${fuelType}` });
      }
      const grades = FuelGradeService.getFuelGradeOptions(fuelType);
      return reply.code(200).send({ fuelType, grades });
    } catch (error: any) {
      logger.error('Error getting fuel grades', { error });
      return reply.code(500).send({ error: 'Internal server error', message: 'Failed to get fuel grades' });
    }
  }

  async getAllFuelTypes(_request: FastifyRequest, reply: FastifyReply) {
    try {
      const fuelTypes = Object.values(FuelType).map(type => ({
        value: type,
        label: type.charAt(0).toUpperCase() + type.slice(1),
        grades: FuelGradeService.getFuelGradeOptions(type)
      }));
      return reply.code(200).send({ fuelTypes });
    } catch (error: any) {
      logger.error('Error getting fuel types', { error });
      return reply.code(500).send({ error: 'Internal server error', message: 'Failed to get fuel types' });
    }
  }
}

