"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const permissions_1 = require("../../util/config/permissions");
const archive_dataset_1 = require("../controller/datasets/archive-dataset");
const bulk_append_dataset_rows_commit_1 = require("../controller/datasets/bulk-append-dataset-rows-commit");
const bulk_append_dataset_rows_preview_1 = require("../controller/datasets/bulk-append-dataset-rows-preview");
const bulk_append_middleware_1 = require("../controller/datasets/bulk-append-middleware");
const bulk_delete_dataset_rows_1 = require("../controller/datasets/bulk-delete-dataset-rows");
const create_dataset_1 = require("../controller/datasets/create-dataset");
const create_dataset_row_1 = require("../controller/datasets/create-dataset-row");
const dataset_row_files_middleware_1 = require("../controller/datasets/dataset-row-files-middleware");
const delete_dataset_1 = require("../controller/datasets/delete-dataset");
const delete_dataset_row_1 = require("../controller/datasets/delete-dataset-row");
const export_dataset_csv_1 = require("../controller/datasets/export-dataset-csv");
const export_dataset_template_csv_1 = require("../controller/datasets/export-dataset-template-csv");
const generate_dataset_1 = require("../controller/datasets/generate-dataset");
const generate_dataset_from_docs_1 = require("../controller/datasets/generate-dataset-from-docs");
const upload_dataset_source_documents_1 = require("../controller/datasets/upload-dataset-source-documents");
const upload_dataset_source_documents_middleware_1 = require("../controller/datasets/upload-dataset-source-documents-middleware");
const get_dataset_column_values_1 = require("../controller/datasets/get-dataset-column-values");
const get_dataset_details_1 = require("../controller/datasets/get-dataset-details");
const get_dataset_list_1 = require("../controller/datasets/get-dataset-list");
const get_datatset_testruns_1 = require("../controller/datasets/get-datatset-testruns");
const ingest_media_url_1 = require("../controller/datasets/ingest-media-url");
const ingest_media_urls_batch_1 = require("../controller/datasets/ingest-media-urls-batch");
const update_dataset_details_1 = require("../controller/datasets/update-dataset-details");
const update_dataset_row_1 = require("../controller/datasets/update-dataset-row");
const update_dataset_table_1 = require("../controller/datasets/update-dataset-table");
const upload_audio_dataset_1 = require("../controller/datasets/upload-audio-dataset");
const upload_media_dataset_1 = require("../controller/datasets/upload-media-dataset");
const permission_1 = require("../middlewares/permission");
const datasetRouter = (0, express_1.Router)();
const maybeParseMultipartDatasetRow = (req, res, next) => {
    const contentType = String(req.headers['content-type'] || '');
    if (contentType.includes('multipart/form-data')) {
        (0, dataset_row_files_middleware_1.datasetRowFilesUploadMiddleware)(req, res, (err) => {
            if (err) {
                const message = err instanceof Error ? err.message : 'Multipart parse failed';
                res.status(400).json({ error: message });
                return;
            }
            next();
        });
        return;
    }
    next();
};
// Get all datasets for a project
datasetRouter.get('/', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_READ), get_dataset_list_1.getDatasetList);
// Can create ai dataset
datasetRouter.get('/can-create-dataset', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_READ), get_dataset_list_1.canCreateDataset);
// Get a specific dataset by ID
datasetRouter.get('/:id', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_READ), get_dataset_details_1.getDatasetDetails);
// Get column values for a specific dataset column (columnName via query param)
datasetRouter.get('/:id/column/values', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_READ), get_dataset_column_values_1.getDatasetColumnValues);
// Create a new dataset
datasetRouter.post('/', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), create_dataset_1.createDataset);
// Update a dataset
datasetRouter.patch('/', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), update_dataset_details_1.updateDatasetDetails);
// Update dataset table
datasetRouter.patch('/:id/table', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), update_dataset_table_1.updateDatasetTable);
// Download dataset rows as CSV (media columns get 1-year SAS URLs)
datasetRouter.get('/:id/export', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_READ), export_dataset_csv_1.exportDatasetCsv);
// Download empty CSV template for dataset schema
datasetRouter.get('/:id/export-template', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_READ), export_dataset_template_csv_1.exportDatasetTemplateCsv);
// Ingest a remote media URL into Azure Blob, return blobUrl
// NOTE: This is dataset-scoped; datasetId is derived from the route param.
datasetRouter.post('/:id/ingest-media-url', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), (req, _res, next) => {
    req.body = { ...(req.body ?? {}), datasetId: req.params.id };
    next();
}, ingest_media_url_1.ingestMediaUrl);
datasetRouter.post('/:id/ingest-media-urls-batch', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), ingest_media_urls_batch_1.ingestMediaUrlsBatch);
// Dataset rows CRUD (modal-driven table editing)
// Multipart must be parsed before hasPermission so projectId/orgId from FormData are on req.body
datasetRouter.post('/:id/rows', maybeParseMultipartDatasetRow, (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), create_dataset_row_1.createDatasetRow);
datasetRouter.patch('/:id/rows/:recordId', maybeParseMultipartDatasetRow, (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), update_dataset_row_1.updateDatasetRow);
datasetRouter.delete('/:id/rows/:recordId', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), delete_dataset_row_1.deleteDatasetRow);
datasetRouter.post('/:id/rows/bulk-delete', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), bulk_delete_dataset_rows_1.bulkDeleteDatasetRows);
const runBulkAppendUpload = (req, res, next) => {
    (0, bulk_append_middleware_1.bulkAppendUploadMiddleware)(req, res, (err) => {
        if (err) {
            const message = err instanceof Error ? err.message : 'Upload failed';
            res.status(400).json({ error: message });
            return;
        }
        next();
    });
};
datasetRouter.post('/:id/rows/bulk-append-preview', runBulkAppendUpload, (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), bulk_append_dataset_rows_preview_1.bulkAppendDatasetRowsPreview);
datasetRouter.post('/:id/rows/bulk-append-commit', runBulkAppendUpload, (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), bulk_append_dataset_rows_commit_1.bulkAppendDatasetRowsCommit);
// Archive a dataset
datasetRouter.patch('/:datasetId/archive', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), archive_dataset_1.archiveDataset);
// Delete a dataset (for draft datasets)
datasetRouter.delete('/:datasetId', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), delete_dataset_1.deleteDataset);
// Generate a new dataset
datasetRouter.post('/generate-dataset', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), generate_dataset_1.generateNewDataset);
// Generate dataset from documents
datasetRouter.post('/generate-dataset-from-docs', 
// hasPermission(Permission.DATASET_WRITE),
generate_dataset_from_docs_1.generateDatasetFromDocuments);
// Upload source documents to Azure for evaluation-time document datasets (config.documentSources)
datasetRouter.post('/upload-source-documents', (req, res, next) => {
    (0, upload_dataset_source_documents_middleware_1.uploadDatasetSourceDocumentsMiddleware)(req, res, (err) => {
        if (err) {
            return res.status(400).json({ error: err instanceof Error ? err.message : 'Upload failed' });
        }
        next();
    });
}, (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), upload_dataset_source_documents_1.uploadDatasetSourceDocuments);
// Upload audio dataset
datasetRouter.post('/upload-audio', (req, res, next) => {
    (0, upload_audio_dataset_1.uploadAudioDatasetMiddleware)(req, res, (err) => {
        if (err) {
            return res.status(400).json({ error: err.message });
        }
        next();
    });
}, (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), upload_audio_dataset_1.uploadAudioDataset);
// Upload media dataset (video, image, conversation)
datasetRouter.post('/upload-media', (req, res, next) => {
    (0, upload_media_dataset_1.uploadMediaDatasetMiddleware)(req, res, (err) => {
        if (err) {
            return res.status(400).json({ error: err.message });
        }
        next();
    });
}, (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_WRITE), upload_media_dataset_1.uploadMediaDataset);
// Get all datasets for a project
datasetRouter.get('/:id/test-runs', (0, permission_1.hasPermission)(permissions_1.Permission.DATASET_READ), get_datatset_testruns_1.getDatasetTestRuns);
exports.default = datasetRouter;
//# sourceMappingURL=dataset.js.map