const kleur = require('kleur');
const ui = require('../utils/ui');
const { printCompactHeader } = require('../utils/brand');
const { createTranslator, detectDefaultLanguage } = require('../utils/i18n');
const {
  AVAILABLE_BACKENDS,
  getVisibleFeatures,
  getBaseFeatures,
  BASE_FEATURES
} = require('../scaffold/catalog');

function findBaseDisplayName(id) {
  const base = BASE_FEATURES.find((f) => f.id === id);
  return base ? base.displayName : id;
}

const KASY_AUDIENCE = process.env.KASY_INTERNAL === '1' ? 'internal' : 'public';

function runFeatures(options = {}) {
  const t = createTranslator(options.language || detectDefaultLanguage());
  printCompactHeader(t);
  ui.intro(t('modules.backends'));

  const backendLines = AVAILABLE_BACKENDS.map((backend) =>
    `${kleur.green('✓')} ${kleur.white(backend.id)} - ${kleur.gray(
      t(`modules.backend.${backend.id}.description`)
    )}`
  );
  ui.log.message(backendLines.join('\n'));

  ui.log.step(kleur.bold(t('modules.featuresBase')));
  const baseLines = getBaseFeatures().map((feature) =>
    `${kleur.green('✓')} ${kleur.white(feature.displayName)} - ${kleur.gray(
      t(`modules.feature.base.${feature.id}.description`)
    )}`
  );
  ui.log.message(baseLines.join('\n'));

  ui.log.step(kleur.bold(t('modules.features')));
  const featureLines = getVisibleFeatures({ audience: KASY_AUDIENCE }).map((feature) => {
    const betaBadge = feature.status === 'internal' ? kleur.yellow(' [beta]') : '';
    const tagBadge = feature.tag ? kleur.yellow(` [${t(`modules.tag.${feature.tag}`)}]`) : '';
    const enhancesBadge = feature.enhances
      ? kleur.magenta(` [${t('modules.tag.enhances', { target: findBaseDisplayName(feature.enhances) })}]`)
      : '';
    return `${kleur.cyan('○')} ${kleur.white(feature.displayName)}${tagBadge}${enhancesBadge}${betaBadge} - ${kleur.gray(
      t(`modules.feature.${feature.id}.description`)
    )}`;
  });
  ui.log.message(featureLines.join('\n'));

  ui.outro('');
}

module.exports = {
  runFeatures,
  AVAILABLE_MODULES: AVAILABLE_BACKENDS
};
