import fetch from 'node-fetch'
import { logInfo, logError } from './log'
import { DeploymentPlatform, ContainerDeploymentRequest } from 'types'

export const Matchers = {
  host: host => ({ host: [host] }),
  path: path => ({ path: [path] })
}

const Handlers = {
  reverseProxy: upstreamPort => ({
    handler: 'reverse_proxy',
    upstreams: [{ dial: `localhost:${upstreamPort}` }]
  }),
  subroute: routes => ({
    handler: 'subroute',
    routes
  })
}

export class CaddyClient {

  static deploy(deploymentPlatform: DeploymentPlatform, deploymentRequest: ContainerDeploymentRequest) {

    const url = `${deploymentRequest.subdomain}.${deploymentPlatform.activeEnvironment}.${deploymentPlatform.domain}`

    logInfo({ message: `[CaddyClient] Updating caddy config to route ${url} to localhost:${deploymentRequest.hostPort}` })

    return fetch(`http://localhost:2019/config/apps/http/servers/${deploymentPlatform.ciServerName}/routes/0/handle`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(Handlers.subroute([
        {
          match: [Matchers.host(url)],
          handle: [Handlers.subroute([{ handle: [Handlers.reverseProxy(deploymentRequest.hostPort)]}])]
        }
      ]))
    })
    .catch(error => logError({  message: `[CaddyClient] Could not update caddy config to include ${url}`, error }))
  }

  static applyDefaultServerConfig(deploymentPlatform: DeploymentPlatform) {
    logInfo({  message: '[CaddyClient] Applying default caddy config' })
    return fetch('http://localhost:2019/load', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        apps: {
          http: {
            servers: {
              [deploymentPlatform.ciServerName]: {
                listen: [':80', ':443'],
                routes: [
                  {
                    match: [Matchers.host(`*.${deploymentPlatform.activeEnvironment}.${deploymentPlatform.domain}`)],
                    handle: [Handlers.subroute([
                      {
                        match: [Matchers.host(`${deploymentPlatform.ciSubDomain}.${deploymentPlatform.activeEnvironment}.${deploymentPlatform.domain}`)],
                        handle: [Handlers.subroute([{ handle: [Handlers.reverseProxy(deploymentPlatform.ciInternalServerPort)]}])]
                      }
                    ])],
                    terminal: true
                  }
                ]
              }
            }
          },
          tls: {
            automation: {
              policies: [
                {
                  subjects:['*.qa.ossy.com'],
                  issuers:[
                    {
                      challenges:{
                        dns:{
                          provider:{
                            'max_retries': 10,
                            name: 'route53',
                            'aws_profile': 'ci-client'
                          }
                        }
                      },
                      module: 'acme'
                    },
                    {
                      challenges:{
                        dns:{
                          provider:{
                            'max_retries': 10,
                            name: 'route53'
                          }
                        }
                      },
                      module: 'zerossl'
                    }
                  ]
                }
              ]
            }
          }
        }
      })
    })
      .catch(error => logError({  message: '[CaddyClient] Could not apply default caddy config', error }))
  }

}
