import { pgTable, uuid, text, integer, timestamp, jsonb, doublePrecision, } from 'drizzle-orm/pg-core'; import type { CourseStatus, Depth, Difficulty, JobStatus, Question, SegmentStatus, SourceStatus, SourceType, Stage, VisualCue, } from '../../lib/types'; export const courses = pgTable('courses', { id: uuid('id').defaultRandom().primaryKey(), title: text('title').notNull().default('Untitled course'), status: text('status').$type().notNull().default('draft'), depth: text('depth').$type().notNull().default('standard'), difficulty: text('difficulty').$type().notNull().default('beginner'), createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(), }); export const sourceInputs = pgTable('source_inputs', { id: uuid('id').defaultRandom().primaryKey(), courseId: uuid('course_id') .notNull() .references(() => courses.id, { onDelete: 'cascade' }), type: text('type').$type().notNull(), originalRef: text('original_ref'), // filename or URL; null for pasted text geminiFileId: text('gemini_file_id'), geminiFileUri: text('gemini_file_uri'), mimeType: text('mime_type'), extractedText: text('extracted_text'), status: text('status').$type().notNull().default('pending'), error: text('error'), createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), }); export const modules = pgTable('modules', { id: uuid('id').defaultRandom().primaryKey(), courseId: uuid('course_id') .notNull() .references(() => courses.id, { onDelete: 'cascade' }), position: integer('position').notNull(), title: text('title').notNull(), summary: text('summary'), }); // One segment === one ~1-minute AI-generated video. export const segments = pgTable('segments', { id: uuid('id').defaultRandom().primaryKey(), moduleId: uuid('module_id') .notNull() .references(() => modules.id, { onDelete: 'cascade' }), position: integer('position').notNull(), title: text('title').notNull(), objective: text('objective'), script: text('script'), visualCues: jsonb('visual_cues').$type(), audioPath: text('audio_path'), videoPath: text('video_path'), thumbnailPath: text('thumbnail_path'), durationSec: doublePrecision('duration_sec'), status: text('status').$type().notNull().default('pending'), error: text('error'), }); export const assessments = pgTable('assessments', { id: uuid('id').defaultRandom().primaryKey(), courseId: uuid('course_id') .notNull() .references(() => courses.id, { onDelete: 'cascade' }), scope: text('scope').$type<'module' | 'course'>().notNull(), refId: uuid('ref_id'), // module id when scope === 'module' type: text('type').$type<'mcq' | 'short_answer' | 'mixed'>().notNull(), difficulty: text('difficulty').$type().notNull(), questions: jsonb('questions').$type().notNull(), createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), }); export const generationJobs = pgTable('generation_jobs', { id: uuid('id').defaultRandom().primaryKey(), courseId: uuid('course_id') .notNull() .references(() => courses.id, { onDelete: 'cascade' }), status: text('status').$type().notNull().default('queued'), currentStage: text('current_stage').$type(), error: text('error'), startedAt: timestamp('started_at', { withTimezone: true }), finishedAt: timestamp('finished_at', { withTimezone: true }), createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), }); export const jobSteps = pgTable('job_steps', { id: uuid('id').defaultRandom().primaryKey(), jobId: uuid('job_id') .notNull() .references(() => generationJobs.id, { onDelete: 'cascade' }), stage: text('stage').$type().notNull(), status: text('status').notNull().default('pending'), startedAt: timestamp('started_at', { withTimezone: true }), finishedAt: timestamp('finished_at', { withTimezone: true }), durationMs: integer('duration_ms'), error: text('error'), }); // One row per LLM / TTS API call. The Gemini client writes here centrally so // token + cost accounting is never scattered across call sites. export const llmUsage = pgTable('llm_usage', { id: uuid('id').defaultRandom().primaryKey(), jobId: uuid('job_id').references(() => generationJobs.id, { onDelete: 'cascade' }), stepId: uuid('step_id').references(() => jobSteps.id, { onDelete: 'set null' }), courseId: uuid('course_id'), sourceInputId: uuid('source_input_id'), provider: text('provider').notNull().default('google'), model: text('model').notNull(), operation: text('operation').notNull(), promptTokens: integer('prompt_tokens').notNull().default(0), completionTokens: integer('completion_tokens').notNull().default(0), totalTokens: integer('total_tokens').notNull().default(0), estCostUsd: doublePrecision('est_cost_usd').notNull().default(0), createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), });