"use strict";
/**
 * Seed the MCP service account at startup.
 *
 * Creates a single `mcp-service` bot account with two API keys:
 *   - `mcp:read`  — read-safe tools only (discovery, monitoring)
 *   - `mcp:full`  — all tools (automation, orchestration)
 *
 * Idempotent — skips creation if the account already exists.
 * Keys are generated once and logged; they cannot be retrieved again.
 */
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.seedMcpServiceAccount = seedMcpServiceAccount;
const logger_1 = require("../../lib/logger");
const crud_1 = require("../user/crud");
const iam = __importStar(require("../iam"));
const SERVICE_ACCOUNT_NAME = 'mcp-service';
async function seedMcpServiceAccount() {
    try {
        // Check if account already exists (bot external_id = name)
        const existing = await (0, crud_1.getUserByExternalId)(SERVICE_ACCOUNT_NAME);
        if (existing) {
            logger_1.loggerRegistry.info(`[seed-mcp] ${SERVICE_ACCOUNT_NAME} already exists, skipping`);
            return;
        }
        // Create the service account
        const bot = await iam.createBot({
            name: SERVICE_ACCOUNT_NAME,
            description: 'MCP service account for external tool access. Use the read key to explore, full key to automate.',
            display_name: 'MCP Service',
            roles: [{ role: 'system', type: 'member' }],
        });
        // Generate read key (scoped to mcp:read)
        const readKey = await iam.createBotKey(bot.id, 'read', ['mcp:read']);
        logger_1.loggerRegistry.info(`[seed-mcp] read key: ${readKey.rawKey}`);
        // Generate full key (scoped to mcp:full)
        const fullKey = await iam.createBotKey(bot.id, 'full', ['mcp:read', 'mcp:full']);
        logger_1.loggerRegistry.info(`[seed-mcp] full key: ${fullKey.rawKey}`);
        logger_1.loggerRegistry.info(`[seed-mcp] ${SERVICE_ACCOUNT_NAME} created with read + full API keys`);
    }
    catch (err) {
        logger_1.loggerRegistry.warn(`[seed-mcp] failed to seed: ${err.message}`);
    }
}
