"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCustomAIContext = void 0;
const drizzle_orm_1 = require("drizzle-orm");
const database_1 = require("../../../database");
const tables_1 = require("../../../database/tables");
const logger_1 = __importDefault(require("../../../logger"));
const customAIApplicationService_1 = require("../../services/customAIApplicationService");
const getCustomAIContext = async (req, res) => {
    try {
        const { applicationId } = req.params;
        if (!applicationId) {
            res.status(400).json({ success: false, message: 'applicationId is required' });
            return;
        }
        const db = await (0, database_1.getDb)();
        const [application] = await db
            .select({ provider_config: tables_1.applicationsTable.provider_config })
            .from(tables_1.applicationsTable)
            .where((0, drizzle_orm_1.eq)(tables_1.applicationsTable.id, applicationId))
            .limit(1);
        if (!application) {
            res.status(404).json({ success: false, message: 'Application not found' });
            return;
        }
        const providerConfig = (application.provider_config ?? {});
        const config = (providerConfig.config ?? {});
        const provider = (config.provider ?? {});
        const setupMethod = String(provider.setupMethod ?? 'shell').trim();
        const isUploadMode = setupMethod === 'upload';
        const service = new customAIApplicationService_1.CustomAIApplicationService();
        let envVariables = [];
        if (isUploadMode) {
            try {
                envVariables = await service.getMaskedEnvVariables(applicationId);
            }
            catch (error) {
                logger_1.default.warn(`getCustomAIContext: could not load env for ${applicationId}: ${error}`);
            }
        }
        res.json({
            success: true,
            data: {
                bundleFileName: isUploadMode && typeof provider.bundleFileName === 'string'
                    ? provider.bundleFileName
                    : undefined,
                bundleBlobUrl: isUploadMode && typeof provider.bundleBlobUrl === 'string'
                    ? provider.bundleBlobUrl
                    : undefined,
                hasBundle: isUploadMode && Boolean(provider.bundleBlobUrl),
                applicationPath: isUploadMode && typeof provider.applicationPath === 'string'
                    ? provider.applicationPath
                    : undefined,
                envVariables,
            },
        });
    }
    catch (error) {
        logger_1.default.error(`getCustomAIContext failed: ${error}`);
        res.status(500).json({
            success: false,
            message: error instanceof Error ? error.message : 'Failed to load custom AI context',
        });
    }
};
exports.getCustomAIContext = getCustomAIContext;
//# sourceMappingURL=get-custom-ai-context.js.map