const { exec } = require("node:child_process")

class GetCategoryUseCase {
  execute(domain) {
    return new Promise((resolve, reject) => {
      exec(`echo ${domain} | bin/gcf1check.sh etc check_categorize_hybrid`, { cwd: '/usr/local/gcf1' }, (error, stdout, stderr) => {
        if (error) {
          console.error(error);
          reject(error);
          return;
        }

        const outputParts = stdout.split(/\t/);
        if (outputParts[6] !== '0') {
          resolve(outputParts[6])
        } else if (outputParts[3]) {
          resolve(outputParts[2]);
        } else {
          console.log({ error: 'Category ID not found' });
          reject('Category ID not found');
        }
      });
    });
  }
}

module.exports = { GetCategoryUseCase }

