"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDatasetColumnValues = 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 constants_1 = require("../../../constants");
const getDatasetColumnValues = async (req, res) => {
    try {
        const { id } = req.params;
        const { page, limit, requiredType = constants_1.DATASET_VAR_TYPE.TEXT, columnName } = req.query;
        const parsedLimit = Number(limit) || 100;
        const parsedPage = Number(page) || 1;
        const offset = (parsedPage - 1) * parsedLimit;
        if (!id) {
            res.status(400).json({ error: 'Dataset ID is required' });
            return;
        }
        const columnNameStr = typeof columnName === 'string' ? columnName.trim() : '';
        if (!columnNameStr) {
            res.status(400).json({ error: 'Column name is required' });
            return;
        }
        const db = await (0, database_1.getDb)();
        // Get dataset details to verify it exists
        const dataset = await db
            .select({
            id: tables_1.trusysDatasetsTable.id,
            name: tables_1.trusysDatasetsTable.name,
            type: tables_1.trusysDatasetsTable.type,
            fields: tables_1.trusysDatasetsTable.fields,
            config: tables_1.trusysDatasetsTable.config,
        })
            .from(tables_1.trusysDatasetsTable)
            .where((0, drizzle_orm_1.eq)(tables_1.trusysDatasetsTable.id, id))
            .limit(1);
        if (!dataset[0]) {
            res.status(404).json({ error: 'Dataset not found' });
            return;
        }
        const datasetData = dataset[0];
        const isDynamicDataset = (datasetData?.type === constants_1.DATASET_TYPE.GENERATE ||
            datasetData?.type === constants_1.DATASET_TYPE.GENERATE_FROM_DOCUMENTS) &&
            datasetData?.config?.generateOnEvaluation === true;
        let datasetRecords = [];
        let audioRecordsCount = 0;
        // For dynamic datasets (generateOnEvaluation), do NOT return persisted records.
        // These datasets are generated at evaluation time; any stored records may be from previous runs
        // and are misleading in UIs like conversation builder.
        if (isDynamicDataset) {
            datasetRecords = [];
            audioRecordsCount = 0;
        }
        // Check if dataset type is huggingface, then fetch from URL
        else if (datasetData.type === constants_1.DATASET_TYPE.CONNECT && datasetData.config?.datasetUrl) {
            try {
                const datasetUrl = datasetData.config.datasetUrl;
                // Validate and build URL with pagination parameters
                let url;
                try {
                    url = new URL(datasetUrl);
                }
                catch (urlError) {
                    throw new Error(`Invalid dataset URL: ${datasetUrl} ${urlError}`);
                }
                // Fetch from HuggingFace datasets server
                const hfToken = process.env.HF_TOKEN ||
                    process.env.HF_API_TOKEN ||
                    process.env.HUGGING_FACE_HUB_TOKEN;
                const headers = {};
                if (hfToken) {
                    headers.Authorization = `Bearer ${hfToken}`;
                }
                const response = await fetch(url.toString(), { headers });
                if (!response.ok) {
                    throw new Error(`Failed to fetch dataset from URL: ${response.statusText}`);
                }
                const responseData = await response.json();
                // Convert HuggingFace rows to the expected record format
                datasetRecords = responseData.rows.map((item, index) => ({
                    id: `hf-${offset + index}`,
                    recordData: item.row,
                    recordIndex: offset + index,
                }));
                audioRecordsCount = responseData.rows.length;
            }
            catch (error) {
                logger_1.default.error(`Error fetching dataset from HuggingFace URL: ${error}`);
                // Fallback to empty data if fetch fails
                datasetRecords = [];
                audioRecordsCount = 0;
            }
        }
        else {
            // Get dataset records from database for non-huggingface datasets
            datasetRecords = await db
                .select({
                id: tables_1.datasetRecordsTable.id,
                recordData: tables_1.datasetRecordsTable.recordData,
                recordIndex: tables_1.datasetRecordsTable.recordIndex,
            })
                .from(tables_1.datasetRecordsTable)
                .where((0, drizzle_orm_1.eq)(tables_1.datasetRecordsTable.datasetId, id))
                .orderBy((0, drizzle_orm_1.asc)(tables_1.datasetRecordsTable.recordIndex))
                .limit(parsedLimit)
                .offset(offset);
            // Count records with voice type in this column using SQL
            const audioRecordsCountResult = await db
                .select({ count: (0, drizzle_orm_1.count)() })
                .from(tables_1.datasetRecordsTable)
                .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(tables_1.datasetRecordsTable.datasetId, id)));
            audioRecordsCount = Number(audioRecordsCountResult[0]?.count || 0);
        }
        // Extract column values from records, filtering for audio and text type
        const columnValues = datasetRecords
            .map((record) => {
            const recordData = record.recordData;
            const columnValue = recordData[columnNameStr];
            if (datasetData.type === constants_1.DATASET_TYPE.CONNECT) {
                return {
                    id: record.id,
                    value: columnValue?.url || columnValue?.src || columnValue || '',
                    ...(requiredType === constants_1.DATASET_VAR_TYPE.VOICE ? { voiceUrl: columnValue?.url || columnValue?.src || columnValue || '' } : {}),
                    type: requiredType,
                    recordIndex: record.recordIndex,
                };
            }
            // Check if the value is of type audio
            if (requiredType === constants_1.DATASET_VAR_TYPE.VOICE && columnValue && columnValue.type === constants_1.DATASET_VAR_TYPE.VOICE) {
                return {
                    id: record.id,
                    value: typeof columnValue === 'object' ? columnValue?.value || columnValue?.voiceUrl || '' : columnValue || '',
                    voiceUrl: typeof columnValue === 'object' ? columnValue?.voiceUrl : '',
                    type: columnValue.type,
                    recordIndex: record.recordIndex,
                };
            }
            if (requiredType === constants_1.DATASET_VAR_TYPE.TEXT && columnValue) {
                if (typeof columnValue === 'object' && columnValue.type === constants_1.DATASET_VAR_TYPE.TEXT) {
                    return {
                        id: record.id,
                        value: columnValue?.value || '',
                        type: requiredType,
                        recordIndex: record.recordIndex,
                    };
                }
                if (typeof columnValue === 'string') {
                    return {
                        id: record.id,
                        value: columnValue,
                        type: requiredType,
                        recordIndex: record.recordIndex,
                    };
                }
            }
            return null;
        })
            .filter((value) => value !== null);
        // For dynamic datasets, rows are generated at evaluation time.
        // Use config.numSamples as the "total rows" contract for UIs like conversation builder.
        const dynamicTotalRows = isDynamicDataset && datasetData?.config?.numSamples != null
            ? Number(datasetData.config.numSamples)
            : 0;
        const totalRows = isDynamicDataset && Number.isFinite(dynamicTotalRows) && dynamicTotalRows > 0
            ? dynamicTotalRows
            : audioRecordsCount;
        const pagination = {
            total: totalRows,
            page: parsedPage,
            limit: parsedLimit,
            totalPages: Math.ceil(totalRows / parsedLimit),
        };
        res.json({
            data: {
                datasetId: id,
                datasetType: dataset[0].type,
                isDynamicDataset,
                totalRows: isDynamicDataset ? totalRows : undefined,
                datasetName: dataset[0].name,
                columnName: columnNameStr,
                values: columnValues,
                pagination,
            },
        });
    }
    catch (error) {
        logger_1.default.error(`Error fetching dataset column values: ${error}`);
        res.status(500).json({ error: 'Failed to fetch dataset column values' });
    }
};
exports.getDatasetColumnValues = getDatasetColumnValues;
//# sourceMappingURL=get-dataset-column-values.js.map