import * as fs from 'node:fs';
import * as fsp from 'node:fs/promises';
import * as path from 'node:path';
import * as os from 'node:os';
const CONTRACTS_FILE = 'contracts.json';
export function getDefaultCodragraphDir() {
    return process.env.CODRAGRAPH_HOME || path.join(os.homedir(), '.codragraph');
}
export function getGroupsBaseDir(codragraphDir) {
    return path.join(codragraphDir || getDefaultCodragraphDir(), 'groups');
}
const GROUP_NAME_RE = /^[a-zA-Z0-9][a-zA-Z0-9_-]*$/;
export function validateGroupName(name) {
    if (!GROUP_NAME_RE.test(name)) {
        throw new Error(`Invalid group name "${name}". Names must start with a letter or digit and contain only [a-zA-Z0-9_-].`);
    }
}
export function getGroupDir(codragraphDir, groupName) {
    validateGroupName(groupName);
    return path.join(codragraphDir, 'groups', groupName);
}
export async function writeContractRegistry(groupDir, registry) {
    const targetPath = path.join(groupDir, CONTRACTS_FILE);
    const tmpPath = `${targetPath}.tmp.${Date.now()}`;
    await fsp.writeFile(tmpPath, JSON.stringify(registry, null, 2), 'utf-8');
    await fsp.rename(tmpPath, targetPath);
}
export async function readContractRegistry(groupDir) {
    const filePath = path.join(groupDir, CONTRACTS_FILE);
    try {
        const content = await fsp.readFile(filePath, 'utf-8');
        return JSON.parse(content);
    }
    catch (err) {
        if (err.code === 'ENOENT')
            return null;
        throw err;
    }
}
export async function listGroups(codragraphDir) {
    const groupsDir = getGroupsBaseDir(codragraphDir);
    try {
        const entries = await fsp.readdir(groupsDir, { withFileTypes: true });
        const names = [];
        for (const entry of entries) {
            if (entry.isDirectory()) {
                const yamlPath = path.join(groupsDir, entry.name, 'group.yaml');
                if (fs.existsSync(yamlPath)) {
                    names.push(entry.name);
                }
            }
        }
        return names;
    }
    catch (err) {
        if (err.code === 'ENOENT')
            return [];
        throw err;
    }
}
export async function createGroupDir(codragraphDir, groupName, force = false) {
    const groupDir = getGroupDir(codragraphDir, groupName);
    if (fs.existsSync(path.join(groupDir, 'group.yaml')) && !force) {
        throw new Error(`Group "${groupName}" already exists. Use --force to overwrite.`);
    }
    await fsp.mkdir(groupDir, { recursive: true });
    const template = `version: 1
name: ${groupName}
description: ""

repos: {}

links: []

packages: {}

detect:
  http: true
  grpc: true
  topics: true
  shared_libs: true
  embedding_fallback: true

matching:
  bm25_threshold: 0.7
  embedding_threshold: 0.65
  max_candidates_per_step: 3
`;
    await fsp.writeFile(path.join(groupDir, 'group.yaml'), template, 'utf-8');
    return groupDir;
}
