// pathList.ts
// Scope: central path glossary and grouped path exports.
// This is path-only. No runtime authority, guards, or business rules.

import path from "node:path";

export const ROOT = process.cwd();

export const SettingsPaths = {
  dir: path.join(ROOT, "settings"),
  settings: path.join(ROOT, "settings", "settings.json"),
  user: path.join(ROOT, "settings", "user.json"),
  usage: path.join(ROOT, "settings", "usage.json"),
  projectIndex: path.join(ROOT, "settings", "projectIndex.json"),
  authToken: path.join(ROOT, "settings", ".auth_token"),
  authAccess: path.join(ROOT, "settings", ".auth_access"),
  authRefresh: path.join(ROOT, "settings", ".auth_refresh")
} as const;

export const RuntimePaths = {
  tempDir: path.join(ROOT, ".temp"),
  powerPid: path.join(ROOT, ".temp", "power.pid"),
  reports: path.join(ROOT, ".temp", "reports.json"),
  updateState: path.join(ROOT, ".temp", "update-state.json")
} as const;

export const LegacyEqPaths = {
  dir: path.join(ROOT, ".eq"),
  reports: path.join(ROOT, ".eq", "reports.json")
} as const;

export const DataPaths = {
  projectsDir: path.join(ROOT, "projects"),
  logsDir: path.join(ROOT, "logs"),
  scrapesDir: path.join(ROOT, "logs", "system", "scrapes"),
  systemUpdatesDir: path.join(ROOT, "logs", "system", "updates")
} as const;

export const SYSTEM_SERIAL = "system";

export function normalizeLogScope(serial?: string): string {
  const clean = String(serial || "").trim();
  return clean || SYSTEM_SERIAL;
}

export function getLogScopeDir(serial?: string): string {
  return path.join(DataPaths.logsDir, normalizeLogScope(serial));
}

export const UiPaths = {
  monitorHtml: path.join(ROOT, "monitor.html"),
  monitorCss: path.join(ROOT, "monitor.css"),
  monitorTabsCss: path.join(ROOT, "monitorTabs.css"),
  monitorTabsHtml: path.join(ROOT, "monitortabs.html"),
  monitorJs: path.join(ROOT, "monitor.js"),
  monitorProjectJs: path.join(ROOT, "monitorProject.js"),
  monitorLaunchGateJs: path.join(ROOT, "monitorLaunchGate.js"),
  mgrSnpJs: path.join(ROOT, "mgrSNP.js"),
  sysMenuJs: path.join(ROOT, "sysMenu.js"),
  dubUndoJs: path.join(ROOT, "dubUndo.js"),
  editorJs: path.join(ROOT, "editor.js"),
  projectSwitchJs: path.join(ROOT, "projectSwitch.js"),
  projectSwitchCss: path.join(ROOT, "projectSwitch.css"),
  projectOutlineJs: path.join(ROOT, "projectOutline.js"),
  assetsDir: path.join(ROOT, "assets"),
  favicon: path.join(ROOT, "favicon.ico"),
  assetFavicon: path.join(ROOT, "assets", "favicon.ico"),
  webmanifest: path.join(ROOT, "site.webmanifest"),
  nodaramaWhitePng: path.join(ROOT, "nodaramaWhite.png")
} as const;

export const StaticFileMap: Record<string, { filePath: string; type: string; label: string }> = {
  "/monitor.css": { filePath: UiPaths.monitorCss, type: "text/css", label: "monitor.css" },
  "/monitorTabs.css": { filePath: UiPaths.monitorTabsCss, type: "text/css", label: "monitorTabs.css" },
  "/monitor.js": { filePath: UiPaths.monitorJs, type: "application/javascript", label: "monitor.js" },
  "/mgrSNP.js": { filePath: UiPaths.mgrSnpJs, type: "application/javascript", label: "mgrSNP.js" },
  "/sysMenu.js": { filePath: UiPaths.sysMenuJs, type: "application/javascript", label: "sysMenu.js" },
  "/dubUndo.js": { filePath: UiPaths.dubUndoJs, type: "application/javascript", label: "dubUndo.js" },
  "/editor.js": { filePath: UiPaths.editorJs, type: "application/javascript", label: "editor.js" },
  "/projectOutline.js": { filePath: UiPaths.projectOutlineJs, type: "application/javascript", label: "projectOutline.js" }
};

export function fromRoot(...parts: string[]): string {
  return path.resolve(ROOT, ...parts.filter(Boolean));
}

export function isInsideRoot(absPath: string, rootPath = ROOT): boolean {
  const abs = path.resolve(absPath);
  const root = path.resolve(rootPath);
  return abs === root || abs.startsWith(root + path.sep);
}

// Compatibility names for current imports.
export const SETTINGS_DIR = SettingsPaths.dir;
export const SETTINGS_PATH = SettingsPaths.settings;
export const USER_SETTINGS_PATH = SettingsPaths.user;
export const USER_PATH = SettingsPaths.user;
export const USAGE_PATH = SettingsPaths.usage;
export const AUTH_TOKEN_PATH = SettingsPaths.authToken;
export const AUTH_ACCESS_PATH = SettingsPaths.authAccess;
export const AUTH_REFRESH_PATH = SettingsPaths.authRefresh;
export const TEMP_STATE_DIR = RuntimePaths.tempDir;
export const POWER_PID_PATH = RuntimePaths.powerPid;
export const REPORTS_PATH = RuntimePaths.reports;
export const UPDATE_STATE_PATH = RuntimePaths.updateState;
export const EQ_STATE_DIR = LegacyEqPaths.dir;
export const EQ_REPORTS_PATH = LegacyEqPaths.reports;
export const PROJECTS_DIR = DataPaths.projectsDir;
export const SYSTEM_PROJECT_META_PATH = path.join(PROJECTS_DIR, "system.json");
export const SYSTEM_MESSAGES_PATH = path.join(PROJECTS_DIR, "system_messages.json");
export const PROJECT_INDEX_PATH = SettingsPaths.projectIndex;
export const LOGS_DIR = DataPaths.logsDir;
export const SCRAPES_DIR = DataPaths.scrapesDir;
export const UI_PATH = UiPaths.monitorHtml;
export const UI_CSS_PATH = UiPaths.monitorCss;
export const UI_TABS_CSS_PATH = UiPaths.monitorTabsCss;
export const UI_TABS_HTML_PATH = UiPaths.monitorTabsHtml;
export const UI_JS_PATH = UiPaths.monitorJs;
export const MONITOR_PROJECT_JS_PATH = UiPaths.monitorProjectJs;
export const MONITOR_LAUNCH_GATE_JS_PATH = UiPaths.monitorLaunchGateJs;
export const MGRSNP_JS_PATH = UiPaths.mgrSnpJs;
export const SYSMENU_JS_PATH = UiPaths.sysMenuJs;
export const DUB_UNDO_JS_PATH = UiPaths.dubUndoJs;
export const EDITOR_JS_PATH = UiPaths.editorJs;
export const PROJECT_SWITCH_JS_PATH = UiPaths.projectSwitchJs;
export const PROJECT_SWITCH_CSS_PATH = UiPaths.projectSwitchCss;
export const PROJECT_OUTLINE_JS_PATH = UiPaths.projectOutlineJs;
export const ASSETS_DIR = UiPaths.assetsDir;
export const FAVICON_PATH = UiPaths.favicon;
export const ASSET_FAVICON_PATH = UiPaths.assetFavicon;
export const WEBMANIFEST_PATH = UiPaths.webmanifest;
export const NODARAMA_WHITE_PNG_PATH = UiPaths.nodaramaWhitePng;
