import {
  registerDecorator,
  ValidationArguments,
  ValidationOptions,
  ValidatorConstraint,
  ValidatorConstraintInterface
} from '@hmcts/class-validator'
import { Country } from 'common/country'
import { ErrorLogger } from 'logging/errorLogger'
import { AddressInfoResponse } from '@hmcts/os-places-client'
import { ClientFactory } from 'postcode-lookup/clientFactory'

const postcodeClient = ClientFactory.createOSPlacesClient()
const countryClient = ClientFactory.createPostcodeToCountryClient()

enum BlockedPostcodes {
  ISLE_OF_MAN = 'IM'
}

@ValidatorConstraint({ async: true })
export class CheckCountryConstraint implements ValidatorConstraintInterface {

  async validate (value: any | string, args?: ValidationArguments): Promise<boolean> {
    if (value === undefined || value === null || value === '') {
      return true
    }

    if (value.trim().toUpperCase().startsWith(BlockedPostcodes.ISLE_OF_MAN)) {
      return false
    }
import * as express from 'express'
import { AddressInfoResponse } from '@hmcts/os-places-client'

import { Paths as AppPaths } from 'paths'
import { Logger } from '@hmcts/nodejs-logging'
import { ClientFactory } from 'postcode-lookup/clientFactory'

const osPlacesClient = ClientFactory.createOSPlacesClient()
const logger = Logger.getLogger('postcode-lookup')

/* tslint:disable:no-default-export */
export default express.Router()
  .get(AppPaths.postcodeLookupProxy.uri, (req, res) => {
    if (!req.query.postcode || !req.query.postcode.trim()) {
      return res.status(400).json({
        error: {
          status: 400,
          message: 'Postcode not provided'
        }
      })
    }
    osPlacesClient.lookupByPostcode(req.query.postcode)
      .then((addressInfoResponse: AddressInfoResponse) => res.json(addressInfoResponse))
      .catch(err => {
        logger.error(err.stack)
        res.status(500).json({
          error: {
            status: 500,
            message: err.message
          }
        })