/**
 * DC-077: i18n route — serves translations and language metadata
 */
const express = require('express');
const { ok } = require('../src/utils/responses');
const i18n = require('../src/utilities/i18n');

module.exports = function() {
  const router = express.Router();

  // Language display names and RTL metadata for the full supported set.
  const NAMES = {en: "English",es: "Espa\u00f1ol",fr: "Fran\u00e7ais",de: "Deutsch",ar: "\u0627\u0644\u0639\u0631\u0628\u064a\u0629",bn: "\u09ac\u09be\u0982\u09b2\u09be",cs: "\u010ce\u0161tina",da: "Dansk",el: "\u0395\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac",fa: "\u0641\u0627\u0631\u0633\u06cc",fi: "Suomi",hi: "\u0939\u093f\u0928\u094d\u0926\u0940",hu: "Magyar",id: "Bahasa Indonesia",it: "Italiano",ja: "\u65e5\u672c\u8a9e",ko: "\ud55c\uad6d\uc5b4",ms: "Bahasa Melayu",nl: "Nederlands",no: "Norsk",pl: "Polski",pt: "Portugu\u00eas",ro: "Rom\u00e2n\u0103",ru: "\u0420\u0443\u0441\u0441\u043a\u0438\u0439",sv: "Svenska",th: "\u0e44\u0e17\u0e22",tr: "T\u00fcrk\u00e7e",uk: "\u0423\u043a\u0440\u0430\u0457\u043d\u0441\u044c\u043a\u0430",ur: "\u0627\u0631\u062f\u0648",vi: "Ti\u1ebfng Vi\u1ec7t",zh: "\u4e2d\u6587"};
  const RTL = new Set(['ar', 'fa', 'ur']);

  // GET /api/v1/i18n/languages — list supported languages
  router.get('/i18n/languages', (req, res) => {
    ok(res, {
      languages: i18n.getSupportedLanguages().map(code => ({
        code,
        name: NAMES[code] || code,
        rtl: RTL.has(code),
      })),
      default: i18n.DEFAULT_LANGUAGE,
    });
  });

  // GET /api/v1/i18n/translations/:lang — get all translations for a language
  router.get('/i18n/translations/:lang', (req, res) => {
    const lang = req.params.lang;
    if (!i18n.isSupported(lang)) {
      return res.status(400).json({
        success: false,
        error: `Unsupported language: ${lang}`,
        supported: i18n.getSupportedLanguages(),
      });
    }
    ok(res, { lang, translations: i18n.TRANSLATIONS[lang] || {} });
  });

  return router;
};
