"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProjectRegistry = void 0;
const database_1 = require("../walls/06-memory/database/database");
class ProjectRegistry {
    registerProject(params) {
        const db = (0, database_1.getDb)();
        const existing = db.prepare('SELECT id FROM projects WHERE name = ?').get(params.name);
        if (existing) {
            db.prepare('UPDATE projects SET path = ?, tech_stack = ?, file_count = ?, updated_at = ? WHERE id = ?')
                .run(params.path, params.stack || '', params.fileCount || 0, new Date().toISOString(), existing.id);
            return existing.id;
        }
        const result = db.prepare('INSERT INTO projects (name, path, tech_stack, file_count, updated_at) VALUES (?, ?, ?, ?, ?)').run(params.name, params.path, params.stack || '', params.fileCount || 0, new Date().toISOString());
        return Number(result.lastInsertRowid);
    }
    listProjects() {
        const db = (0, database_1.getDb)();
        const rows = db.prepare('SELECT * FROM projects ORDER BY updated_at DESC NULLS LAST, name ASC').all();
        return rows.map(mapRow);
    }
    getProject(name) {
        const db = (0, database_1.getDb)();
        const row = db.prepare('SELECT * FROM projects WHERE name = ?').get(name);
        return row ? mapRow(row) : null;
    }
    updateLastScan(name, fileCount, _lineCount) {
        const db = (0, database_1.getDb)();
        db.prepare('UPDATE projects SET updated_at = ?, file_count = ? WHERE name = ?')
            .run(new Date().toISOString(), fileCount, name);
    }
    removeProject(name) {
        const db = (0, database_1.getDb)();
        db.prepare('DELETE FROM projects WHERE name = ?').run(name);
    }
    detectProjectFromCwd(cwd) {
        if (!cwd)
            return null;
        const normalizedCwd = cwd.replace(/[\\\/]/g, '/').toLowerCase();
        const projects = this.listProjects();
        for (const p of projects) {
            const normalizedPath = p.path.replace(/[\\\/]/g, '/').toLowerCase();
            if (normalizedCwd.includes(normalizedPath) || normalizedPath.includes(normalizedCwd)) {
                return p.name;
            }
        }
        for (const p of projects) {
            const normalizedPath = p.path.replace(/[\\\/]/g, '/').toLowerCase();
            const segments = normalizedPath.split('/').filter(Boolean);
            const lastSegment = segments[segments.length - 1];
            if (lastSegment && normalizedCwd.includes('/' + lastSegment)) {
                return p.name;
            }
        }
        return null;
    }
}
exports.ProjectRegistry = ProjectRegistry;
function mapRow(r) {
    return {
        id: Number(r.id),
        name: String(r.name || ''),
        path: String(r.path || ''),
        stack: String(r.tech_stack || ''),
        fileCount: Number(r.file_count || 0),
        lastScanAt: r.updated_at ? String(r.updated_at) : undefined,
        createdAt: r.updated_at ? String(r.updated_at) : '',
    };
}
//# sourceMappingURL=project-registry.js.map