import { basename } from "node:path";
import { ensureConfigDirs } from "../../utils/config.js";
import { createProject, getProjectByPath } from "../../db/repositories/projects.js";
import { validateProjectPath } from "../../utils/validate-project-path.js";
import { notifyDaemonProjectAdded } from "../notify-project-sync.js";
import { notifyDaemonReRegister } from "../notify-re-register.js";
/**
 * Handle the `pocketcoder add <path>` CLI command.
 * Validates the path exists and is a directory, then registers it as a project.
 * If daemon is running, sends project_added message and triggers re-registration with the gateway.
 */
export async function addCommand(pathArg) {
    try {
        const fullPath = await validateProjectPath(pathArg);
        // Check if already registered
        await ensureConfigDirs();
        const existing = getProjectByPath(fullPath);
        if (existing) {
            console.error(`Project already registered: ${existing.name} (${existing.project_id})`);
            process.exitCode = 1;
            return;
        }
        const name = basename(fullPath);
        const project = createProject(name, fullPath);
        console.log(`Project registered: ${project.name}`);
        console.log(`ID: ${project.project_id}`);
        // Notify daemon about the new project and re-register with gateway (if running)
        await notifyDaemonProjectAdded(project.project_id, project.name, fullPath);
        await notifyDaemonReRegister();
    }
    catch (err) {
        const message = err instanceof Error ? err.message : String(err);
        console.error(`Failed to add project: ${message}`);
        process.exitCode = 1;
    }
}
//# sourceMappingURL=add.js.map