"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.testRunsToDatasetRecordsRelations = exports.datasetRecordsRelations = exports.pulseMonitoringAlertsRelations = exports.pulseMonitoringAlertsToNotificationChannelsRelations = exports.notificationChannelRelations = exports.pulseMonitoringAlertsToNotificationChannelsTable = exports.notificationChannelTable = exports.pulseAlertLogsTable = exports.evalsToTracesTable = exports.pulseMonitoringAlertsTable = exports.argusChatHistoryTable = exports.pulseMonitoringTable = exports.testRunsToSecurityConfigsTable = exports.securityConfigTable = exports.providerSupportedLanguagesTable = exports.strategyPresetsTable = exports.pluginsPresetsTable = exports.defaultProvidersTable = exports.assertionsTable = exports.configsTable = exports.evalsToPromptsRelations = exports.evalsRelations = exports.testRunsToDatasetsRelations = exports.trusysDatasetsRelations = exports.testRunsRelations = exports.testRunsToDatasetRecordsTable = exports.testRunsToDatasetsTable = exports.testRunsToEvalsTable = exports.testRunsTable = exports.evalsToDatasetsRelations = exports.datasetsRelations = exports.evalsToDatasetsTable = exports.datasetRecordsTable = exports.trusysDatasetsTable = exports.datasetsTable = exports.evalsToTagsRelations = exports.tagsRelations = exports.promptsRelations = exports.evalsToTagsTable = exports.evalsToPromptsTable = exports.backfillsTable = exports.evalResultComponentsTable = exports.evalResultsAnalyticsTable = exports.evalResultsTable = exports.evalsTable = exports.tagsTable = exports.applicationsTable = exports.promptsLibraryPromptsTable = exports.promptsLibraryTable = exports.promptsTable = void 0;
exports.applicationGuardrailsRelations = exports.applicationGuardrailsTable = exports.testRunsToScheduleJobsRelations = exports.scheduleJobsRelations = exports.testRunsToScheduleJobsTable = exports.scheduleJobsTable = exports.trusysSecretsVaultTable = exports.providersConfigTable = exports.boardWidgetsMappingRelations = exports.boardWidgetsRelations = exports.boardUserMappingRelations = exports.boardsRelations = exports.boardWidgetsMappingTable = exports.boardWidgetsTable = exports.boardUserMappingTable = exports.boardsTable = exports.observeDashboardRelations = exports.observeDashboardWidgetsRelations = exports.observeDashboardWidgetsTable = exports.observeWidgetTable = exports.observeDashboardTable = exports.defaultObserveDashboardTable = exports.trialDatasetResultsRelations = exports.trialsRelations = exports.optimizationRunsRelations = exports.trialDatasetResultsTable = exports.trialsTable = exports.optimizationRunsTable = void 0;
const drizzle_orm_1 = require("drizzle-orm");
const pg_core_1 = require("drizzle-orm/pg-core");
const types_1 = require("../types");
// // ------------ Prompts ------------
exports.promptsTable = (0, pg_core_1.pgTable)('prompts', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    prompt: (0, pg_core_1.text)('prompt').notNull(),
    type: (0, pg_core_1.text)('type'),
    projectId: (0, pg_core_1.text)('project_id').notNull(),
    workspaceId: (0, pg_core_1.text)('workspace_id'),
    tests: (0, pg_core_1.jsonb)('tests').$type(),
    dataset: (0, pg_core_1.jsonb)('dataset'),
    userIntentions: (0, pg_core_1.jsonb)('user_intentions').$type(),
    userProfiles: (0, pg_core_1.jsonb)('user_profiles').$type(),
    openingMessage: (0, pg_core_1.text)('opening_message'),
    minimumTurns: (0, pg_core_1.integer)('minimum_turns'),
    maximumTurns: (0, pg_core_1.integer)('maximum_turns'),
    chatStopCriteria: (0, pg_core_1.text)('chat_stop_criteria'),
    conversationFlow: (0, pg_core_1.jsonb)('conversation_flow'),
    turns: (0, pg_core_1.jsonb)('turns'),
    conversationBuilderType: (0, pg_core_1.text)('conversation_builder_type'),
}, (table) => ({
    createdAtIdx: (0, pg_core_1.index)('prompts_created_at_idx').on(table.createdAt),
}));
// // ------------ Prompts Library------------
exports.promptsLibraryTable = (0, pg_core_1.pgTable)('prompts_library', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    name: (0, pg_core_1.text)('name'),
    status: (0, pg_core_1.integer)('status'),
    type: (0, pg_core_1.text)('type').notNull().default('evaluation'),
    globalMetrics: (0, pg_core_1.jsonb)('global_metrics').array(),
    projectId: (0, pg_core_1.text)('project_id').notNull(),
    workspaceId: (0, pg_core_1.text)('workspace_id'),
    is_archived: (0, pg_core_1.boolean)('is_archived').default(false).notNull(),
}, (table) => ({
    createdAtIdx: (0, pg_core_1.index)('prompts_library_created_at_idx').on(table.createdAt),
}));
exports.promptsLibraryPromptsTable = (0, pg_core_1.pgTable)('prompts_library_prompts', {
    libraryId: (0, pg_core_1.text)('library_id')
        .notNull()
        .references(() => exports.promptsLibraryTable.id),
    promptId: (0, pg_core_1.text)('prompt_id')
        .notNull()
        .references(() => exports.promptsTable.id),
});
// ------------ Application ------------
exports.applicationsTable = (0, pg_core_1.pgTable)('applications', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    name: (0, pg_core_1.text)('name'),
    description: (0, pg_core_1.text)('description'),
    status: (0, pg_core_1.integer)('status'),
    security_config: (0, pg_core_1.jsonb)('security_config'),
    useCase: (0, pg_core_1.text)('use_case'),
    /** Main eval provider; `provider_type` key (string) is used to filter evaluation-enabled apps in list APIs. */
    provider_config: (0, pg_core_1.jsonb)('provider_config').$type().notNull(),
    projectId: (0, pg_core_1.text)('project_id'),
    workspaceId: (0, pg_core_1.text)('workspace_id'),
    test_runs: (0, pg_core_1.integer)('test_runs'),
    provider_type: (0, pg_core_1.text)('provider_type'),
    is_archived: (0, pg_core_1.boolean)('is_archived').default(false).notNull(),
    enableEncryption: (0, pg_core_1.text)('enable_encryption').default('0'),
    isAgentic: (0, pg_core_1.boolean)('is_agentic').default(false).notNull(),
    hasVectorDb: (0, pg_core_1.boolean)('has_vector_db'),
}, (table) => ({
    createdAtIdx: (0, pg_core_1.index)('applications_created_at_idx').on(table.createdAt),
}));
// // ------------ Tags ------------
exports.tagsTable = (0, pg_core_1.pgTable)('tags', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    name: (0, pg_core_1.text)('name').notNull(),
    value: (0, pg_core_1.text)('value').notNull(),
}, (table) => ({
    nameIdx: (0, pg_core_1.index)('tags_name_idx').on(table.name),
    uniqueNameValue: (0, pg_core_1.uniqueIndex)('tags_name_value_unique').on(table.name, table.value),
}));
// ------------ Evals ------------
exports.evalsTable = (0, pg_core_1.pgTable)('evals', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    author: (0, pg_core_1.text)('author'),
    description: (0, pg_core_1.text)('description'),
    results: (0, pg_core_1.jsonb)('results').$type().notNull(),
    config: (0, pg_core_1.jsonb)('config').$type().notNull(),
    prompts: (0, pg_core_1.jsonb)('prompts').$type(),
    applicationId: (0, pg_core_1.text)('application_id').references(() => exports.applicationsTable.id),
    projectId: (0, pg_core_1.text)('project_id'),
    workspaceId: (0, pg_core_1.text)('workspace_id'),
    sessionId: (0, pg_core_1.text)('session_id'),
    vars: (0, pg_core_1.jsonb)('vars').$type(),
}, (table) => ({
    createdAtIdx: (0, pg_core_1.index)('evals_created_at_idx').on(table.createdAt),
    authorIdx: (0, pg_core_1.index)('evals_author_idx').on(table.author),
}));
exports.evalResultsTable = (0, pg_core_1.pgTable)('eval_results', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    evalId: (0, pg_core_1.text)('eval_id')
        .notNull()
        .references(() => exports.evalsTable.id),
    promptIdx: (0, pg_core_1.integer)('prompt_idx').notNull(),
    testIdx: (0, pg_core_1.integer)('test_idx').notNull(),
    testCase: (0, pg_core_1.jsonb)('test_case').$type().notNull(),
    prompt: (0, pg_core_1.jsonb)('prompt').$type().notNull(),
    promptId: (0, pg_core_1.text)('prompt_id').references(() => exports.promptsTable.id),
    applicationId: (0, pg_core_1.text)('application_id').references(() => exports.applicationsTable.id),
    sessionId: (0, pg_core_1.text)('session_id'),
    traceId: (0, pg_core_1.text)('trace_id'),
    projectId: (0, pg_core_1.text)('project_id'),
    provider: (0, pg_core_1.jsonb)('provider').$type().notNull(),
    latencyMs: (0, pg_core_1.integer)('latency_ms'),
    cost: (0, pg_core_1.real)('cost'),
    response: (0, pg_core_1.jsonb)('response').$type(),
    error: (0, pg_core_1.text)('error'),
    failureReason: (0, pg_core_1.integer)('failure_reason').default(types_1.ResultFailureReason.NONE).notNull(),
    success: (0, pg_core_1.integer)('success').notNull(),
    score: (0, pg_core_1.real)('score').notNull(),
    gradingResult: (0, pg_core_1.jsonb)('grading_result').$type(),
    namedScores: (0, pg_core_1.jsonb)('named_scores').$type(),
    metadata: (0, pg_core_1.jsonb)('metadata').$type(),
    // New field for redteam
    isRedteam: (0, pg_core_1.integer)('is_redteam').default(0), // boolean as integer, 0 = false, 1 = true
    workspaceId: (0, pg_core_1.text)('workspace_id'),
    parentPromptId: (0, pg_core_1.text)('parent_prompt_id'),
    datasetRecordId: (0, pg_core_1.text)('dataset_record_id'),
    traceIds: (0, pg_core_1.jsonb)('trace_ids').$type(),
}, (table) => ({
    evalIdIdx: (0, pg_core_1.index)('eval_result_eval_id_idx').on(table.evalId),
    testIdxIdx: (0, pg_core_1.index)('eval_result_test_idx').on(table.testIdx),
    // gradingResultCommentIdx: index('eval_result_grading_result_comment_idx').on(
    //   sql`(${table.gradingResult} ->> 'comment')`,
    // ),
    // testCaseVarsIdx: index('eval_result_test_case_vars_idx').on(sql`(${table.testCase} -> 'vars')`),
    // testCaseMetadataIdx: index('eval_result_test_case_metadata_idx').on(
    //   sql`(${table.metadata})`, // already valid jsonb column
    // ),
    // namedScoresIdx: index('eval_result_named_scores_idx').on(
    //   sql`(${table.namedScores})`, // or extract a key if needed
    // ),
    // metadataIdx: index('eval_result_metadata_idx').on(
    //   sql`(${table.metadata})`, // or extract a key if needed
    // ),
}));
exports.evalResultsAnalyticsTable = (0, pg_core_1.pgTable)('eval_results_analytics', {
    evalResultId: (0, pg_core_1.text)('eval_result_id')
        .primaryKey()
        .references(() => exports.evalResultsTable.id, { onDelete: 'cascade' }),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true }).notNull(),
    projectId: (0, pg_core_1.text)('project_id'),
    applicationId: (0, pg_core_1.text)('application_id'),
    workspaceId: (0, pg_core_1.text)('workspace_id'),
    traceId: (0, pg_core_1.text)('trace_id'),
    sessionId: (0, pg_core_1.text)('session_id'),
    traceIds: (0, pg_core_1.jsonb)('trace_ids').$type().default([]),
    isRedteam: (0, pg_core_1.integer)('is_redteam').default(0).notNull(),
    testRunId: (0, pg_core_1.text)('test_run_id'),
    evalId: (0, pg_core_1.text)('eval_id'),
    testIdx: (0, pg_core_1.integer)('test_idx'),
    failureReason: (0, pg_core_1.integer)('failure_reason').default(types_1.ResultFailureReason.NONE).notNull(),
    error: (0, pg_core_1.text)('error'),
    gradingPass: (0, pg_core_1.integer)('grading_pass').default(0).notNull(),
    gradingScore: (0, pg_core_1.real)('grading_score').default(0).notNull(),
    gradingReason: (0, pg_core_1.text)('grading_reason'),
    componentPassCount: (0, pg_core_1.integer)('component_pass_count').default(0).notNull(),
    componentFailCount: (0, pg_core_1.integer)('component_fail_count').default(0).notNull(),
    metricKeys: (0, pg_core_1.jsonb)('metric_keys').$type().default([]),
    failedMetricKeys: (0, pg_core_1.jsonb)('failed_metric_keys').$type().default([]),
    pluginId: (0, pg_core_1.text)('plugin_id'),
    pluginPass: (0, pg_core_1.integer)('plugin_pass'),
    denormVersion: (0, pg_core_1.integer)('denorm_version').default(1).notNull(),
}, (table) => ({
    scopeCreatedIdx: (0, pg_core_1.index)('eval_results_analytics_scope_created_idx').on(table.projectId, table.applicationId, table.createdAt),
    traceIdIdx: (0, pg_core_1.index)('eval_results_analytics_trace_id_idx').on(table.traceId),
    sessionIdIdx: (0, pg_core_1.index)('eval_results_analytics_session_id_idx').on(table.sessionId),
    pluginIdIdx: (0, pg_core_1.index)('eval_results_analytics_plugin_id_idx').on(table.pluginId),
}));
exports.evalResultComponentsTable = (0, pg_core_1.pgTable)('eval_result_components', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    evalResultId: (0, pg_core_1.text)('eval_result_id')
        .notNull()
        .references(() => exports.evalResultsTable.id, { onDelete: 'cascade' }),
    componentIndex: (0, pg_core_1.integer)('component_index').notNull(),
    assertionType: (0, pg_core_1.text)('assertion_type'),
    assertionDisplayLabel: (0, pg_core_1.text)('assertion_display_label'),
    metricKey: (0, pg_core_1.text)('metric_key'),
    componentPass: (0, pg_core_1.integer)('component_pass').default(0).notNull(),
    componentScore: (0, pg_core_1.real)('component_score').default(0).notNull(),
    componentReason: (0, pg_core_1.text)('component_reason'),
    assertionExpected: (0, pg_core_1.text)('assertion_expected'),
    assertionActual: (0, pg_core_1.text)('assertion_actual'),
    projectId: (0, pg_core_1.text)('project_id'),
    applicationId: (0, pg_core_1.text)('application_id'),
    workspaceId: (0, pg_core_1.text)('workspace_id'),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true }).notNull(),
    traceId: (0, pg_core_1.text)('trace_id'),
    sessionId: (0, pg_core_1.text)('session_id'),
    traceIds: (0, pg_core_1.jsonb)('trace_ids').$type().default([]),
    testRunId: (0, pg_core_1.text)('test_run_id'),
    evalId: (0, pg_core_1.text)('eval_id'),
    isRedteam: (0, pg_core_1.integer)('is_redteam').default(0).notNull(),
    failureReason: (0, pg_core_1.integer)('failure_reason').default(types_1.ResultFailureReason.NONE).notNull(),
    error: (0, pg_core_1.text)('error'),
    pluginId: (0, pg_core_1.text)('plugin_id'),
    denormVersion: (0, pg_core_1.integer)('denorm_version').default(1).notNull(),
}, (table) => ({
    evalResultIdx: (0, pg_core_1.index)('eval_result_components_eval_result_id_idx').on(table.evalResultId),
    scopeCreatedIdx: (0, pg_core_1.index)('eval_result_components_scope_created_idx').on(table.projectId, table.createdAt),
    metricKeyIdx: (0, pg_core_1.index)('eval_result_components_metric_key_idx').on(table.metricKey),
    assertionTypeIdx: (0, pg_core_1.index)('eval_result_components_assertion_type_idx').on(table.assertionType),
    traceIdIdx: (0, pg_core_1.index)('eval_result_components_trace_id_idx').on(table.traceId),
    evalComponentUnique: (0, pg_core_1.uniqueIndex)('eval_result_components_eval_result_component_idx').on(table.evalResultId, table.componentIndex),
}));
/** Eval-denorm (and future) backfill job progress; replaces JSON checkpoint files. */
exports.backfillsTable = (0, pg_core_1.pgTable)('backfills', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    pipeline: (0, pg_core_1.text)('pipeline').notNull(),
    status: (0, pg_core_1.text)('status').notNull().default('pending'),
    parentId: (0, pg_core_1.text)('parent_id'),
    shardIndex: (0, pg_core_1.integer)('shard_index'),
    shardStartOffset: (0, pg_core_1.integer)('shard_start_offset'),
    shardEndOffset: (0, pg_core_1.integer)('shard_end_offset'),
    readSource: (0, pg_core_1.text)('read_source'),
    targets: (0, pg_core_1.text)('targets'),
    totalRows: (0, pg_core_1.integer)('total_rows').default(0).notNull(),
    rowsProcessed: (0, pg_core_1.integer)('rows_processed').default(0).notNull(),
    rowsRemaining: (0, pg_core_1.integer)('rows_remaining'),
    batchSize: (0, pg_core_1.integer)('batch_size'),
    batchNumber: (0, pg_core_1.integer)('batch_number').default(0).notNull(),
    estimatedBatches: (0, pg_core_1.integer)('estimated_batches'),
    percentComplete: (0, pg_core_1.real)('percent_complete'),
    elapsedSeconds: (0, pg_core_1.real)('elapsed_seconds'),
    etaSeconds: (0, pg_core_1.real)('eta_seconds'),
    resumeCursor: (0, pg_core_1.jsonb)('resume_cursor').$type(),
    scopeCreatedAfter: (0, pg_core_1.timestamp)('scope_created_after', { withTimezone: true }),
    scopeCreatedBefore: (0, pg_core_1.timestamp)('scope_created_before', { withTimezone: true }),
    scopeReason: (0, pg_core_1.text)('scope_reason'),
    chAnalyticsInserted: (0, pg_core_1.integer)('ch_analytics_inserted').default(0).notNull(),
    chComponentsInserted: (0, pg_core_1.integer)('ch_components_inserted').default(0).notNull(),
    pgAnalyticsInserted: (0, pg_core_1.integer)('pg_analytics_inserted').default(0).notNull(),
    pgComponentsInserted: (0, pg_core_1.integer)('pg_components_inserted').default(0).notNull(),
    skipped: (0, pg_core_1.integer)('skipped').default(0).notNull(),
    errors: (0, pg_core_1.jsonb)('errors').$type().default([]).notNull(),
    lastError: (0, pg_core_1.text)('last_error'),
    lastFailedAt: (0, pg_core_1.timestamp)('last_failed_at', { withTimezone: true }),
    projectId: (0, pg_core_1.text)('project_id'),
    applicationId: (0, pg_core_1.text)('application_id'),
    dryRun: (0, pg_core_1.integer)('dry_run').default(0).notNull(),
    options: (0, pg_core_1.jsonb)('options').$type(),
    startedAt: (0, pg_core_1.timestamp)('started_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    completedAt: (0, pg_core_1.timestamp)('completed_at', { withTimezone: true }),
}, (table) => ({
    pipelineStatusIdx: (0, pg_core_1.index)('backfills_pipeline_status_idx').on(table.pipeline, table.status),
    parentIdIdx: (0, pg_core_1.index)('backfills_parent_id_idx').on(table.parentId),
    updatedAtIdx: (0, pg_core_1.index)('backfills_updated_at_idx').on(table.updatedAt),
}));
exports.evalsToPromptsTable = (0, pg_core_1.pgTable)('evals_to_prompts', {
    evalId: (0, pg_core_1.text)('eval_id')
        .notNull()
        .references(() => exports.evalsTable.id, { onDelete: 'cascade' }),
    promptId: (0, pg_core_1.text)('prompt_id')
        .notNull()
        .references(() => exports.promptsTable.id),
}, (t) => ({
    pk: (0, pg_core_1.primaryKey)({ columns: [t.evalId, t.promptId] }),
    evalIdIdx: (0, pg_core_1.index)('evals_to_prompts_eval_id_idx').on(t.evalId),
    promptIdIdx: (0, pg_core_1.index)('evals_to_prompts_prompt_id_idx').on(t.promptId),
}));
exports.evalsToTagsTable = (0, pg_core_1.pgTable)('evals_to_tags', {
    evalId: (0, pg_core_1.text)('eval_id')
        .notNull()
        .references(() => exports.evalsTable.id),
    tagId: (0, pg_core_1.text)('tag_id')
        .notNull()
        .references(() => exports.tagsTable.id),
}, (t) => ({
    pk: (0, pg_core_1.primaryKey)({ columns: [t.evalId, t.tagId] }),
    evalIdIdx: (0, pg_core_1.index)('evals_to_tags_eval_id_idx').on(t.evalId),
    tagIdIdx: (0, pg_core_1.index)('evals_to_tags_tag_id_idx').on(t.tagId),
}));
exports.promptsRelations = (0, drizzle_orm_1.relations)(exports.promptsTable, ({ many }) => ({
    evalsToPrompts: many(exports.evalsToPromptsTable),
}));
exports.tagsRelations = (0, drizzle_orm_1.relations)(exports.tagsTable, ({ many }) => ({
    evalsToTags: many(exports.evalsToTagsTable),
}));
exports.evalsToTagsRelations = (0, drizzle_orm_1.relations)(exports.evalsToTagsTable, ({ one }) => ({
    eval: one(exports.evalsTable, {
        fields: [exports.evalsToTagsTable.evalId],
        references: [exports.evalsTable.id],
    }),
    tag: one(exports.tagsTable, {
        fields: [exports.evalsToTagsTable.tagId],
        references: [exports.tagsTable.id],
    }),
}));
// // ------------ Datasets ------------
exports.datasetsTable = (0, pg_core_1.pgTable)('datasets', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    tests: (0, pg_core_1.jsonb)('tests'), // assumes UnifiedConfig['tests'] is JSON-serializable
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true }).defaultNow().notNull(),
});
exports.trusysDatasetsTable = (0, pg_core_1.pgTable)('trusys_datasets', {
    id: (0, pg_core_1.text)('id').primaryKey().notNull(),
    name: (0, pg_core_1.text)('name').notNull(),
    type: (0, pg_core_1.text)('type'),
    projectId: (0, pg_core_1.text)('project_id').notNull(),
    data: (0, pg_core_1.jsonb)('data'),
    count: (0, pg_core_1.integer)('count').default(0),
    fields: (0, pg_core_1.jsonb)('fields'),
    config: (0, pg_core_1.jsonb)('config'),
    status: (0, pg_core_1.integer)('status').notNull().default(0), // 0 for draft, 1 for live
    createdAt: (0, pg_core_1.timestamp)('created_at').notNull().defaultNow(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at').notNull().defaultNow(),
    is_archived: (0, pg_core_1.boolean)('is_archived').default(false).notNull(),
    workspaceId: (0, pg_core_1.text)('workspace_id'),
}, (table) => ({
    createdAtIdx: (0, pg_core_1.index)('trusys_datasets_created_at_idx').on(table.createdAt),
    projectIdIdx: (0, pg_core_1.index)('trusys_datasets_project_id_idx').on(table.projectId),
}));
// New table for storing dataset records
exports.datasetRecordsTable = (0, pg_core_1.pgTable)('dataset_records', {
    id: (0, pg_core_1.text)('id').primaryKey().notNull(),
    datasetId: (0, pg_core_1.text)('dataset_id')
        .notNull()
        .references(() => exports.trusysDatasetsTable.id, { onDelete: 'cascade' }),
    recordData: (0, pg_core_1.jsonb)('record_data').notNull(),
    recordIndex: (0, pg_core_1.integer)('record_index').notNull(),
    createdAt: (0, pg_core_1.timestamp)('created_at').notNull().defaultNow(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at').notNull().defaultNow(),
}, (table) => ({
    datasetIdIdx: (0, pg_core_1.index)('dataset_records_dataset_id_idx').on(table.datasetId),
    recordIndexIdx: (0, pg_core_1.index)('dataset_records_record_index_idx').on(table.recordIndex),
    createdAtIdx: (0, pg_core_1.index)('dataset_records_created_at_idx').on(table.createdAt),
}));
exports.evalsToDatasetsTable = (0, pg_core_1.pgTable)('evals_to_datasets', {
    evalId: (0, pg_core_1.text)('eval_id')
        .notNull()
        .references(() => exports.evalsTable.id),
    datasetId: (0, pg_core_1.text)('dataset_id')
        .notNull()
        .references(() => exports.datasetsTable.id),
}, (t) => ({
    pk: (0, pg_core_1.primaryKey)({ columns: [t.evalId, t.datasetId] }),
    evalIdIdx: (0, pg_core_1.index)('evals_to_datasets_eval_id_idx').on(t.evalId),
    datasetIdIdx: (0, pg_core_1.index)('evals_to_datasets_dataset_id_idx').on(t.datasetId),
}));
exports.datasetsRelations = (0, drizzle_orm_1.relations)(exports.datasetsTable, ({ many }) => ({
    evalsToDatasets: many(exports.evalsToDatasetsTable),
}));
exports.evalsToDatasetsRelations = (0, drizzle_orm_1.relations)(exports.evalsToDatasetsTable, ({ one }) => ({
    eval: one(exports.evalsTable, {
        fields: [exports.evalsToDatasetsTable.evalId],
        references: [exports.evalsTable.id],
    }),
    dataset: one(exports.datasetsTable, {
        fields: [exports.evalsToDatasetsTable.datasetId],
        references: [exports.datasetsTable.id],
    }),
}));
// ------------ Test Runs ------------
exports.testRunsTable = (0, pg_core_1.pgTable)('test_runs', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    type: (0, pg_core_1.text)('type').default('eval'),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    status: (0, pg_core_1.text)('status').notNull().default('pending'), // pending, running, completed, failed
    projectId: (0, pg_core_1.text)('project_id'),
    applicationIds: (0, pg_core_1.jsonb)('application_ids').$type().notNull(),
    libraryId: (0, pg_core_1.text)('library_id').references(() => exports.promptsLibraryTable.id),
    workspaceId: (0, pg_core_1.text)('workspace_id'),
    is_archived: (0, pg_core_1.boolean)('is_archived').default(false),
    totalTestCase: (0, pg_core_1.integer)('total_test_case'),
    shareableReportExpiry: (0, pg_core_1.timestamp)('shareable_report_expiry', { withTimezone: true }),
    shareableReportAccessKey: (0, pg_core_1.text)('shareable_report_access_key'),
}, (table) => ({
    createdAtIdx: (0, pg_core_1.index)('test_runs_created_at_idx').on(table.createdAt),
    statusIdx: (0, pg_core_1.index)('test_runs_status_idx').on(table.status),
}));
exports.testRunsToEvalsTable = (0, pg_core_1.pgTable)('test_runs_to_evals', {
    testRunId: (0, pg_core_1.text)('test_run_id')
        .notNull()
        .references(() => exports.testRunsTable.id, { onDelete: 'cascade' }),
    evalId: (0, pg_core_1.text)('eval_id')
        .notNull()
        .references(() => exports.evalsTable.id),
}, (t) => ({
    pk: (0, pg_core_1.primaryKey)({ columns: [t.testRunId, t.evalId] }),
    testRunIdIdx: (0, pg_core_1.index)('test_runs_to_evals_test_run_id_idx').on(t.testRunId),
    evalIdIdx: (0, pg_core_1.index)('test_runs_to_evals_eval_id_idx').on(t.evalId),
}));
exports.testRunsToDatasetsTable = (0, pg_core_1.pgTable)('test_runs_to_datasets', {
    testRunId: (0, pg_core_1.text)('test_run_id')
        .notNull()
        .references(() => exports.testRunsTable.id, { onDelete: 'cascade' }),
    datasetId: (0, pg_core_1.text)('dataset_id')
        .notNull()
        .references(() => exports.trusysDatasetsTable.id),
    personas: (0, pg_core_1.jsonb)('personas').$type(),
    contexts: (0, pg_core_1.jsonb)('contexts').$type(),
}, (t) => ({
    pk: (0, pg_core_1.primaryKey)({ columns: [t.testRunId, t.datasetId] }),
    testRunIdIdx: (0, pg_core_1.index)('test_runs_to_datasets_test_run_id_idx').on(t.testRunId),
    datasetIdIdx: (0, pg_core_1.index)('test_runs_to_datasets_dataset_id_idx').on(t.datasetId),
}));
exports.testRunsToDatasetRecordsTable = (0, pg_core_1.pgTable)('test_runs_to_dataset_records', {
    testRunId: (0, pg_core_1.text)('test_run_id')
        .notNull()
        .references(() => exports.testRunsTable.id, { onDelete: 'cascade' }),
    datasetRecordId: (0, pg_core_1.text)('dataset_record_id')
        .notNull()
        .references(() => exports.datasetRecordsTable.id, { onDelete: 'cascade' }),
    datasetId: (0, pg_core_1.text)('dataset_id')
        .notNull()
        .references(() => exports.trusysDatasetsTable.id),
}, (t) => ({
    pk: (0, pg_core_1.primaryKey)({ columns: [t.testRunId, t.datasetRecordId] }),
    testRunIdIdx: (0, pg_core_1.index)('test_runs_to_dataset_records_test_run_id_idx').on(t.testRunId),
    datasetRecordIdIdx: (0, pg_core_1.index)('test_runs_to_dataset_records_dataset_record_id_idx').on(t.datasetRecordId),
    datasetIdIdx: (0, pg_core_1.index)('test_runs_to_dataset_records_dataset_id_idx').on(t.datasetId),
}));
// Update testRunsRelations to include datasets
exports.testRunsRelations = (0, drizzle_orm_1.relations)(exports.testRunsTable, ({ many, one }) => ({
    evals: many(exports.testRunsToEvalsTable),
    datasets: many(exports.testRunsToDatasetsTable),
    datasetRecords: many(exports.testRunsToDatasetRecordsTable),
    library: one(exports.promptsLibraryTable, {
        fields: [exports.testRunsTable.libraryId],
        references: [exports.promptsLibraryTable.id],
    }),
}));
// Add relations for trusysDatasetsTable
exports.trusysDatasetsRelations = (0, drizzle_orm_1.relations)(exports.trusysDatasetsTable, ({ many }) => ({
    testRuns: many(exports.testRunsToDatasetsTable),
    datasetRecords: many(exports.datasetRecordsTable),
}));
// Add relations for testRunsToDatasetsTable
exports.testRunsToDatasetsRelations = (0, drizzle_orm_1.relations)(exports.testRunsToDatasetsTable, ({ one }) => ({
    testRun: one(exports.testRunsTable, {
        fields: [exports.testRunsToDatasetsTable.testRunId],
        references: [exports.testRunsTable.id],
    }),
    dataset: one(exports.trusysDatasetsTable, {
        fields: [exports.testRunsToDatasetsTable.datasetId],
        references: [exports.trusysDatasetsTable.id],
    }),
}));
// ------------ Evals ------------
exports.evalsRelations = (0, drizzle_orm_1.relations)(exports.evalsTable, ({ many }) => ({
    evalsToPrompts: many(exports.evalsToPromptsTable),
    evalsToDatasets: many(exports.evalsToDatasetsTable),
    evalsToTags: many(exports.evalsToTagsTable),
    testRuns: many(exports.testRunsToEvalsTable),
}));
exports.evalsToPromptsRelations = (0, drizzle_orm_1.relations)(exports.evalsToPromptsTable, ({ one }) => ({
    eval: one(exports.evalsTable, {
        fields: [exports.evalsToPromptsTable.evalId],
        references: [exports.evalsTable.id],
    }),
    prompt: one(exports.promptsTable, {
        fields: [exports.evalsToPromptsTable.promptId],
        references: [exports.promptsTable.id],
    }),
}));
// // ------------ Configs ------------
exports.configsTable = (0, pg_core_1.pgTable)('configs', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true }).defaultNow().notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true }).defaultNow().notNull(),
    name: (0, pg_core_1.text)('name').notNull(),
    type: (0, pg_core_1.text)('type').notNull(),
    config: (0, pg_core_1.jsonb)('config').notNull(),
}, (t) => ({
    createdAtIdx: (0, pg_core_1.index)('configs_created_at_idx').on(t.createdAt),
    typeIdx: (0, pg_core_1.index)('configs_type_idx').on(t.type),
}));
// ------------ Assertions ------------
exports.assertionsTable = (0, pg_core_1.pgTable)('assertions', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    name: (0, pg_core_1.text)('name').notNull(),
    label: (0, pg_core_1.text)('label'),
    description: (0, pg_core_1.text)('description'),
    helpText: (0, pg_core_1.text)('help_text'),
    placeholder: (0, pg_core_1.text)('placeholder'),
    allowEval: (0, pg_core_1.integer)('allow_eval').default(1),
    allowObserve: (0, pg_core_1.integer)('allow_observe').default(1),
    group: (0, pg_core_1.text)('group'),
    valueType: (0, pg_core_1.text)('value_type'),
    mandatory: (0, pg_core_1.integer)('mandatory').default(0),
    additionalFields: (0, pg_core_1.jsonb)('additional_fields'),
    options: (0, pg_core_1.jsonb)('options'),
    /** Catalog metadata: what provider/eval pipeline must expose for this assertion (seeded JSON). */
    providerResponseRequirements: (0, pg_core_1.jsonb)('provider_response_requirements'),
}, (table) => ({
    createdAtIdx: (0, pg_core_1.index)('assertions_created_at_idx').on(table.createdAt),
    nameIdx: (0, pg_core_1.index)('assertions_name_idx').on(table.name),
    groupIdx: (0, pg_core_1.index)('assertions_group_idx').on(table.group),
}));
exports.defaultProvidersTable = (0, pg_core_1.pgTable)('default_providers', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    label: (0, pg_core_1.text)('label').notNull(),
    group: (0, pg_core_1.text)('group').notNull(),
    config: (0, pg_core_1.jsonb)('config').notNull(),
}, (table) => ({
    labelIdx: (0, pg_core_1.index)('default_providers_label_idx').on(table.label),
    groupIdx: (0, pg_core_1.index)('default_providers_group_idx').on(table.group),
}));
exports.pluginsPresetsTable = (0, pg_core_1.pgTable)('plugins_presets', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    name: (0, pg_core_1.text)('name').notNull(),
    description: (0, pg_core_1.text)('description'),
    plugins: (0, pg_core_1.jsonb)('plugins'),
}, (table) => ({
    nameIdx: (0, pg_core_1.index)('plugins_presets_name_idx').on(table.name),
}));
exports.strategyPresetsTable = (0, pg_core_1.pgTable)('strategy_presets', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    name: (0, pg_core_1.text)('name').notNull(),
    description: (0, pg_core_1.text)('description'),
    strategies: (0, pg_core_1.jsonb)('strategies'), // array of strategy ids
    options: (0, pg_core_1.jsonb)('options'), // optional extra config
}, (table) => ({
    nameIdx: (0, pg_core_1.index)('strategy_presets_name_idx').on(table.name),
}));
exports.providerSupportedLanguagesTable = (0, pg_core_1.pgTable)('provider_supported_languages', {
    id: (0, pg_core_1.text)('id').primaryKey().notNull(),
    name: (0, pg_core_1.text)('name').notNull(),
    code: (0, pg_core_1.text)('code').notNull(),
    SpeechSynthesisLanguage: (0, pg_core_1.text)('SpeechSynthesisLanguage').notNull(),
    SpeechSynthesisVoiceName: (0, pg_core_1.text)('SpeechSynthesisVoiceName').notNull(),
}, (table) => ({
    nameIdx: (0, pg_core_1.index)('provider_supported_languages_name_idx').on(table.name),
    codeIdx: (0, pg_core_1.index)('provider_supported_languages_code_idx').on(table.code),
}));
// ------------ Security Config ------------
exports.securityConfigTable = (0, pg_core_1.pgTable)('security_config', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    name: (0, pg_core_1.text)('name').notNull(),
    status: (0, pg_core_1.integer)('status').notNull(),
    config: (0, pg_core_1.jsonb)('config').notNull(),
    projectId: (0, pg_core_1.text)('project_id'),
    workspaceId: (0, pg_core_1.text)('workspace_id'),
    is_archived: (0, pg_core_1.boolean)('is_archived').default(false).notNull(),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (table) => ({
    nameIdx: (0, pg_core_1.index)('security_config_name_idx').on(table.name),
    statusIdx: (0, pg_core_1.index)('security_config_status_idx').on(table.status),
    projectIdIdx: (0, pg_core_1.index)('security_config_project_id_idx').on(table.projectId),
}));
exports.testRunsToSecurityConfigsTable = (0, pg_core_1.pgTable)('test_runs_to_security_configs', {
    testRunId: (0, pg_core_1.text)('test_run_id')
        .notNull()
        .references(() => exports.testRunsTable.id, { onDelete: 'cascade' }),
    securityConfigId: (0, pg_core_1.text)('security_config_id')
        .notNull()
        .references(() => exports.securityConfigTable.id),
    /** Snapshot of `security_config.config` at run start; null for legacy rows. */
    config: (0, pg_core_1.jsonb)('config').$type(),
}, (t) => ({
    pk: (0, pg_core_1.primaryKey)({ columns: [t.testRunId, t.securityConfigId] }),
    testRunIdIdx: (0, pg_core_1.index)('test_runs_to_security_configs_test_run_id_idx').on(t.testRunId),
    securityConfigIdIdx: (0, pg_core_1.index)('test_runs_to_security_configs_security_config_id_idx').on(t.securityConfigId),
}));
// ------------ Pulse Monitoring ------------
exports.pulseMonitoringTable = (0, pg_core_1.pgTable)('monitoring', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    applicationId: (0, pg_core_1.text)('application_id')
        .notNull()
        .references(() => exports.applicationsTable.id),
    workspaceId: (0, pg_core_1.text)('workspace_id'),
    projectId: (0, pg_core_1.text)('project_id'),
    logLocation: (0, pg_core_1.text)('log_location').notNull(),
    serviceProvider: (0, pg_core_1.text)('service_provider').notNull(),
    locationUrl: (0, pg_core_1.text)('location_url').notNull(),
    samplingFrequency: (0, pg_core_1.text)('sampling_frequency').notNull(),
    samplingPercentage: (0, pg_core_1.integer)('sampling_percentage').notNull(),
    samplingCount: (0, pg_core_1.integer)('sampling_count').notNull(),
    evaluationWindow: (0, pg_core_1.integer)('evaluation_window').notNull(),
    metrics: (0, pg_core_1.jsonb)('metrics').notNull(),
    pluginsByCategory: (0, pg_core_1.jsonb)('pluginsByCategory'),
    secretName: (0, pg_core_1.text)('secret_name').notNull(),
}, (table) => ({
    applicationIdIdx: (0, pg_core_1.index)('monitoring_application_id_idx').on(table.applicationId),
}));
// ------------ Argus Chat History ------------
// Stores Ask Argus chat history across all domains (pulse, eval, scout)
exports.argusChatHistoryTable = (0, pg_core_1.pgTable)('argus_chat_history', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    query: (0, pg_core_1.text)('query').notNull(),
    result: (0, pg_core_1.text)('result').notNull(),
    type: (0, pg_core_1.text)('type').notNull(), // 'pulse' | 'eval' | 'scout'
    userId: (0, pg_core_1.text)('user_id'),
    username: (0, pg_core_1.text)('username'),
    orgId: (0, pg_core_1.text)('org_id'),
    projectId: (0, pg_core_1.text)('project_id'),
    applicationId: (0, pg_core_1.text)('application_id'),
    securityConfigId: (0, pg_core_1.text)('security_config_id'),
    traceId: (0, pg_core_1.text)('trace_id'),
    sessionId: (0, pg_core_1.text)('session_id'),
    testRunId: (0, pg_core_1.text)('test_run_id'),
    threadId: (0, pg_core_1.text)('thread_id'),
    contextSource: (0, pg_core_1.text)('context_source'),
    chatGroupId: (0, pg_core_1.text)('chat_group_id'),
    chatGroupStatus: (0, pg_core_1.text)('chat_group_status'),
    chatGroupTitle: (0, pg_core_1.text)('chat_group_title'),
    filters: (0, pg_core_1.jsonb)('filters'),
    usage: (0, pg_core_1.jsonb)('usage'),
    userFeedback: (0, pg_core_1.integer)('user_feedback'), // 1 = thumbs up, -1 = thumbs down, null = no feedback
    userFeedbackAt: (0, pg_core_1.timestamp)('user_feedback_at', { withTimezone: true }),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (table) => ({
    projectIdIdx: (0, pg_core_1.index)('argus_chat_history_project_id_idx').on(table.projectId),
    applicationIdIdx: (0, pg_core_1.index)('argus_chat_history_application_id_idx').on(table.applicationId),
    securityConfigIdIdx: (0, pg_core_1.index)('argus_chat_history_security_config_id_idx').on(table.securityConfigId),
    typeIdx: (0, pg_core_1.index)('argus_chat_history_type_idx').on(table.type),
    contextGroupIdx: (0, pg_core_1.index)('argus_chat_history_context_group_idx').on(table.projectId, table.userId, table.type, table.contextSource, table.chatGroupId, table.createdAt),
}));
exports.pulseMonitoringAlertsTable = (0, pg_core_1.pgTable)('monitoring_alerts', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    name: (0, pg_core_1.text)('name').notNull(),
    applicationId: (0, pg_core_1.text)('application_id')
        .notNull()
        .references(() => exports.applicationsTable.id),
    alert: (0, pg_core_1.jsonb)('alert'),
    emails: (0, pg_core_1.text)('emails'),
    webhook: (0, pg_core_1.text)('webhook'),
    projectId: (0, pg_core_1.text)('project_id').notNull(),
    workspaceId: (0, pg_core_1.text)('workspace_id'),
    status: (0, pg_core_1.integer)('status').default(0),
    condition: (0, pg_core_1.text)('condition'),
    alertsTriggered: (0, pg_core_1.integer)('alerts_triggered').default(0),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (table) => ({
    applicationIdIdx: (0, pg_core_1.index)('monitoring_alerts_application_id_idx').on(table.applicationId),
}));
// New relation table: evalsToTracesTable
exports.evalsToTracesTable = (0, pg_core_1.pgTable)('evals_to_traces', {
    evalId: (0, pg_core_1.text)('eval_id')
        .notNull()
        .references(() => exports.evalsTable.id),
    traceId: (0, pg_core_1.text)('trace_id').notNull(),
    projectId: (0, pg_core_1.text)('project_id').notNull(),
    workspaceId: (0, pg_core_1.text)('workspace_id'),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (t) => ({
    pk: (0, pg_core_1.primaryKey)({ columns: [t.evalId, t.traceId] }),
    evalIdIdx: (0, pg_core_1.index)('evals_to_traces_eval_id_idx').on(t.evalId),
    traceIdIdx: (0, pg_core_1.index)('evals_to_traces_trace_id_idx').on(t.traceId),
    projectId: (0, pg_core_1.index)('evals_to_traces_project_id_idx').on(t.projectId),
}));
// Pulse Alert Logs table
exports.pulseAlertLogsTable = (0, pg_core_1.pgTable)('pulse_alert_logs', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    alertId: (0, pg_core_1.text)('alert_id')
        .notNull()
        .references(() => exports.pulseMonitoringAlertsTable.id),
    metrics: (0, pg_core_1.jsonb)('metrics'),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    count: (0, pg_core_1.integer)('count').default(0),
    timestamp: (0, pg_core_1.timestamp)('timestamp', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    applicationId: (0, pg_core_1.text)('application_id')
        .notNull()
        .references(() => exports.applicationsTable.id),
    projectId: (0, pg_core_1.text)('project_id').notNull(),
    workspaceId: (0, pg_core_1.text)('workspace_id'),
}, (table) => ({
    alertIdIdx: (0, pg_core_1.index)('pulse_alert_logs_alert_id_idx').on(table.alertId),
    applicationIdIdx: (0, pg_core_1.index)('pulse_alert_logs_application_id_idx').on(table.applicationId),
    projectIdIdx: (0, pg_core_1.index)('pulse_alert_logs_project_id_idx').on(table.projectId),
    createdAtIdx: (0, pg_core_1.index)('pulse_alert_logs_created_at_idx').on(table.createdAt),
}));
// Notification Channel table
exports.notificationChannelTable = (0, pg_core_1.pgTable)('notification_channels', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    name: (0, pg_core_1.text)('name').notNull(),
    type: (0, pg_core_1.integer)('type').notNull().default(1), // 'email' or 'webhook'
    config: (0, pg_core_1.jsonb)('config').notNull(), // Contains email addresses or webhook configuration
    projectId: (0, pg_core_1.text)('project_id').notNull(),
    workspaceId: (0, pg_core_1.text)('workspace_id'),
    status: (0, pg_core_1.integer)('status').default(1),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (table) => ({
    projectIdIdx: (0, pg_core_1.index)('notification_channels_project_id_idx').on(table.projectId),
    typeIdx: (0, pg_core_1.index)('notification_channels_type_idx').on(table.type),
    statusIdx: (0, pg_core_1.index)('notification_channels_status_idx').on(table.status),
    createdAtIdx: (0, pg_core_1.index)('notification_channels_created_at_idx').on(table.createdAt),
}));
// Relation table between pulseMonitoringAlertsTable and notificationChannelTable
exports.pulseMonitoringAlertsToNotificationChannelsTable = (0, pg_core_1.pgTable)('pulse_monitoring_alerts_to_notification_channels', {
    alertId: (0, pg_core_1.text)('alert_id')
        .notNull()
        .references(() => exports.pulseMonitoringAlertsTable.id, { onDelete: 'cascade' }),
    notificationChannelId: (0, pg_core_1.text)('notification_channel_id')
        .notNull()
        .references(() => exports.notificationChannelTable.id, { onDelete: 'cascade' }),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (t) => ({
    pk: (0, pg_core_1.primaryKey)({ columns: [t.alertId, t.notificationChannelId] }),
    alertIdIdx: (0, pg_core_1.index)('pulse_monitoring_alerts_to_notification_channels_alert_id_idx').on(t.alertId),
    notificationChannelIdIdx: (0, pg_core_1.index)('pulse_monitoring_alerts_to_notification_channels_notification_channel_id_idx').on(t.notificationChannelId),
}));
// Relations for notification channel tables
exports.notificationChannelRelations = (0, drizzle_orm_1.relations)(exports.notificationChannelTable, ({ many }) => ({
    pulseMonitoringAlerts: many(exports.pulseMonitoringAlertsToNotificationChannelsTable),
}));
exports.pulseMonitoringAlertsToNotificationChannelsRelations = (0, drizzle_orm_1.relations)(exports.pulseMonitoringAlertsToNotificationChannelsTable, ({ one }) => ({
    alert: one(exports.pulseMonitoringAlertsTable, {
        fields: [exports.pulseMonitoringAlertsToNotificationChannelsTable.alertId],
        references: [exports.pulseMonitoringAlertsTable.id],
    }),
    notificationChannel: one(exports.notificationChannelTable, {
        fields: [exports.pulseMonitoringAlertsToNotificationChannelsTable.notificationChannelId],
        references: [exports.notificationChannelTable.id],
    }),
}));
// Update pulseMonitoringAlertsTable relations to include notification channels
exports.pulseMonitoringAlertsRelations = (0, drizzle_orm_1.relations)(exports.pulseMonitoringAlertsTable, ({ many }) => ({
    notificationChannels: many(exports.pulseMonitoringAlertsToNotificationChannelsTable),
}));
// Relations for dataset records
exports.datasetRecordsRelations = (0, drizzle_orm_1.relations)(exports.datasetRecordsTable, ({ one, many }) => ({
    dataset: one(exports.trusysDatasetsTable, {
        fields: [exports.datasetRecordsTable.datasetId],
        references: [exports.trusysDatasetsTable.id],
    }),
    testRuns: many(exports.testRunsToDatasetRecordsTable),
}));
// Add relations for testRunsToDatasetRecordsTable
exports.testRunsToDatasetRecordsRelations = (0, drizzle_orm_1.relations)(exports.testRunsToDatasetRecordsTable, ({ one }) => ({
    testRun: one(exports.testRunsTable, {
        fields: [exports.testRunsToDatasetRecordsTable.testRunId],
        references: [exports.testRunsTable.id],
    }),
    datasetRecord: one(exports.datasetRecordsTable, {
        fields: [exports.testRunsToDatasetRecordsTable.datasetRecordId],
        references: [exports.datasetRecordsTable.id],
    }),
    dataset: one(exports.trusysDatasetsTable, {
        fields: [exports.testRunsToDatasetRecordsTable.datasetId],
        references: [exports.trusysDatasetsTable.id],
    }),
}));
// ------------ Prompt Optimization Runs ------------
exports.optimizationRunsTable = (0, pg_core_1.pgTable)('optimization_runs', {
    id: (0, pg_core_1.uuid)('id').primaryKey(),
    experimentName: (0, pg_core_1.text)('experiment_name').notNull(),
    description: (0, pg_core_1.text)('description'),
    optimizerType: (0, pg_core_1.text)('optimizer_type').notNull(),
    metricName: (0, pg_core_1.text)('metric_name').notNull(),
    metricConfig: (0, pg_core_1.jsonb)('metric_config'),
    llmModel: (0, pg_core_1.text)('llm_model').notNull(),
    llmParameters: (0, pg_core_1.jsonb)('llm_parameters'),
    initialPrompt: (0, pg_core_1.text)('initial_prompt'),
    initialScore: (0, pg_core_1.doublePrecision)('initial_score'),
    /** ParameterOptimizer output: optimized_parameters, parameter_importance, optimizer_details */
    optimizationSummary: (0, pg_core_1.jsonb)('optimization_summary'),
    status: (0, pg_core_1.text)('status').notNull(),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true }).defaultNow().notNull(),
    completedAt: (0, pg_core_1.timestamp)('completed_at', { withTimezone: true }),
    bestTrialId: (0, pg_core_1.uuid)('best_trial_id'),
}, (table) => ({
    createdAtIdx: (0, pg_core_1.index)('optimization_runs_created_at_idx').on(table.createdAt),
}));
exports.trialsTable = (0, pg_core_1.pgTable)('trials', {
    id: (0, pg_core_1.uuid)('id').primaryKey(),
    optimizationRunId: (0, pg_core_1.uuid)('optimization_run_id')
        .notNull()
        .references(() => exports.optimizationRunsTable.id, { onDelete: 'cascade' }),
    trialNumber: (0, pg_core_1.integer)('trial_number').notNull(),
    // Self-referential FK is modeled via relations; we don't declare a DB-level FK here
    parentTrialId: (0, pg_core_1.uuid)('parent_trial_id'),
    promptSystem: (0, pg_core_1.text)('prompt_system'),
    promptUser: (0, pg_core_1.text)('prompt_user'),
    score: (0, pg_core_1.doublePrecision)('score'),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true }).defaultNow().notNull(),
}, (table) => ({
    optimizationRunIdIdx: (0, pg_core_1.index)('trials_optimization_run_id_idx').on(table.optimizationRunId),
    scoreIdx: (0, pg_core_1.index)('trials_score_idx').on(table.score),
    parentTrialIdIdx: (0, pg_core_1.index)('trials_parent_trial_id_idx').on(table.parentTrialId),
}));
exports.trialDatasetResultsTable = (0, pg_core_1.pgTable)('trial_dataset_results', {
    id: (0, pg_core_1.uuid)('id').primaryKey(),
    trialId: (0, pg_core_1.uuid)('trial_id')
        .notNull()
        .references(() => exports.trialsTable.id, { onDelete: 'cascade' }),
    datasetSampleId: (0, pg_core_1.uuid)('dataset_sample_id').notNull(),
    datasetInput: (0, pg_core_1.text)('dataset_input'),
    modelOutput: (0, pg_core_1.text)('model_output'),
    metricScore: (0, pg_core_1.doublePrecision)('metric_score'),
}, (table) => ({
    trialIdIdx: (0, pg_core_1.index)('trial_dataset_results_trial_id_idx').on(table.trialId),
}));
exports.optimizationRunsRelations = (0, drizzle_orm_1.relations)(exports.optimizationRunsTable, ({ many }) => ({
    trials: many(exports.trialsTable),
}));
exports.trialsRelations = (0, drizzle_orm_1.relations)(exports.trialsTable, ({ one, many }) => ({
    optimizationRun: one(exports.optimizationRunsTable, {
        fields: [exports.trialsTable.optimizationRunId],
        references: [exports.optimizationRunsTable.id],
    }),
    parentTrial: one(exports.trialsTable, {
        fields: [exports.trialsTable.parentTrialId],
        references: [exports.trialsTable.id],
    }),
    datasetResults: many(exports.trialDatasetResultsTable),
}));
exports.trialDatasetResultsRelations = (0, drizzle_orm_1.relations)(exports.trialDatasetResultsTable, ({ one }) => ({
    trial: one(exports.trialsTable, {
        fields: [exports.trialDatasetResultsTable.trialId],
        references: [exports.trialsTable.id],
    }),
}));
exports.defaultObserveDashboardTable = (0, pg_core_1.pgTable)('default_observe_dashboard', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    name: (0, pg_core_1.text)('name').notNull(),
    description: (0, pg_core_1.text)('description'),
    widgets: (0, pg_core_1.jsonb)('widgets').$type().default([]),
    positions: (0, pg_core_1.jsonb)('positions').$type().default([]),
    status: (0, pg_core_1.integer)('status').default(0).notNull(), // 0: draft, 1: published
    type: (0, pg_core_1.text)('type').notNull(), // 'agentic', 'non-agentic'
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
});
// Observe Dashboard
exports.observeDashboardTable = (0, pg_core_1.pgTable)('observe_dashboard', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    name: (0, pg_core_1.text)('name').notNull(),
    workspaceId: (0, pg_core_1.text)('workspace_id').notNull(),
    description: (0, pg_core_1.text)('description'),
    status: (0, pg_core_1.integer)('status').default(0).notNull(), // 0: draft, 1: published
    type: (0, pg_core_1.text)('type').notNull(), // 'agentic', 'non-agentic'
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (t) => ({
    workspaceIdIdx: (0, pg_core_1.index)('observe_dashboard_workspace_id_idx').on(t.workspaceId),
}));
// Observe Widgets
exports.observeWidgetTable = (0, pg_core_1.pgTable)('observe_widgets', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    workspaceId: (0, pg_core_1.text)('workspace_id').default('default').notNull(),
    name: (0, pg_core_1.text)('name').notNull(),
    description: (0, pg_core_1.text)('description').default(''),
    parameters: (0, pg_core_1.jsonb)('parameters').$type().default({}),
    type: (0, pg_core_1.text)('type').notNull(),
    chartType: (0, pg_core_1.text)('chart_type').notNull(),
    title: (0, pg_core_1.text)('title').notNull(),
    items: (0, pg_core_1.jsonb)('items').$type().default([]),
    metadata: (0, pg_core_1.jsonb)('metadata').$type().default({}),
    is_custom: (0, pg_core_1.boolean)('is_custom').default(false).notNull(),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (t) => ({
    workspaceIdIdx: (0, pg_core_1.index)('observe_widgets_workspace_id_idx').on(t.workspaceId),
}));
// Observe Widgets Positions
exports.observeDashboardWidgetsTable = (0, pg_core_1.pgTable)('observe_dashboard_widgets', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    workspaceId: (0, pg_core_1.text)('workspace_id').notNull(),
    dashboardId: (0, pg_core_1.text)('dashboard_id')
        .notNull()
        .references(() => exports.observeDashboardTable.id),
    widgetId: (0, pg_core_1.text)('widget_id').notNull(),
    position: (0, pg_core_1.jsonb)('position').notNull(),
    name: (0, pg_core_1.text)('name').notNull(),
    description: (0, pg_core_1.text)('description').default(''),
    parameters: (0, pg_core_1.jsonb)('parameters').$type().default({}),
    type: (0, pg_core_1.text)('type').notNull(),
    chartType: (0, pg_core_1.text)('chart_type').notNull(),
    title: (0, pg_core_1.text)('title').notNull(),
    items: (0, pg_core_1.jsonb)('items').$type().default([]),
    metadata: (0, pg_core_1.jsonb)('metadata').$type().default({}),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (t) => ({
    workspaceIdIdx: (0, pg_core_1.index)('observe_dashboard_widgets_workspace_id_idx').on(t.workspaceId),
    widgetId: (0, pg_core_1.index)('observe_dashboard_widgets_widget_id_idx').on(t.widgetId),
    dashboardIdIdx: (0, pg_core_1.index)('observe_dashboard_widgets_dashboard_id_idx').on(t.dashboardId),
}));
// Relations for observe dashboard widgets
exports.observeDashboardWidgetsRelations = (0, drizzle_orm_1.relations)(exports.observeDashboardWidgetsTable, ({ one }) => ({
    dashboard: one(exports.observeDashboardTable, {
        fields: [exports.observeDashboardWidgetsTable.dashboardId],
        references: [exports.observeDashboardTable.id],
    }),
    widget: one(exports.observeWidgetTable, {
        fields: [exports.observeDashboardWidgetsTable.widgetId],
        references: [exports.observeWidgetTable.id],
    }),
}));
// Relations for observe dashboard (one dashboard has many widgets)
exports.observeDashboardRelations = (0, drizzle_orm_1.relations)(exports.observeDashboardTable, ({ many }) => ({
    widgets: many(exports.observeDashboardWidgetsTable),
}));
// ------------ Boards ------------
// 1. Board table: core board entity scoped to workspace/project/application
exports.boardsTable = (0, pg_core_1.pgTable)('boards', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    name: (0, pg_core_1.text)('name').notNull(),
    description: (0, pg_core_1.text)('description'),
    status: (0, pg_core_1.integer)('status').default(0).notNull(), // e.g. 0: draft, 1: active, 2: archived
    applicationId: (0, pg_core_1.text)('application_id').references(() => exports.applicationsTable.id),
    projectId: (0, pg_core_1.text)('project_id').notNull(),
    workspaceId: (0, pg_core_1.text)('workspace_id').notNull(),
    createdBy: (0, pg_core_1.text)('created_by'), // userId of creator (no users table in this DB)
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (table) => ({
    workspaceIdIdx: (0, pg_core_1.index)('boards_workspace_id_idx').on(table.workspaceId),
    projectIdIdx: (0, pg_core_1.index)('boards_project_id_idx').on(table.projectId),
    applicationIdIdx: (0, pg_core_1.index)('boards_application_id_idx').on(table.applicationId),
    statusIdx: (0, pg_core_1.index)('boards_status_idx').on(table.status),
    updatedAtIdx: (0, pg_core_1.index)('boards_updated_at_idx').on(table.updatedAt),
    createdByIdx: (0, pg_core_1.index)('boards_created_by_idx').on(table.createdBy),
}));
// 2. Board User Mapping: roles per user per board (creator / viewer / editor)
exports.boardUserMappingTable = (0, pg_core_1.pgTable)('board_user_mapping', {
    boardId: (0, pg_core_1.text)('board_id')
        .notNull()
        .references(() => exports.boardsTable.id, { onDelete: 'cascade' }),
    userId: (0, pg_core_1.text)('user_id').notNull(),
    role: (0, pg_core_1.text)('role').notNull(), // 'creator' | 'viewer' | 'editor'
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (t) => ({
    pk: (0, pg_core_1.primaryKey)({ columns: [t.boardId, t.userId] }),
    boardIdIdx: (0, pg_core_1.index)('board_user_mapping_board_id_idx').on(t.boardId),
    userIdIdx: (0, pg_core_1.index)('board_user_mapping_user_id_idx').on(t.userId),
    roleIdx: (0, pg_core_1.index)('board_user_mapping_role_idx').on(t.role),
}));
// 3. Widgets: global widget catalog (name, type, chartType, config shape)
exports.boardWidgetsTable = (0, pg_core_1.pgTable)('board_widgets', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    name: (0, pg_core_1.text)('name').notNull(),
    category: (0, pg_core_1.text)('category').default(''),
    group: (0, pg_core_1.text)('group').default(''),
    description: (0, pg_core_1.text)('description').default(''),
    parameters: (0, pg_core_1.jsonb)('parameters').$type().default({}),
    type: (0, pg_core_1.text)('type').notNull(),
    chartType: (0, pg_core_1.text)('chart_type').notNull(),
    title: (0, pg_core_1.text)('title').notNull(),
    items: (0, pg_core_1.jsonb)('items').$type().default([]),
    metadata: (0, pg_core_1.jsonb)('metadata').$type().default({}),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (table) => ({
    typeIdx: (0, pg_core_1.index)('board_widgets_type_idx').on(table.type),
    chartTypeIdx: (0, pg_core_1.index)('board_widgets_chart_type_idx').on(table.chartType),
}));
// 4. Board Widgets Mapping: placement + board-specific overrides per widget instance
exports.boardWidgetsMappingTable = (0, pg_core_1.pgTable)('board_widgets_mapping', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    boardId: (0, pg_core_1.text)('board_id')
        .notNull()
        .references(() => exports.boardsTable.id, { onDelete: 'cascade' }),
    workspaceId: (0, pg_core_1.text)('workspace_id').notNull(),
    projectId: (0, pg_core_1.text)('project_id').notNull(),
    applicationId: (0, pg_core_1.text)('application_id').references(() => exports.applicationsTable.id),
    widgetId: (0, pg_core_1.text)('widget_id')
        .notNull()
        .references(() => exports.boardWidgetsTable.id),
    position: (0, pg_core_1.jsonb)('position')
        .$type()
        .notNull(),
    name: (0, pg_core_1.text)('name').notNull(),
    description: (0, pg_core_1.text)('description').default(''),
    parameters: (0, pg_core_1.jsonb)('parameters').$type().default({}),
    type: (0, pg_core_1.text)('type').notNull(),
    chartType: (0, pg_core_1.text)('chart_type').notNull(),
    title: (0, pg_core_1.text)('title').notNull(),
    items: (0, pg_core_1.jsonb)('items').$type().default([]),
    metadata: (0, pg_core_1.jsonb)('metadata').$type().default({}),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (t) => ({
    boardIdIdx: (0, pg_core_1.index)('board_widgets_mapping_board_id_idx').on(t.boardId),
    workspaceIdIdx: (0, pg_core_1.index)('board_widgets_mapping_workspace_id_idx').on(t.workspaceId),
    projectIdIdx: (0, pg_core_1.index)('board_widgets_mapping_project_id_idx').on(t.projectId),
    applicationIdIdx: (0, pg_core_1.index)('board_widgets_mapping_application_id_idx').on(t.applicationId),
    widgetIdIdx: (0, pg_core_1.index)('board_widgets_mapping_widget_id_idx').on(t.widgetId),
}));
// Board relations
exports.boardsRelations = (0, drizzle_orm_1.relations)(exports.boardsTable, ({ many, one }) => ({
    userMappings: many(exports.boardUserMappingTable),
    widgetMappings: many(exports.boardWidgetsMappingTable),
    application: one(exports.applicationsTable, {
        fields: [exports.boardsTable.applicationId],
        references: [exports.applicationsTable.id],
    }),
}));
exports.boardUserMappingRelations = (0, drizzle_orm_1.relations)(exports.boardUserMappingTable, ({ one }) => ({
    board: one(exports.boardsTable, {
        fields: [exports.boardUserMappingTable.boardId],
        references: [exports.boardsTable.id],
    }),
}));
exports.boardWidgetsRelations = (0, drizzle_orm_1.relations)(exports.boardWidgetsTable, ({ many }) => ({
    boardMappings: many(exports.boardWidgetsMappingTable),
}));
exports.boardWidgetsMappingRelations = (0, drizzle_orm_1.relations)(exports.boardWidgetsMappingTable, ({ one }) => ({
    board: one(exports.boardsTable, {
        fields: [exports.boardWidgetsMappingTable.boardId],
        references: [exports.boardsTable.id],
    }),
    widget: one(exports.boardWidgetsTable, {
        fields: [exports.boardWidgetsMappingTable.widgetId],
        references: [exports.boardWidgetsTable.id],
    }),
}));
// ------------ Provider Configurations ------------
// Table for workspace-level provider configurations (TTS, STT, etc.)
exports.providersConfigTable = (0, pg_core_1.pgTable)('providers_config', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    workspaceId: (0, pg_core_1.text)('workspace_id').notNull(),
    providerType: (0, pg_core_1.text)('provider_type').notNull(), // e.g., 'tts', 'stt'
    providerName: (0, pg_core_1.text)('provider_name').notNull(), // e.g., 'elevenlabs', 'deepgram', 'azure-speech'
    config: (0, pg_core_1.jsonb)('config').$type(), // Flexible JSONB for provider-specific configuration
    isDefault: (0, pg_core_1.boolean)('is_default').default(false).notNull(), // Whether this is the default provider for the type
    status: (0, pg_core_1.integer)('status').default(1).notNull(), // 1 = active, 0 = inactive
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (table) => ({
    workspaceIdIdx: (0, pg_core_1.index)('providers_config_workspace_id_idx').on(table.workspaceId),
    providerTypeIdx: (0, pg_core_1.index)('providers_config_provider_type_idx').on(table.providerType),
    providerNameIdx: (0, pg_core_1.index)('providers_config_provider_name_idx').on(table.providerName),
    workspaceProviderTypeIdx: (0, pg_core_1.index)('providers_config_workspace_provider_type_idx').on(table.workspaceId, table.providerType),
    statusIdx: (0, pg_core_1.index)('providers_config_status_idx').on(table.status),
    createdAtIdx: (0, pg_core_1.index)('providers_config_created_at_idx').on(table.createdAt),
}));
// ------------ Trusys Secrets Vault ------------
// Table for storing secrets encrypted in the database (similar to Azure Key Vault)
exports.trusysSecretsVaultTable = (0, pg_core_1.pgTable)('trusys_secrets_vault', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    name: (0, pg_core_1.text)('name').notNull(),
    secrets: (0, pg_core_1.jsonb)('secrets').$type().notNull(), // Encrypted secrets stored as JSONB
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (table) => ({
    nameIdx: (0, pg_core_1.index)('trusys_secrets_vault_name_idx').on(table.name),
    nameUniqueIdx: (0, pg_core_1.uniqueIndex)('trusys_secrets_vault_name_unique').on(table.name),
    createdAtIdx: (0, pg_core_1.index)('trusys_secrets_vault_created_at_idx').on(table.createdAt),
}));
// ------------ Schedule Jobs ------------
exports.scheduleJobsTable = (0, pg_core_1.pgTable)('schedule_jobs', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    name: (0, pg_core_1.text)('name'),
    type: (0, pg_core_1.integer)('type').notNull(), // 1: functional_eval, 2: security_eval
    status: (0, pg_core_1.integer)('status').default(1).notNull(), // 1: active, 2: archived
    projectId: (0, pg_core_1.text)('project_id').notNull(),
    workspaceId: (0, pg_core_1.text)('workspace_id'),
    applicationId: (0, pg_core_1.text)('application_id').references(() => exports.applicationsTable.id),
    libraryId: (0, pg_core_1.text)('library_id').references(() => exports.promptsLibraryTable.id),
    securityConfigId: (0, pg_core_1.text)('security_config_id').references(() => exports.securityConfigTable.id),
    startDate: (0, pg_core_1.timestamp)('start_date', { withTimezone: true }),
    runTime: (0, pg_core_1.text)('run_time'), // 'HH:mm'
    timeZone: (0, pg_core_1.text)('time_zone'), // IANA tz, e.g. 'America/Los_Angeles'
    repeatEvery: (0, pg_core_1.integer)('repeat_every'), // Number (e.g., 2)
    repeatUnit: (0, pg_core_1.text)('repeat_unit'), // 'day', 'week', 'month'
    repeatOnDays: (0, pg_core_1.jsonb)('repeat_on_days').$type(), // 0 (Sun) ... 6 (Sat); only used for weekly schedules
    endsOn: (0, pg_core_1.text)('ends_on'), // 'on' (specific date), 'never'
    endsDate: (0, pg_core_1.timestamp)('ends_date', { withTimezone: true }),
    endsAfter: (0, pg_core_1.integer)('ends_after'), // Number (e.g., 2)
    lastScheduleJobRun: (0, pg_core_1.timestamp)('last_schedule_job_run', { withTimezone: true }),
    nextRunAt: (0, pg_core_1.timestamp)('next_run_at', { withTimezone: true }),
    is_archived: (0, pg_core_1.boolean)('is_archived').default(false).notNull(),
}, (table) => ({
    createdAtIdx: (0, pg_core_1.index)('schedule_jobs_created_at_idx').on(table.createdAt),
    projectIdIdx: (0, pg_core_1.index)('schedule_jobs_project_id_idx').on(table.projectId),
    statusIdx: (0, pg_core_1.index)('schedule_jobs_status_idx').on(table.status),
    typeIdx: (0, pg_core_1.index)('schedule_jobs_type_idx').on(table.type),
    nextRunAtIdx: (0, pg_core_1.index)('schedule_jobs_next_run_at_idx').on(table.nextRunAt),
}));
// ------------ Test Runs to Schedule Jobs Mapping ------------
exports.testRunsToScheduleJobsTable = (0, pg_core_1.pgTable)('test_runs_to_schedule_jobs', {
    testRunId: (0, pg_core_1.text)('test_run_id')
        .notNull()
        .references(() => exports.testRunsTable.id, { onDelete: 'cascade' }),
    scheduleJobId: (0, pg_core_1.text)('schedule_job_id')
        .notNull()
        .references(() => exports.scheduleJobsTable.id, { onDelete: 'cascade' }),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (t) => ({
    pk: (0, pg_core_1.primaryKey)({ columns: [t.testRunId, t.scheduleJobId] }),
    testRunIdIdx: (0, pg_core_1.index)('test_runs_to_schedule_jobs_test_run_id_idx').on(t.testRunId),
    scheduleJobIdIdx: (0, pg_core_1.index)('test_runs_to_schedule_jobs_schedule_job_id_idx').on(t.scheduleJobId),
}));
// Relations for schedule jobs
exports.scheduleJobsRelations = (0, drizzle_orm_1.relations)(exports.scheduleJobsTable, ({ many, one }) => ({
    testRuns: many(exports.testRunsToScheduleJobsTable),
    application: one(exports.applicationsTable, {
        fields: [exports.scheduleJobsTable.applicationId],
        references: [exports.applicationsTable.id],
    }),
    library: one(exports.promptsLibraryTable, {
        fields: [exports.scheduleJobsTable.libraryId],
        references: [exports.promptsLibraryTable.id],
    }),
    securityConfig: one(exports.securityConfigTable, {
        fields: [exports.scheduleJobsTable.securityConfigId],
        references: [exports.securityConfigTable.id],
    }),
}));
// Relations for test runs to schedule jobs
exports.testRunsToScheduleJobsRelations = (0, drizzle_orm_1.relations)(exports.testRunsToScheduleJobsTable, ({ one }) => ({
    testRun: one(exports.testRunsTable, {
        fields: [exports.testRunsToScheduleJobsTable.testRunId],
        references: [exports.testRunsTable.id],
    }),
    scheduleJob: one(exports.scheduleJobsTable, {
        fields: [exports.testRunsToScheduleJobsTable.scheduleJobId],
        references: [exports.scheduleJobsTable.id],
    }),
}));
// ------------ Application Guardrails ------------
exports.applicationGuardrailsTable = (0, pg_core_1.pgTable)('application_guardrails', {
    id: (0, pg_core_1.text)('id').primaryKey(),
    applicationId: (0, pg_core_1.text)('application_id')
        .notNull()
        .references(() => exports.applicationsTable.id, { onDelete: 'cascade' }),
    enable_guard_rails: (0, pg_core_1.boolean)('enable_guard_rails').default(false).notNull(),
    input_validators: (0, pg_core_1.jsonb)('input_validators').default([]),
    output_validators: (0, pg_core_1.jsonb)('output_validators').default([]),
    createdAt: (0, pg_core_1.timestamp)('created_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
    updatedAt: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true })
        .default((0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`)
        .notNull(),
}, (table) => ({
    applicationIdIdx: (0, pg_core_1.index)('application_guardrails_application_id_idx').on(table.applicationId),
    applicationIdUnique: (0, pg_core_1.uniqueIndex)('application_guardrails_application_id_unique').on(table.applicationId),
}));
// Relations for application guardrails
exports.applicationGuardrailsRelations = (0, drizzle_orm_1.relations)(exports.applicationGuardrailsTable, ({ one }) => ({
    application: one(exports.applicationsTable, {
        fields: [exports.applicationGuardrailsTable.applicationId],
        references: [exports.applicationsTable.id],
    }),
}));
//# sourceMappingURL=tables.js.map