"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdminFirstAgent = void 0;
const AICore_1 = require("../../core/ai/AICore");
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
class AdminFirstAgent {
    constructor() {
        this.name = "AdminFirstAgent";
        this.description = "Validates that every feature has corresponding admin configuration and management flows.";
        this.personality = "An admin-first advocate who ensures no feature ships without its admin interface.";
        this.llmProvider = "github-models";
        this.expertise = ["Admin UX", "Configuration Management", "Permission Design", "Feature Completeness"];
    }
    getAICore() {
        if (!this.ai) {
            try {
                this.ai = AICore_1.AICore.getInstance();
            }
            catch {
                this.ai = AICore_1.AICore.getInstance({
                    workingDirectory: process.cwd(),
                    fallbackEnabled: true,
                });
            }
        }
        return this.ai;
    }
    getContextJson() {
        try {
            const ctxPath = path.join(process.cwd(), ".mycontext", "context.json");
            if (fs.pathExistsSync(ctxPath)) {
                return fs.readJsonSync(ctxPath);
            }
        }
        catch { }
        return {};
    }
    scanForAdminPatterns() {
        const src = path.join(process.cwd(), "src");
        const adminDirs = [];
        const adminFiles = [];
        try {
            if (!fs.pathExistsSync(src))
                return { adminDirs, adminFiles };
            const walk = (dir) => {
                const entries = fs.readdirSync(dir, { withFileTypes: true });
                for (const entry of entries) {
                    const fullPath = path.join(dir, entry.name);
                    if (entry.isDirectory()) {
                        if (entry.name.toLowerCase().includes("admin") ||
                            entry.name.toLowerCase().includes("config") ||
                            entry.name.toLowerCase().includes("settings") ||
                            entry.name.toLowerCase().includes("management")) {
                            adminDirs.push(fullPath);
                        }
                        walk(fullPath);
                    }
                    else if (entry.name.toLowerCase().includes("admin") ||
                        entry.name.toLowerCase().includes("config") ||
                        entry.name.toLowerCase().includes("settings") ||
                        entry.name.toLowerCase().includes("permission")) {
                        adminFiles.push(fullPath);
                    }
                }
            };
            walk(src);
        }
        catch { }
        return { adminDirs, adminFiles };
    }
    async run(input) {
        const context = this.getContextJson();
        const features = context?.features || [];
        const { adminDirs, adminFiles } = this.scanForAdminPatterns();
        const targetFeatures = input.featureIds
            ? features.filter((f) => input.featureIds.includes(f.id))
            : features;
        const flows = [];
        const withoutAdmin = [];
        for (const feature of targetFeatures) {
            const hasConfigScreen = adminFiles.some((f) => fs.readFileSync(f, "utf-8").toLowerCase().includes(feature.id.toLowerCase()));
            const hasManagementUI = adminDirs.some((d) => d.toLowerCase().includes(feature.id.toLowerCase()));
            const hasPermissions = adminFiles.some((f) => fs.readFileSync(f, "utf-8").toLowerCase().includes("permission"));
            const coverage = [hasConfigScreen, hasManagementUI, hasPermissions].filter(Boolean).length;
            flows.push({
                featureId: feature.id,
                featureName: feature.name || feature.id,
                hasConfigScreen,
                hasManagementUI,
                hasPermissions,
                coverage: (coverage / 3) * 100,
            });
            if (coverage < 2) {
                withoutAdmin.push(feature.name || feature.id);
            }
        }
        const recommendations = [];
        if (withoutAdmin.length > 0) {
            recommendations.push(`Missing admin flows for: ${withoutAdmin.join(", ")}. Create config screens and management UIs.`);
        }
        const zeroCoverage = flows.filter((f) => f.coverage === 0);
        if (zeroCoverage.length > 0) {
            recommendations.push(`${zeroCoverage.length} feature(s) have zero admin coverage. Prioritize admin-first development.`);
        }
        try {
            const aiCore = this.getAICore();
            const result = await aiCore.generateStructuredText(`You are an admin-first advocate analyzing ${targetFeatures.length} features.
${withoutAdmin.length} lack admin coverage. Admin dirs found: ${adminDirs.length}, admin files: ${adminFiles.length}.
Suggest 2-3 concrete admin UI patterns to implement.`, '{ "suggestions": ["string"] }');
            recommendations.push(...result.suggestions);
        }
        catch { }
        return {
            totalFeatures: targetFeatures.length,
            featuresWithAdmin: targetFeatures.length - withoutAdmin.length,
            featuresWithoutAdmin: withoutAdmin,
            adminFlows: flows,
            recommendations,
        };
    }
    async validate(input) {
        return true;
    }
}
exports.AdminFirstAgent = AdminFirstAgent;
//# sourceMappingURL=AdminFirstAgent.js.map