/**
 * Brand resolver — reads this install's package.json once and exposes
 * the brand-specific names (bin, product, docs URL, data-dir suffix).
 *
 * Why this exists: the codebase ships as TWO separately-published npm
 * packages from the SAME source — `@loxia-labs/loxia-autopilot-one` and
 * `onbuzz`. The publish-onbuzz.bat script swaps `package.json` so its
 * `name` and `bin` reflect onbuzz before publishing. At runtime, each
 * installed binary has its own package.json file alongside; reading
 * THAT is the most reliable way to know which brand we are.
 *
 * Fallback policy: if package.json is missing or malformed, default to
 * autopilot — preserves existing behavior for legacy paths and unit-test
 * harnesses that run from the repo without a published package.
 *
 * Lazy + cached: read once on first call, then memoized. Tests can call
 * `_resetBrandCacheForTests()` to force a re-read with a different cwd
 * or mocked fs.
 */

import { readFileSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname  = path.dirname(__filename);

let cached = null;

/**
 * @typedef {object} BrandInfo
 * @property {string} binName        e.g. 'loxia' | 'onbuzz'
 * @property {string} productName    e.g. 'Loxia Autopilot One'
 * @property {string} shortName      e.g. 'Autopilot One' | 'OnBuzz'
 * @property {string} docsUrl        e.g. 'https://autopilot.loxia.ai'
 * @property {string} version        from package.json
 * @property {string} packageName    raw package.json `name`
 * @property {boolean} isAutopilot   true when binName === 'loxia'
 * @property {boolean} isOnBuzz      true when binName === 'onbuzz'
 *
 * NOTE: brand is COSMETIC ONLY. Both brands deliberately share the
 * same user data directory (agents, conversations, attachments) so an
 * agent fleet is visible to whichever brand the user happens to launch.
 * Do not introduce a brand-keyed appDataDir without product approval.
 */

/**
 * Resolve brand info from the package.json of the currently-running CLI.
 * @returns {BrandInfo}
 */
export function getBrand() {
  if (cached) return cached;

  // package.json is two levels above /src/utilities/.
  const pkgPath = path.join(__dirname, '..', '..', 'package.json');
  let pkg = {};
  try {
    pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
  } catch {
    // Missing / malformed → default everything below.
  }

  // The bin name is the only key under pkg.bin (or 'loxia' as the safe default).
  let binName = 'loxia';
  if (pkg.bin && typeof pkg.bin === 'object' && !Array.isArray(pkg.bin)) {
    const keys = Object.keys(pkg.bin);
    if (keys.length > 0) binName = keys[0];
  }

  const isOnBuzz    = binName === 'onbuzz';
  const isAutopilot = !isOnBuzz;

  const productName = isOnBuzz ? 'Loxia OnBuzz' : 'Loxia Autopilot One';
  const shortName   = isOnBuzz ? 'OnBuzz'       : 'Autopilot One';
  const docsUrl     = isOnBuzz ? 'https://onbuzz.loxia.ai' : 'https://autopilot.loxia.ai';

  cached = {
    binName,
    productName,
    shortName,
    docsUrl,
    version:     typeof pkg.version === 'string' ? pkg.version : '0.0.0',
    packageName: typeof pkg.name === 'string' ? pkg.name : '',
    isAutopilot,
    isOnBuzz,
  };
  return cached;
}

/** Test-only — clear the cache so a subsequent getBrand() re-reads. */
export function _resetBrandCacheForTests() { cached = null; }

export default { getBrand, _resetBrandCacheForTests };
