import { pgTable, text, timestamp, index, uuid, boolean, jsonb, doublePrecision, integer } from "drizzle-orm/pg-core";
import { users } from "./auth";
import { detectionVerdict, analysisType, analysisJobStatus } from "./enums";

export const voiceEnrollments = pgTable("voice_enrollments", {
  id: uuid("id").defaultRandom().primaryKey(),
  userId: uuid("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  name: text("name").notNull(),
  voiceHash: text("voice_hash").notNull(),
  audioMetadata: jsonb("audio_metadata"),
  isActive: boolean("is_active").default(true).notNull(),
  createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }).defaultNow().notNull(),
  updatedAt: timestamp("updated_at", { withTimezone: true, mode: "date" }).defaultNow().notNull().$onUpdate(() => new Date()),
}, (table) => ({
  userIdIdx: index("voice_enrollments_user_id_idx").on(table.userId),
  voiceHashIdx: index("voice_enrollments_voice_hash_idx").on(table.voiceHash),
}));

export const voiceAnalyses = pgTable("voice_analyses", {
  id: uuid("id").defaultRandom().primaryKey(),
  enrollmentId: uuid("enrollment_id").references(() => voiceEnrollments.id),
  userId: uuid("user_id").notNull().references(() => users.id),
  audioHash: text("audio_hash").notNull(),
  isSynthetic: boolean("is_synthetic").notNull(),
  confidence: doublePrecision("confidence").notNull(),
  analysisResult: jsonb("analysis_result").notNull(),
  audioUrl: text("audio_url").notNull(),
  createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }).defaultNow().notNull(),
}, (table) => ({
  userIdIdx: index("voice_analyses_user_id_idx").on(table.userId),
  enrollmentIdIdx: index("voice_analyses_enrollment_id_idx").on(table.enrollmentId),
  audioHashIdx: index("voice_analyses_audio_hash_idx").on(table.audioHash),
}));

export const analysisJobs = pgTable("analysis_jobs", {
  id: uuid("id").defaultRandom().primaryKey(),
  userId: uuid("user_id").notNull().references(() => users.id),
  analysisType: analysisType("analysis_type").notNull(),
  audioFilePath: text("audio_file_path").notNull(),
  status: analysisJobStatus("status").notNull(),
  errorMessage: text("error_message"),
  completedAt: timestamp("completed_at", { withTimezone: true, mode: "date" }),
  createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }).defaultNow().notNull(),
}, (table) => ({
  userIdIdx: index("analysis_jobs_user_id_idx").on(table.userId),
  statusIdx: index("analysis_jobs_status_idx").on(table.status),
  createdAtIdx: index("analysis_jobs_created_at_idx").on(table.createdAt),
}));

export const analysisResults = pgTable("analysis_results", {
  id: uuid("id").defaultRandom().primaryKey(),
  analysisJobId: uuid("analysis_job_id").notNull().unique().references(() => analysisJobs.id),
  syntheticScore: doublePrecision("synthetic_score").notNull(),
  verdict: detectionVerdict("verdict").notNull(),
  confidence: doublePrecision("confidence").notNull(),
  processingTimeMs: integer("processing_time_ms").notNull(),
  matchedEnrollmentId: text("matched_enrollment_id"),
  matchedSimilarity: doublePrecision("matched_similarity"),
  modelVersion: text("model_version"),
  createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }).defaultNow().notNull(),
}, (table) => ({
  analysisJobIdIdx: index("analysis_results_analysis_job_id_idx").on(table.analysisJobId),
  syntheticScoreIdx: index("analysis_results_synthetic_score_idx").on(table.syntheticScore),
  verdictIdx: index("analysis_results_verdict_idx").on(table.verdict),
}));
