"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getApplicationGaurdRailConfig = 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 getApplicationGaurdRailConfig = async (req, res) => {
    try {
        const applicationId = req.query.applicationId;
        const projectId = req.query.projectId;
        const name = req.query.name;
        const db = await (0, database_1.getDb)();
        let application;
        if (applicationId) {
            [application] = await db
                .select()
                .from(tables_1.applicationsTable)
                .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(tables_1.applicationsTable.id, applicationId), (0, drizzle_orm_1.eq)(tables_1.applicationsTable.projectId, projectId)))
                .limit(1);
        }
        else if (name) {
            [application] = await db
                .select()
                .from(tables_1.applicationsTable)
                .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(tables_1.applicationsTable.name, name), (0, drizzle_orm_1.eq)(tables_1.applicationsTable.projectId, projectId)))
                .limit(1);
        }
        else {
            res.status(400).json({
                success: false,
                message: 'Either applicationId or name must be provided',
            });
            return;
        }
        if (!application) {
            res.status(404).json({
                success: false,
                message: 'Application not found',
            });
            return;
        }
        // Fetch guardrails configuration
        const [guardrailsConfig] = await db
            .select()
            .from(tables_1.applicationGuardrailsTable)
            .where((0, drizzle_orm_1.eq)(tables_1.applicationGuardrailsTable.applicationId, application.id))
            .limit(1);
        logger_1.default.info(`Fetched application ${applicationId || name} Gaurd Rail config`);
        res.json({
            id: guardrailsConfig?.id,
            application_id: application?.id,
            enable_guard_rails: guardrailsConfig?.enable_guard_rails || false,
            input_validators: guardrailsConfig.input_validators || [],
            output_validators: guardrailsConfig.output_validators || [],
        });
    }
    catch (error) {
        logger_1.default.error(`Error fetching application gaurdrails config: ${error}`);
        res.status(500).json({
            success: false,
            message: 'Failed to fetch  application gaurdrails config',
        });
    }
};
exports.getApplicationGaurdRailConfig = getApplicationGaurdRailConfig;
//# sourceMappingURL=get-application-gaurd-rail-config.js.map