#!/usr/bin/env node

import fs from 'fs';
import path from 'path';
import superagent from '@cloudron/superagent';
import translations from '../dashboard/public/translation/en.json';

// get token from https://translate.cloudron.io/accounts/profile/#api
const token = '';

const flat = flattenObject(translations);
const keys = Object.keys(flat);

function flattenObject(obj, prefix = '', result = {}) {
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      const newKey = prefix ? `${prefix}.${key}` : key;

      if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) {
        // Rekursiver Aufruf für verschachtelte Objekte
        flattenObject(obj[key], newKey, result);
      } else {
        // Wert direkt zuweisen
        result[newKey] = obj[key];
      }
    }
  }
  return result;
}

async function listFiles(dir) {
    const files = await fs.promises.readdir(dir);
    let result = [];

    for (const file of files) {
        const fullPath = path.join(dir, file);
        const stat = await fs.promises.stat(fullPath);

        if (stat.isDirectory()) {
            result = result.concat(await listFiles(fullPath));
        } else if (stat.isFile()) {
            result.push(fullPath);
        }
    }

    return result;
}

async function grepFile(fullPath, searchString) {
    const content = await fs.promises.readFile(fullPath, 'utf-8');
    return content.includes(searchString);
}

async function findDuplicates() {
    const duplicates = [];
    for (const key of keys) {
        const value = flat[key];
        const dups = keys.filter(k => flat[k] === value && k !== key);
        if (dups.length === 0) continue;

        // skip double entries
        if (duplicates.find(d => d.value === value)) continue;

        duplicates.push({ key, value, dups });
    }

    console.log('Keys with the same values multiple times:');
    console.log('----------------------------------------------------------');
    duplicates.forEach(d => console.log(`${d.key}\n\t${d.dups.join(' \n\t')} \n  => "${d.value}"\n `));
    console.log('----------------------------------------------------------');
    console.log(' Count: ' + duplicates.length);
    console.log('----------------------------------------------------------\n\n');
}

async function findUnused() {
    const keyUsage = {};
    let files = await listFiles(path.resolve('./dashboard/src/'));
    files = files.concat(await listFiles(path.resolve('./src/')));

    for (const file of files) {
        const content = await fs.promises.readFile(file, 'utf-8');

        for (const key of keys) {
            if (!keyUsage[key]) keyUsage[key] = 0;
            if (content.includes(key)) keyUsage[key]++;
        }
    }
    const unusedKeys = Object.keys(keyUsage).filter(k => keyUsage[k] === 0)

    console.log('Unused keys:');
    console.log('----------------------------------------------------------');
    console.log(unusedKeys.join('\n'));
    console.log('----------------------------------------------------------');
    console.log(' Count: ' + unusedKeys.length);
    console.log('----------------------------------------------------------\n\n');

    return unusedKeys;
}

async function purge(key) {
    let result = await superagent.get('https://translate.cloudron.io/api/units/')
        .set('Authorization', `Token ${token}`)
        .query({q: `language:en AND component:dashboard AND key:${key}`});

    const id = result.body.results[0].id;

    console.log(` => deleting ${key} with id ${id}`);
    await superagent.del(`https://translate.cloudron.io/api/units/${id}/`)
        .set('Authorization', `Token ${token}`);

    console.log('    done');
}

(async () => {

console.log(`Found ${keys.length} strings`);
console.log('----------------------------------------------------------\n\n');

console.log('Toplevel keys to be moved:');
console.log('----------------------------------------------------------');
console.log(Object.keys(flat).find(k => k.indexOf('.') === -1));
console.log('----------------------------------------------------------\n\n');

await findDuplicates();
// const unused = await findUnused();

// for (const key of unused) {
//     await purge(key);
// }

})();
