const idrErr = require('../../utils/error')
const getConstants = require('../../constants').getConstants

class CveIdControllerError extends idrErr.IDRError {
  invalidState (state) { // cve-id
    const err = {}
    err.error = 'INVALID_STATE'
    err.message = `State '${state}' is invalid.`
    return err
  }

  invalidReservedState () { // cve-id
    const err = {}
    err.error = 'INVALID_RESERVED_STATE'
    err.message = 'Cannot change the state to RESERVED.'
    return err
  }

  invalidAmount () { // cve-id
    const err = {}
    err.error = 'INVALID_AMOUNT'
    err.message = 'The amount query parameter needs to be above 0.'
    return err
  }

  noAmount () { // cve-d
    const err = {}
    err.error = 'NO_AMOUNT'
    err.message = 'The amount query parameter is required and needs to be above 0.'
    return err
  }

  overNonSequentialMaxAmount () { // cve-id
    const err = {}
    const CONSTANTS = getConstants()
    err.error = 'OVER_NONSEQUENTIAL_MAX_AMOUNT'
    err.message = 'The amount query parameter exceeds the maximum amount allowed. Only amounts of ' + CONSTANTS.NONSEQUENTIAL_MAX_AMOUNT + ' ids or less can be reserved at a time.'
    return err
  }

  noYear () { // cve-id
    const err = {}
    err.error = 'NO_YEAR'
    err.message = 'The cve_year query parameter is required [1999-present+1].'
    return err
  }

  noBatchType () { // cve-id
    const err = {}
    err.error = 'NO_BATCH_TYPE'
    err.message = 'The batch_type query parameter must be specified when reserving more than one CVE ID. Available values are: sequential, nonsequential, non-sequential.'
    return err
  }

  noOrgShortName () { // cve-id
    const err = {}
    err.error = 'NO_ORG_SHORTNAME'
    err.message = 'The short_name query parameter is required to reserve CVE IDs.'
    return err
  }

  orgCannotReserveForOther () { // cve-id
    const err = {}
    const CONSTANTS = getConstants()
    err.error = 'ORG_CANNOT_RESERVE_FOR_OTHER'
    err.message = 'The organization designated by the ' + CONSTANTS.AUTH_HEADERS.ORG + ' header is not allowed to reserve IDs for the organization specified by the short_name query parameter.'
    return err
  }

  invalidBatchType () { // cve-id
    const err = {}
    err.error = 'INVALID_BATCH_TYPE'
    err.message = 'The batch_type provided is invalid. Available values are sequential, nonsequential, non-sequential.'
    return err
  }

  cannotReserveForYear (year) { // cve-id
    const err = {}
    err.error = 'CANNOT_RESERVE_FOR_YEAR'
    err.message = `IDR is not currently configured to reserve IDs for year ${year}. If you believe you are receiving this message in error, please contact an administrator.`
    return err
  }

  yearRangeFull (year) { // cve-id
    const err = {}
    err.error = 'YEAR_RANGE_FULL'
    err.message = `IDR cannot currently reserve IDs for year ${year} as the range it's allowed to reserve from is full. Please contact an administrator to have the range extended.`
    return err
  }

  yearRangeExists (year) { // cve-id
    const err = {}
    err.error = 'YEAR_RANGE_EXISTS'
    err.message = `CVE Id Range document for year ${year} was not created because it already exists.`
    return err
  }

  reservationInProgress () { // cve-id
    const err = {}
    err.error = 'RESERVATION_IN_PROGRESS'
    err.message = 'An ID reservation request for this organization is already in progress.'
    return err
  }

  overIdQuota (details) { // cve-id
    const err = {}
    err.error = 'EXCEEDED_ID_QUOTA'
    err.message = 'The amount requested would exceed the organization\'s ID quota. No more IDs can be reserved until the number of IDs in the Reserved state goes below the ID quota or the ID quota is raised. If you feel you are receiving this message in error, please contact support here: https://cveform.mitre.org/'
    err.details = details
    return err
  }

  reservedPartialAmount (amount, ids) { // cve-id
    const err = {}
    err.error = 'RESERVED_PARTIAL_AMOUNT'
    err.message = 'Only a partial amount of CVE IDs were reserved because there are not enough IDs available in the configured range for IDR to reserve the full amount.'
    err.details = {}
    err.details.amount_reserved = amount
    err.cve_ids = ids
    return err
  }

  cannotChangeCveIdWithRecord (id) {
    const err = {
      error: 'CANNOT_CHANGE_CVE_ID_WITH_RECORD',
      message: `A record was found for ${id}. A CVE ID cannot be changed once a record exists for it. Instead, it changes according to the record.`
    }
    return err
  }

  modifyCveIdNoQuota () {
    const err = {
      error: 'TRANSITION_STATE_RESERVED_NO_QUOTA',
      message: 'A CVE ID state cannot be changed to RESERVED if the owning organization has no remaining quota.'
    }
    return err
  }
}

module.exports = {
  CveIdControllerError
}
