import { organizationIntelligenceProfiles } from "@rudderhq/db";
import { ORGANIZATION_INTELLIGENCE_PROFILE_PURPOSES, } from "@rudderhq/shared";
import { and, eq } from "drizzle-orm";
const AGENT_ONLY_CONFIG_KEYS = new Set([
    "promptTemplate",
    "bootstrapPromptTemplate",
    "instructionsFilePath",
    "instructionsRootPath",
    "instructionsEntryFile",
    "instructionsBundleMode",
    "agentsMdPath",
    "rudderSkillSync",
    "paperclipSkillSync",
    "rudderRuntimeSkills",
    "paperclipRuntimeSkills",
    "workspaceStrategy",
    "workspaceRuntime",
    "cwd",
]);
const DEFAULT_CODEX_INTELLIGENCE_MODEL = "gpt-5.4-mini";
function toProfile(row) {
    return {
        id: row.id,
        orgId: row.orgId,
        purpose: row.purpose,
        agentRuntimeType: row.agentRuntimeType,
        agentRuntimeConfig: row.agentRuntimeConfig ?? {},
        status: row.status,
        lastError: row.lastError ?? null,
        lastVerifiedAt: row.lastVerifiedAt ?? null,
        createdAt: row.createdAt,
        updatedAt: row.updatedAt,
    };
}
function isRecord(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
export function sanitizeConfigForProductIntelligence(config) {
    const next = {};
    for (const [key, value] of Object.entries(config)) {
        if (AGENT_ONLY_CONFIG_KEYS.has(key))
            continue;
        if (key === "modelFallbacks" && Array.isArray(value)) {
            next.modelFallbacks = value.map((fallback) => {
                if (!isRecord(fallback))
                    return fallback;
                const fallbackConfig = isRecord(fallback.config)
                    ? sanitizeConfigForProductIntelligence(fallback.config)
                    : undefined;
                return {
                    ...fallback,
                    ...(fallbackConfig ? { config: fallbackConfig } : {}),
                };
            });
            continue;
        }
        next[key] = value;
    }
    return next;
}
export function buildIntelligenceProfileConfigWithPurposeDefaults(_purpose, agentRuntimeType, sourceConfig) {
    const base = sanitizeConfigForProductIntelligence(sourceConfig);
    if (agentRuntimeType === "codex_local") {
        const config = { ...base };
        delete config.modelReasoningEffort;
        delete config.reasoningEffort;
        return {
            ...config,
            model: DEFAULT_CODEX_INTELLIGENCE_MODEL,
        };
    }
    return {
        ...base,
        model: typeof base.model === "string" && base.model.trim().length > 0 ? base.model : undefined,
    };
}
export function organizationIntelligenceProfileService(db) {
    async function getByPurpose(orgId, purpose) {
        return db
            .select()
            .from(organizationIntelligenceProfiles)
            .where(and(eq(organizationIntelligenceProfiles.orgId, orgId), eq(organizationIntelligenceProfiles.purpose, purpose)))
            .then((rows) => rows[0] ? toProfile(rows[0]) : null);
    }
    async function list(orgId) {
        const rows = await db
            .select()
            .from(organizationIntelligenceProfiles)
            .where(eq(organizationIntelligenceProfiles.orgId, orgId));
        const byPurpose = new Map(rows.map((row) => [row.purpose, toProfile(row)]));
        return ORGANIZATION_INTELLIGENCE_PROFILE_PURPOSES.map((purpose) => byPurpose.get(purpose) ?? null);
    }
    async function upsert(orgId, purpose, data) {
        const sanitizedConfig = sanitizeConfigForProductIntelligence(data.agentRuntimeConfig);
        const [row] = await db
            .insert(organizationIntelligenceProfiles)
            .values({
            orgId,
            purpose,
            agentRuntimeType: data.agentRuntimeType,
            agentRuntimeConfig: sanitizedConfig,
            status: data.status ?? "disabled",
            lastError: data.lastError ?? null,
            lastVerifiedAt: data.lastVerifiedAt ?? null,
        })
            .onConflictDoUpdate({
            target: [organizationIntelligenceProfiles.orgId, organizationIntelligenceProfiles.purpose],
            set: {
                agentRuntimeType: data.agentRuntimeType,
                agentRuntimeConfig: sanitizedConfig,
                status: data.status ?? "disabled",
                lastError: data.lastError ?? null,
                lastVerifiedAt: data.lastVerifiedAt ?? null,
                updatedAt: new Date(),
            },
        })
            .returning();
        return toProfile(row);
    }
    async function ensureDefaultsFromRuntime(input) {
        const existing = await list(input.orgId);
        const existingPurposes = new Set(existing.filter(Boolean).map((profile) => profile.purpose));
        const created = [];
        for (const purpose of ORGANIZATION_INTELLIGENCE_PROFILE_PURPOSES) {
            if (existingPurposes.has(purpose))
                continue;
            created.push(await upsert(input.orgId, purpose, {
                agentRuntimeType: input.agentRuntimeType,
                agentRuntimeConfig: buildIntelligenceProfileConfigWithPurposeDefaults(purpose, input.agentRuntimeType, input.agentRuntimeConfig),
                status: "disabled",
            }));
        }
        return created;
    }
    return {
        getByPurpose,
        list,
        upsert,
        ensureDefaultsFromRuntime,
        sanitizeConfigForProductIntelligence,
    };
}
//# sourceMappingURL=organization-intelligence-profiles.js.map