import { Controller, Get, Query, UseGuards, UseInterceptors } from '@nestjs/common';
import { ApiHeader, ApiTags } from '@nestjs/swagger';
import { PublicApiCryptoService } from '../common/crypto/public-api-crypto.service';
import { PublicApiKeyGuard } from '../common/guards/public-api-key.guard';
import { EncryptedResponseInterceptor } from '../common/interceptors/encrypted-response.interceptor';
import { LocationsService } from './locations.service';

@ApiTags('Locations')
@ApiHeader({ name: 'x-api-key-enc', required: true })
@UseGuards(PublicApiKeyGuard)
@UseInterceptors(EncryptedResponseInterceptor)
@Controller('locations')
export class LocationsController {
  constructor(
    private readonly locations: LocationsService,
    private readonly crypto: PublicApiCryptoService,
  ) {}

  @Get('countries')
  countries() {
    return this.locations.countries();
  }

  @Get('states')
  states(@Query('country') country: string, @Query('payload') payload?: string) {
    const decrypted = payload ? this.crypto.decrypt<{ country?: string }>(payload) : undefined;
    return this.locations.states(decrypted?.country ?? country);
  }
}
