
import { fetcher } from '@cloudron/pankow';
import { API_ORIGIN } from '../constants.js';

const providers = [
  { name: 'AWS Route53', value: 'route53' },
  { name: 'Bunny', value: 'bunny' },
  { name: 'Cloudflare', value: 'cloudflare' },
  { name: 'deSEC', value: 'desec' },
  { name: 'DigitalOcean', value: 'digitalocean' },
  { name: 'DNSimple', value: 'dnsimple' },
  { name: 'Gandi LiveDNS', value: 'gandi' },
  { name: 'GoDaddy', value: 'godaddy' },
  { name: 'Google Cloud DNS', value: 'gcdns' },
  { name: 'Hetzner', value: 'hetzner' },
  { name: 'Hetzner Cloud', value: 'hetznercloud' },
  { name: 'INWX', value: 'inwx' },
  { name: 'Linode', value: 'linode' },
  { name: 'Name.com', value: 'namecom' },
  { name: 'Namecheap', value: 'namecheap' },
  { name: 'Netcup', value: 'netcup' },
  { name: 'OVH', value: 'ovh' },
  { name: 'Porkbun', value: 'porkbun' },
  { name: 'Vultr', value: 'vultr' },
  { name: 'Wildcard', value: 'wildcard' },
  { name: 'Manual (not recommended)', value: 'manual' },
  { name: 'No-op (only for development)', value: 'noop' }
];

function prettyProviderName(provider) {
  const tmp = providers.find(p => provider === p.value);
  return tmp ? tmp.name : provider;
};

function filterConfigForProvider(provider, config) {
  let props = [];
  switch (provider) {
    case 'route53':
      props = ['accessKeyId', 'secretAccessKey'];
      break;
    case 'gcdns':
      props = ['projectId', 'credentials'];
      break;
    case 'digitalocean':
      props = ['token'];
      break;
    case 'linode':
      props = ['token'];
      break;
    case 'bunny':
      props = ['accessKey'];
      break;
    case 'dnsimple':
      props = ['accessToken'];
      break;
    case 'hetzner':
    case 'hetznercloud':
      props = ['token'];
      break;
    case 'vultr':
      props = ['token'];
      break;
    case 'desec':
      props = ['token'];
      break;
    case 'gandi':
      props = ['token', 'tokenType'];
      break;
    case 'godaddy':
      props = ['apiKey', 'apiSecret'];
      break;
    case 'cloudflare':
      props = ['token', 'email', 'tokenType', 'defaultProxyStatus'];
      break;
    case 'namecom':
      props = ['token', 'username'];
      break;
    case 'namecheap':
      props = ['token', 'username'];
      break;
    case 'inwx':
      props = ['password', 'username'];
      break;
    case 'netcup':
      props = ['customerNumber', 'apiKey', 'apiPassword'];
      break;
    case 'ovh':
      props = ['endpoint', 'consumerKey', 'appKey', 'appSecret'];
      break;
    case 'porkbun':
      props = ['apikey', 'secretapikey'];
      break;
  }

  const ret = {
    customNameservers: !!config.customNameservers,
  };

  for (const p of props) ret[p] = config[p];
  return ret;
}

// creates an object with all potential properties for the UI forms to work with
function createEmptyConfig() {
  return {
    accessKeyId: '',
    accessKey: '',
    accessToken: '',
    apiKey: '',
    apikey: '',
    appSecret: '',
    apiPassword: '',
    apiSecret: '',
    consumerKey: '',
    credentials: { client_email: '', private_key: '' },
    customerNumber: '',
    defaultProxyStatus: false,
    customNameservers: false,
    email: '',
    endpoint: '',
    password: '',
    projectId: '',
    secretAccessKey: '',
    secretapikey: '',
    token: '',
    tokenType: '',
    username: '',
  };
}

function create() {
  const accessToken = localStorage.token;

  const exposed = {
    async list() {
      let error, result;
      try {
        result = await fetcher.get(`${API_ORIGIN}/api/v1/domains`, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error || result];
      return [null, result.body.domains];
    },
    async get(domain) {
      let error, result;
      try {
        result = await fetcher.get(`${API_ORIGIN}/api/v1/domains/${domain}`, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error || result];
      return [null, result.body];
    },
    async add(domain, zoneName, provider, config, fallbackCertificate, tlsConfig) {
      const data = { domain, provider, config: filterConfigForProvider(provider, config), tlsConfig };
      if (zoneName) data.zoneName = zoneName;
      if (fallbackCertificate) data.fallbackCertificate = fallbackCertificate;

      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/domains`, data, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 201) return [error || result];
      return [null];
    },
    async update(domain, zoneName, provider, config, fallbackCertificate, tlsConfig) {
      const data = { provider, config: filterConfigForProvider(provider, config), tlsConfig };
      if (zoneName) data.zoneName = zoneName;
      if (fallbackCertificate) data.fallbackCertificate = fallbackCertificate;

      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/domains/${domain}/config`, data, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 204) return [error || result];

      // this is done so that an out-of-sync dkim key can be synced
      [error] = await exposed.setDnsRecords({ domain: domain, type: 'mail' });
      if (error) console.error(error); // not fatal for now

      return [null];
    },
    async remove(domain) {
      let error, result;
      try {
        result = await fetcher.del(`${API_ORIGIN}/api/v1/domains/${domain}`, null, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 204) return [error || result];
      return [null];
    },
    async renewCerts(options) {
      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/reverseproxy/renew_certs`, { rebuild: !!options.rebuild }, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 202) return [error || result];
      return [null, result.body.taskId];
    },
    async setDnsRecords(options) {
      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/domains/sync_dns`, options, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 201) return [error || result];
      return [null, result.body.taskId];
    },
    async setWellKnown(domain, wellKnown) {
      // see WellKnownDialog.vue for the slightly complex wellKnown object
      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/domains/${domain}/wellknown`, { wellKnown }, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 204) return [error || result];
      return [null];
    },
    async checkRecords(domain, subdomain) {
      let error, result;
      try {
        result = await fetcher.get(`${API_ORIGIN}/api/v1/domains/${domain}/dns_check`, { subdomain, access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error || result];
      return [null, result.body];
    },
  };

  return exposed;
}

export default {
  create,
  createEmptyConfig,
  filterConfigForProvider,
  prettyProviderName,
  providers,
};
