"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateCustomAISettings = 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 updateCustomAISettings = 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({ id: tables_1.applicationsTable.id })
            .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 service = new customAIApplicationService_1.CustomAIApplicationService();
        const { language, applicationPath, envVariables, removedEnvKeys, transformResponse } = customAIApplicationService_1.CustomAIApplicationService.parseRequestFields(req.body);
        if (!language) {
            res.status(400).json({ success: false, message: 'language must be python, js, or go' });
            return;
        }
        if (!applicationPath) {
            res.status(400).json({ success: false, message: 'applicationPath is required' });
            return;
        }
        const provider = await service.updateApplicationSettings({
            applicationId,
            language,
            applicationPath,
            setupMethod: 'upload',
            transformResponse,
            envVariables,
            removedEnvKeys,
        });
        res.json({
            success: true,
            data: { provider },
            message: 'Custom AI settings updated successfully',
        });
    }
    catch (error) {
        logger_1.default.error(`updateCustomAISettings failed: ${error}`);
        res.status(400).json({
            success: false,
            message: error instanceof Error ? error.message : 'Failed to update custom AI settings',
        });
    }
};
exports.updateCustomAISettings = updateCustomAISettings;
//# sourceMappingURL=update-custom-ai-settings.js.map