"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.spansRelations = exports.tracesRelations = exports.spans = exports.traces = exports.logMetadataRelations = exports.metadataRelations = exports.logMetadata = exports.metadata = exports.logsRelations = exports.logs = exports.projectsRelations = exports.projects = exports.config = void 0;
const sqlite_core_1 = require("drizzle-orm/sqlite-core");
const drizzle_orm_1 = require("drizzle-orm");
// Minimal config table for storing key-value pairs
exports.config = (0, sqlite_core_1.sqliteTable)('config', {
    key: (0, sqlite_core_1.text)('key').primaryKey(),
    value: (0, sqlite_core_1.text)('value').notNull(),
});
exports.projects = (0, sqlite_core_1.sqliteTable)('projects', {
    id: (0, sqlite_core_1.text)('id').primaryKey(),
    name: (0, sqlite_core_1.text)('name').notNull().unique(),
    description: (0, sqlite_core_1.text)('description').notNull(),
    createdAt: (0, sqlite_core_1.text)('created_at').notNull(), // ISO string
    config: (0, sqlite_core_1.text)('config').notNull().default('{}'), // JSON string for project-specific configuration
});
// Define relations for projects
exports.projectsRelations = (0, drizzle_orm_1.relations)(exports.projects, ({ many }) => ({
    logs: many(exports.logs),
    traces: many(exports.traces),
}));
exports.logs = (0, sqlite_core_1.sqliteTable)('logs', {
    id: (0, sqlite_core_1.text)('id').primaryKey(),
    projectId: (0, sqlite_core_1.text)('project_id').notNull().references(() => exports.projects.id, { onDelete: 'cascade' }),
    level: (0, sqlite_core_1.text)('level').notNull(),
    message: (0, sqlite_core_1.text)('message').notNull(),
    timestamp: (0, sqlite_core_1.text)('timestamp').notNull(),
    embeddedMetadata: (0, sqlite_core_1.text)('embedded_metadata').notNull().default('{}'), // JSON string for non-tracked metadata
});
// Define relations for logs
exports.logsRelations = (0, drizzle_orm_1.relations)(exports.logs, ({ one, many }) => ({
    project: one(exports.projects, {
        fields: [exports.logs.projectId],
        references: [exports.projects.id],
    }),
    logMetadata: many(exports.logMetadata),
}));
// Table for metadata definitions (key-value pairs that can be reused across logs)
exports.metadata = (0, sqlite_core_1.sqliteTable)('metadata', {
    id: (0, sqlite_core_1.text)('id').primaryKey(),
    projectId: (0, sqlite_core_1.text)('project_id').notNull().references(() => exports.projects.id, { onDelete: 'cascade' }),
    key: (0, sqlite_core_1.text)('key').notNull(),
    value: (0, sqlite_core_1.text)('value').notNull(),
}, (table) => ([
    (0, sqlite_core_1.index)('metadata_key_idx').on(table.key),
    (0, sqlite_core_1.index)('metadata_value_idx').on(table.value),
    (0, sqlite_core_1.index)('metadata_project_id_idx').on(table.projectId),
    (0, sqlite_core_1.unique)('metadata_key_value_project_unique').on(table.key, table.value, table.projectId),
]));
// Bridge table for log-metadata associations (many-to-many relationship)
exports.logMetadata = (0, sqlite_core_1.sqliteTable)('log_metadata', {
    id: (0, sqlite_core_1.text)('id').primaryKey(),
    logId: (0, sqlite_core_1.text)('log_id').notNull().references(() => exports.logs.id, { onDelete: 'cascade' }),
    metadataId: (0, sqlite_core_1.text)('metadata_id').notNull().references(() => exports.metadata.id, { onDelete: 'cascade' }),
});
// Define relations for metadata
exports.metadataRelations = (0, drizzle_orm_1.relations)(exports.metadata, ({ one, many }) => ({
    project: one(exports.projects, {
        fields: [exports.metadata.projectId],
        references: [exports.projects.id],
    }),
    logMetadata: many(exports.logMetadata),
}));
// Define relations for logMetadata bridge table
exports.logMetadataRelations = (0, drizzle_orm_1.relations)(exports.logMetadata, ({ one }) => ({
    log: one(exports.logs, {
        fields: [exports.logMetadata.logId],
        references: [exports.logs.id],
    }),
    metadata: one(exports.metadata, {
        fields: [exports.logMetadata.metadataId],
        references: [exports.metadata.id],
    }),
}));
// Traces table for storing trace information
exports.traces = (0, sqlite_core_1.sqliteTable)('traces', {
    id: (0, sqlite_core_1.text)('id').primaryKey(),
    projectId: (0, sqlite_core_1.text)('project_id').notNull().references(() => exports.projects.id, { onDelete: 'cascade' }),
    name: (0, sqlite_core_1.text)('name').notNull(),
    startTime: (0, sqlite_core_1.text)('start_time').notNull(), // ISO string
    endTime: (0, sqlite_core_1.text)('end_time'), // ISO string, nullable for ongoing traces
    duration: (0, sqlite_core_1.text)('duration'), // Duration in milliseconds, nullable for ongoing traces
    status: (0, sqlite_core_1.text)('status').notNull().default('running'), // 'running', 'completed', 'failed'
    metadata: (0, sqlite_core_1.text)('metadata').notNull().default('{}'), // JSON string for trace metadata
    createdAt: (0, sqlite_core_1.text)('created_at').notNull(), // ISO string
}, (table) => ([
    (0, sqlite_core_1.index)('traces_project_id_idx').on(table.projectId),
    (0, sqlite_core_1.index)('traces_status_idx').on(table.status),
    (0, sqlite_core_1.index)('traces_start_time_idx').on(table.startTime),
]));
// Spans table for storing span information within traces
exports.spans = (0, sqlite_core_1.sqliteTable)('spans', {
    id: (0, sqlite_core_1.text)('id').primaryKey(),
    traceId: (0, sqlite_core_1.text)('trace_id').notNull().references(() => exports.traces.id, { onDelete: 'cascade' }),
    parentSpanId: (0, sqlite_core_1.text)('parent_span_id'), // Self-reference for nested spans
    name: (0, sqlite_core_1.text)('name').notNull(),
    startTime: (0, sqlite_core_1.text)('start_time').notNull(), // ISO string
    endTime: (0, sqlite_core_1.text)('end_time'), // ISO string, nullable for ongoing spans
    status: (0, sqlite_core_1.text)('status').notNull().default('running'), // 'running', 'completed', 'failed'
    metadata: (0, sqlite_core_1.text)('metadata').notNull().default('{}'), // JSON string for span metadata
    error: (0, sqlite_core_1.text)('error'), // Error message/details when status is 'failed', nullable
    createdAt: (0, sqlite_core_1.text)('created_at').notNull(), // ISO string
}, (table) => [
    (0, sqlite_core_1.index)('spans_trace_id_idx').on(table.traceId),
    (0, sqlite_core_1.index)('spans_parent_span_id_idx').on(table.parentSpanId),
    (0, sqlite_core_1.index)('spans_status_idx').on(table.status),
    (0, sqlite_core_1.index)('spans_start_time_idx').on(table.startTime)
]);
// Define relations for traces
exports.tracesRelations = (0, drizzle_orm_1.relations)(exports.traces, ({ one, many }) => ({
    project: one(exports.projects, {
        fields: [exports.traces.projectId],
        references: [exports.projects.id],
    }),
    spans: many(exports.spans),
}));
// Define relations for spans
exports.spansRelations = (0, drizzle_orm_1.relations)(exports.spans, ({ one, many }) => ({
    trace: one(exports.traces, {
        fields: [exports.spans.traceId],
        references: [exports.traces.id],
    }),
    parentSpan: one(exports.spans, {
        fields: [exports.spans.parentSpanId],
        references: [exports.spans.id],
        relationName: "parentChild"
    }),
    childSpans: many(exports.spans, {
        relationName: "parentChild"
    }),
}));
//# sourceMappingURL=schema.js.map