"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateCustomAIStoredBundle = void 0;
const drizzle_orm_1 = require("drizzle-orm");
const database_1 = require("../../../database");
const tables_1 = require("../../../database/tables");
const customAIApplicationService_1 = require("../../services/customAIApplicationService");
const validateCustomAIStoredBundle = async (req, res) => {
    try {
        const { applicationId } = req.params;
        if (!applicationId) {
            res.status(400).json({ success: false, message: 'applicationId is required' });
            return;
        }
        const accessToken = (req.headers['x-access-token'] || req.headers.authorization);
        const tokenValue = Array.isArray(accessToken) ? accessToken[0] : accessToken;
        if (!tokenValue || String(tokenValue).trim() === '') {
            res.status(401).json({ success: false, message: 'Authentication token is required' });
            return;
        }
        const db = await (0, database_1.getDb)();
        const [application] = await db
            .select({
            id: tables_1.applicationsTable.id,
            projectId: tables_1.applicationsTable.projectId,
            workspaceId: tables_1.applicationsTable.workspaceId,
        })
            .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, orgId, projectId } = customAIApplicationService_1.CustomAIApplicationService.parseRequestFields(req.body);
        const resolvedOrgId = orgId || String(application.workspaceId ?? '').trim();
        const resolvedProjectId = projectId || String(application.projectId ?? '').trim();
        if (!resolvedOrgId || !resolvedProjectId) {
            res.status(400).json({ success: false, message: 'orgId and projectId are required' });
            return;
        }
        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 result = await service.validateStoredBundle({
            applicationId,
            language,
            applicationPath,
            accessToken: tokenValue,
        });
        res.json({
            success: result.valid,
            data: result,
            message: result.valid ? 'Bundle validation passed' : result.errors.join('; '),
        });
    }
    catch (error) {
        res.status(500).json({
            success: false,
            message: error instanceof Error ? error.message : 'Validation failed',
        });
    }
};
exports.validateCustomAIStoredBundle = validateCustomAIStoredBundle;
//# sourceMappingURL=validate-custom-ai-stored-bundle.js.map