"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getApplicationListWithTrueGuard = 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 getApplicationListWithTrueGuard = async (req, res) => {
    try {
        const { projectId, page = 1, limit = 10, search, archived_data_required = 'true' } = req.query;
        const parsedLimit = Number(limit);
        const parsedPage = Number(page);
        const offset = (parsedPage - 1) * parsedLimit;
        const includeArchived = archived_data_required === 'true';
        const db = await (0, database_1.getDb)();
        const applicationFilters = [];
        const allApplicationsFilters = [];
        if (projectId) {
            applicationFilters.push((0, drizzle_orm_1.eq)(tables_1.applicationsTable.projectId, projectId));
            allApplicationsFilters.push((0, drizzle_orm_1.eq)(tables_1.applicationsTable.projectId, projectId));
        }
        if (search && typeof search === 'string' && search.trim() !== '') {
            applicationFilters.push((0, drizzle_orm_1.or)((0, drizzle_orm_1.ilike)(tables_1.applicationsTable.name, `%${search}%`), (0, drizzle_orm_1.ilike)(tables_1.applicationsTable.description, `%${search}%`)));
        }
        // Add archived filter if needed
        if (!includeArchived) {
            applicationFilters.push((0, drizzle_orm_1.eq)(tables_1.applicationsTable.is_archived, false));
            allApplicationsFilters.push((0, drizzle_orm_1.eq)(tables_1.applicationsTable.is_archived, false));
        }
        const allFilters = [...applicationFilters];
        const applications = await db
            .select({
            id: tables_1.applicationsTable.id,
            name: tables_1.applicationsTable.name,
            createdAt: tables_1.applicationsTable.createdAt,
            projectId: tables_1.applicationsTable.projectId,
            enable_guard_rails: tables_1.applicationGuardrailsTable.enable_guard_rails,
            input_validators: tables_1.applicationGuardrailsTable.input_validators,
            output_validators: tables_1.applicationGuardrailsTable.output_validators,
        })
            .from(tables_1.applicationsTable)
            .innerJoin(tables_1.applicationGuardrailsTable, (0, drizzle_orm_1.eq)(tables_1.applicationsTable.id, tables_1.applicationGuardrailsTable.applicationId))
            .where((0, drizzle_orm_1.and)(...allFilters))
            .orderBy((0, drizzle_orm_1.desc)(tables_1.applicationsTable.updatedAt))
            .limit(parsedLimit)
            .offset(offset);
        const totalCount = await db
            .select({ count: (0, drizzle_orm_1.sql) `count(*)` })
            .from(tables_1.applicationsTable)
            .innerJoin(tables_1.applicationGuardrailsTable, (0, drizzle_orm_1.eq)(tables_1.applicationsTable.id, tables_1.applicationGuardrailsTable.applicationId))
            .where((0, drizzle_orm_1.and)(...allFilters))
            .then((result) => Number(result[0]?.count));
        const allCountFilters = [...allApplicationsFilters];
        const allCount = await db
            .select({ count: (0, drizzle_orm_1.sql) `count(*)` })
            .from(tables_1.applicationsTable)
            .innerJoin(tables_1.applicationGuardrailsTable, (0, drizzle_orm_1.eq)(tables_1.applicationsTable.id, tables_1.applicationGuardrailsTable.applicationId))
            .where((0, drizzle_orm_1.and)(...allCountFilters))
            .then((result) => Number(result[0]?.count));
        res.json({
            success: true,
            data: {
                applications,
                pagination: {
                    allTotal: allCount,
                    total: totalCount,
                    page: parsedPage,
                    limit: parsedLimit,
                    totalPages: Math.ceil(totalCount / parsedLimit),
                },
            },
        });
    }
    catch (error) {
        logger_1.default.error(`Error fetching applications with true guard: ${error}`);
        res.status(500).json({
            success: false,
            message: 'Failed to fetch applications with true guard',
        });
    }
};
exports.getApplicationListWithTrueGuard = getApplicationListWithTrueGuard;
//# sourceMappingURL=get-application-list-with-true-guard.js.map