'use strict'

const { BaseService } = require('..')

module.exports = class Maintenance extends BaseService {
  static get route() {
    return {
      base: 'maintenance',
      pattern: ':maintained/:year(\\d{4})',
    }
  }

  static get defaultBadgeData() {
    return {
      label: 'maintained',
    }
  }

  async handle({ maintained, year }) {
    const now = new Date()
    const cy = now.getUTCFullYear() // current year.
    const m = now.getUTCMonth() // month.

    if (maintained === 'no') {
      return this.constructor.render({ message: `no! (as of ${year})` })
    } else if (cy <= year) {
      return this.constructor.render({ message: maintained })
    } else if (parseInt(cy) === parseInt(year) + 1 && parseInt(m) < 3) {
      return this.constructor.render({ message: `stale (as of ${cy})` })
    } else {
      return this.constructor.render({ message: `no! (as of ${year})` })
    }
  }

  static render({ message }) {
    if (message.startsWith('yes')) {
      return {
        message,
        color: 'brightgreen',
      }
    } else if (message.startsWith('no')) {
      return {
        message,
        color: 'red',
      }
    } else {
      return { message }
    }
  }

  static get category() {
    return 'other'
  }
  static get examples() {
    return [
      {
        title: 'Maintenance',
        pattern: ':maintained/:year',
        namedParams: {
          maintained: 'yes',
          year: '2019',
        },
        staticPreview: this.render({ message: 'yes' }),
        keywords: ['maintained'],
      },
    ]
  }
}

@@ -15,16 +15,32 @@
 const %7B 
+NonMemoryCaching
 BaseServ
@@ -105,16 +105,32 @@
 extends 
+NonMemoryCaching
 BaseServ
@@ -1279,16 +1279,17 @@
 er'%0A  %7D%0A
+%0A
   static
@@ -1382,24 +1382,32 @@
 ':maintained
+(yes%7Cno)
 /:year',%0A   

Tweak [maintenance] badge in UI; remove caching (#3040)