"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BackendEngine = void 0;
const cell_store_1 = require("../walls/06-memory/stores/cell-store");
class BackendEngine {
    store;
    data;
    constructor(cellDir) {
        this.store = new cell_store_1.CellStore(cellDir);
        this.data = this.store.read('backend.json', 'personal', 'architecture') || {
            version: 1, updated: Date.now(),
            framework: { name: 'unknown', confidence: 0 },
            apiStyle: { name: 'unknown', confidence: 0 },
            database: { name: 'unknown', confidence: 0 },
            auth: { name: 'unknown', confidence: 0 },
            validation: { name: 'unknown', confidence: 0 },
            totalEndpoints: 0,
        };
    }
    analyze(code) {
        if (!code || code.length < 20)
            return { endpointsFound: 0 };
        let found = 0;
        const fw = this.detectFramework(code);
        if (fw.confidence > this.data.framework.confidence) {
            this.data.framework = fw;
            found++;
        }
        const api = this.detectApiStyle(code);
        if (api.confidence > this.data.apiStyle.confidence) {
            this.data.apiStyle = api;
            found++;
        }
        const db = this.detectDatabase(code);
        if (db.confidence > this.data.database.confidence) {
            this.data.database = db;
            found++;
        }
        const auth = this.detectAuth(code);
        if (auth.confidence > this.data.auth.confidence) {
            this.data.auth = auth;
            found++;
        }
        const val = this.detectValidation(code);
        if (val.confidence > this.data.validation.confidence) {
            this.data.validation = val;
            found++;
        }
        this.data.totalEndpoints += (code.match(/\.(?:get|post|put|delete|patch)\s*\(/g) || []).length;
        if (found > 0 || this.data.totalEndpoints > 0) {
            this.data.updated = Date.now();
            this.store.write('backend.json', this.data, 'personal', 'architecture');
        }
        return { endpointsFound: this.data.totalEndpoints };
    }
    getSummary() {
        const lines = [];
        if (this.data.framework.confidence === 0 && this.data.totalEndpoints === 0)
            return [];
        lines.push(`Framework: ${this.data.framework.name} · API: ${this.data.apiStyle.name} · DB: ${this.data.database.name}`);
        lines.push(`Auth: ${this.data.auth.name} · Validation: ${this.data.validation.name} · Endpoints: ${this.data.totalEndpoints}`);
        return lines;
    }
    detectFramework(code) {
        if (/from\s+['"]express['"]/i.test(code) || /require\s*\(['"]express['"]\)/i.test(code))
            return { name: 'Express', confidence: 0.9 };
        if (/from\s+['"]fastify['"]/i.test(code))
            return { name: 'Fastify', confidence: 0.85 };
        if (/from\s+['"]@nestjs/i.test(code) || /@Controller\b/.test(code))
            return { name: 'NestJS', confidence: 0.85 };
        if (/from\s+['"]next['"]/i.test(code) || /\bNextRequest\b/.test(code))
            return { name: 'Next.js', confidence: 0.8 };
        if (/from\s+['"]koa['"]/i.test(code))
            return { name: 'Koa', confidence: 0.8 };
        if (/from\s+['"]honojs['"]/i.test(code) || /from\s+['"]hono['"]/i.test(code))
            return { name: 'Hono', confidence: 0.75 };
        return { name: 'unknown', confidence: 0 };
    }
    detectApiStyle(code) {
        if (/from\s+['"]@apollo\b/i.test(code) || /gql`/.test(code))
            return { name: 'GraphQL', confidence: 0.85 };
        if (/from\s+['"]@trpc\b/i.test(code))
            return { name: 'tRPC', confidence: 0.85 };
        if (/\bgrpc\b/i.test(code) || /proto\s*:/.test(code))
            return { name: 'gRPC', confidence: 0.75 };
        if (/\.(?:get|post|put|delete|patch)\s*\(/.test(code))
            return { name: 'REST', confidence: 0.7 };
        return { name: 'unknown', confidence: 0 };
    }
    detectDatabase(code) {
        if (/from\s+['"]@prisma\b/i.test(code) || /\bprisma\b.*\b(?:findMany|create|update|delete)\b/i.test(code))
            return { name: 'Prisma', confidence: 0.9 };
        if (/from\s+['"]typeorm['"]/i.test(code) || /from\s+['"]@typeorm/i.test(code))
            return { name: 'TypeORM', confidence: 0.85 };
        if (/from\s+['"]mongoose['"]/i.test(code) || /\bMongoose\b/.test(code))
            return { name: 'Mongoose', confidence: 0.85 };
        if (/from\s+['"]drizzle/i.test(code))
            return { name: 'Drizzle', confidence: 0.8 };
        if (/\bdb\.\w+\s*\(/i.test(code) && /\bquery\b/i.test(code))
            return { name: 'raw SQL', confidence: 0.5 };
        if (/from\s+['"]better-sqlite3['"]/i.test(code))
            return { name: 'SQLite', confidence: 0.7 };
        return { name: 'unknown', confidence: 0 };
    }
    detectAuth(code) {
        if (/from\s+['"]passport['"]/i.test(code))
            return { name: 'Passport', confidence: 0.85 };
        if (/\bjwt\b/i.test(code) && /\.(?:sign|verify|decode)\s*\(/.test(code))
            return { name: 'JWT', confidence: 0.8 };
        if (/\bexpress-session\b/i.test(code) || /\bsession\.\w+\b/i.test(code))
            return { name: 'Session', confidence: 0.75 };
        if (/\bnext-auth\b/i.test(code) || /\bAuth.js\b/i.test(code))
            return { name: 'NextAuth', confidence: 0.8 };
        if (/\bOAuth\b/i.test(code))
            return { name: 'OAuth', confidence: 0.75 };
        return { name: 'unknown', confidence: 0 };
    }
    detectValidation(code) {
        if (/\bzod\b/i.test(code) && /\.(?:parse|safeParse|schema)\s*\(/.test(code))
            return { name: 'Zod', confidence: 0.9 };
        if (/\byup\b/i.test(code) && /\.(?:validate|schema)\s*\(/.test(code))
            return { name: 'Yup', confidence: 0.85 };
        if (/from\s+['"]joi['"]/i.test(code))
            return { name: 'Joi', confidence: 0.85 };
        if (/\bclass-validator\b/i.test(code))
            return { name: 'class-validator', confidence: 0.8 };
        return { name: 'unknown', confidence: 0 };
    }
}
exports.BackendEngine = BackendEngine;
//# sourceMappingURL=backend-engine.js.map