import { Body, Controller, Post, Get, Query, Param } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiQuery, ApiResponse } from '@nestjs/swagger';
import { ConfigService } from '@nestjs/config';
import { FareEngineService } from './fare-engine.service';
import { FareCalculateDto, FareBreakdownDto } from './fare-engine.dto';
import { FaydaConfig } from '../../config/fayda.config';

@ApiTags('Fare Engine')
@Controller('fare-engine')
export class FareEngineController {
  constructor(
    private service: FareEngineService,
    private configService: ConfigService,
  ) {}

  @Post('calculate')
  @ApiOperation({
    summary: 'Calculate fare for a journey leg',
    description: `Computes fare using the formula:\n\n**Fare = totalKm × ratePerKm × exchangeRate**`,
  })
  @ApiResponse({ status: 201, type: FareBreakdownDto, description: 'Full fare breakdown with calculation trace' })
  @ApiResponse({ status: 400, description: 'Invalid route/station combination' })
  @ApiResponse({ status: 404, description: 'Route or seat class not found' })
  calculate(@Body() dto: FareCalculateDto) {
    return this.service.calculate(dto);
  }

  @Get('compare')
  @ApiOperation({
    summary: 'Compare fares across all seat classes for a route leg',
  })
  @ApiQuery({ name: 'routeId', description: 'Route UUID' })
  @ApiQuery({ name: 'originStationId', description: 'Origin station UUID' })
  @ApiQuery({ name: 'destinationStationId', description: 'Destination station UUID' })
  @ApiQuery({ name: 'nationality', required: false })
  @ApiQuery({ name: 'adultCount', required: false, type: Number })
  @ApiQuery({ name: 'childCount', required: false, type: Number })
  @ApiResponse({ status: 200, description: 'Array of fare breakdowns' })
  compareClasses(
    @Query('routeId') routeId: string,
    @Query('originStationId') originStationId: string,
    @Query('destinationStationId') destinationStationId: string,
    @Query('nationality') nationality?: string,
    @Query('adultCount') adultCount?: string,
    @Query('childCount') childCount?: string,
  ) {
    return this.service.compareClasses(
      routeId,
      originStationId,
      destinationStationId,
      nationality,
      adultCount ? parseInt(adultCount) : 1,
      childCount ? parseInt(childCount) : 0,
    );
  }


}

@ApiTags('Config')
@Controller('config')
export class ConfigController {
  constructor(private configService: ConfigService) {}

  @Get('fayda-status')
  @ApiOperation({ 
    summary: 'Check Verifayda 2.0 configuration status',
  })
  @ApiResponse({ 
    status: 200, 
    description: 'Verifayda status retrieved successfully',
  })
  getFaydaStatus() {
    const faydaConfig = this.configService.get<FaydaConfig>('fayda');
    const verifaydaEnabled = this.configService.get<boolean>('VERIFAYDA_ENABLED', false);
    
    return {
      enabled: faydaConfig?.enabled || verifaydaEnabled,
      mode: verifaydaEnabled ? 'production' : 'development',
      apiUrl: this.configService.get<string>('VERIFAYDA_API_URL', 'https://api.verifayda.gov.et/v2'),
    };
  }
}
