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

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

  return {
    async list() {
      const page = 1;
      const per_page = 1000;

      let error, result;
      try {
        result = await fetcher.get(`${API_ORIGIN}/api/v1/backup_sites`, { page, per_page, access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error || result];
      return [null, result.body.backupSites];
    },
    async add(name, format, contents, enableForUpdates, provider, config, schedule, retention, limits = null) {
      const data = { name, format, contents, enableForUpdates, provider, config, schedule, retention };

      if (limits !== null) data.limits = limits;

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

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

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

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

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

      if (error || result.status !== 202) return [error || result];
      return [null, result.body.taskId];
    },
    async setEnableForUpdates(id, enable) {
      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/enable_for_updates`, { enable }, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error || result];
      return [null];
    },
    async setRetention(id, retention) {
      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/retention`, { retention }, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error || result];
      return [null];
    },
    async setName(id, name) {
      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/name`, { name }, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error || result];
      return [null];
    },
    async setLimits(id, limits) {
      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/limits`, { limits }, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error || result];
      return [null];
    },
    async setSchedule(id, schedule) {
      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/schedule`, { schedule }, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error || result];
      return [null];
    },
    async setEncryption(id, encryptionPassword, encryptedFilenames, encryptionPasswordHint = '') {
      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/encryption`, { encryptionPassword, encryptedFilenames, encryptionPasswordHint }, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error || result];
      return [null];
    },
    async setConfig(id, config) {
      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/config`, { config }, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error || result];
      return [null];
    },
    async setContents(id, contents) {
      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/contents`, { contents }, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

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

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

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

      if (error || result.status !== 202) return [error || result];
      return [null, result.body];
    },
    async generateBackupConfig(backupOrArchive) {
      const [backupSiteError, backupSite] = await this.get(backupOrArchive.siteId);
      if (backupSiteError) return [backupSiteError];

      const config = {
        remotePath: backupOrArchive.remotePath
      };

      for (const k of ['provider', 'config', 'limits', 'format', 'encrypted', 'encryptedFilenames', 'encryptionPasswordHint']) {
        config[k] = backupSite[k];
      }

      config.siteId = backupSite.id;
      return [null, config];
    }
  };
}

const backupRetentions = [
  { name: '2 days', id: { keepWithinSecs: 2 * 24 * 60 * 60 }},
  { name: '1 week', id: { keepWithinSecs: 7 * 24 * 60 * 60 }}, // default
  { name: '1 month', id: { keepWithinSecs: 30 * 24 * 60 * 60 }},
  { name: '3 months', id: { keepWithinSecs: 3 * 30 * 24 * 60 * 60 }},
  { name: '2 daily, 4 weekly', id: { keepDaily: 2, keepWeekly: 4 }},
  { name: '3 daily, 4 weekly, 6 monthly', id: { keepDaily: 3, keepWeekly: 4, keepMonthly: 6 }},
  { name: '7 daily, 4 weekly, 12 monthly', id: { keepDaily: 7, keepWeekly: 4, keepMonthly: 12 }},
  { name: 'Forever', id: { keepWithinSecs: -1 }}
];

export default {
  create,
  backupRetentions,
};
