import { pgTable, serial, text, timestamp, doublePrecision, integer, boolean, jsonb, uniqueIndex, uuid } from 'drizzle-orm/pg-core';

export const users = pgTable('users', {
  id: uuid('id').primaryKey().defaultRandom(),
  email: text('email').notNull().unique(),
  password_hash: text('password_hash'), // null for OAuth-only users
  name: text('name'),
  image: text('image'),
  provider: text('provider').notNull().default('credentials'), // 'credentials' | 'google'
  provider_account_id: text('provider_account_id'), // Google sub ID
  email_verified: timestamp('email_verified'), // nullable — set when email is verified
  created_at: timestamp('created_at').notNull().defaultNow(),
  updated_at: timestamp('updated_at').notNull().defaultNow(),
});

export const charts = pgTable('charts', {
  id: serial('id').primaryKey(),
  user_id: uuid('user_id').references(() => users.id), // nullable for now; will be NOT NULL after data migration (task 2.5)
  name: text('name').notNull(),
  created_at: timestamp('created_at').notNull().defaultNow(),
}, (table) => [
  uniqueIndex('charts_user_id_name_unique').on(table.user_id, table.name),
]);

export const candles = pgTable('candles', {
  id: serial('id').primaryKey(),
  chart_id: integer('chart_id').notNull().references(() => charts.id),
  time: timestamp('time').notNull(),
  open: doublePrecision('open').notNull(),
  high: doublePrecision('high').notNull(),
  low: doublePrecision('low').notNull(),
  close: doublePrecision('close').notNull(),
}, (table) => [
  uniqueIndex('candles_chart_time_unique').on(table.chart_id, table.time),
]);

export const annotationTypes = pgTable('annotation_types', {
  id: serial('id').primaryKey(),
  user_id: uuid('user_id').references(() => users.id), // nullable for now; will be NOT NULL after data migration (task 2.5)
  name: text('name').notNull(), // internal name (e.g., 'break_up')
  display_name: text('display_name').notNull(), // display name (e.g., 'Break Up')
  color: text('color').notNull(), // hex color code
  category: text('category').notNull(), // 'marker' or 'line'
  icon: text('icon'), // icon name or symbol
  is_active: boolean('is_active').notNull().default(true), // true = active, false = inactive
  created_at: timestamp('created_at').notNull().defaultNow(),
}, (table) => [
  uniqueIndex('annotation_types_user_id_name_unique').on(table.user_id, table.name),
]);

export const annotations = pgTable('annotations', {
  id: serial('id').primaryKey(),
  user_id: uuid('user_id').references(() => users.id), // nullable for now; will be NOT NULL after data migration (task 2.5)
  chart_id: integer('chart_id').notNull().references(() => charts.id),
  timestamp: timestamp('timestamp').notNull(),
  label_type: text('label_type').notNull(),
  geometry: jsonb('geometry'), // JSON for line coordinates
  color: text('color').default('#3b82f6'), // hex color code
  created_at: timestamp('created_at').notNull().defaultNow(),
});

export const spanLabelTypes = pgTable('span_label_types', {
  id: serial('id').primaryKey(),
  user_id: uuid('user_id').references(() => users.id), // nullable for now; will be NOT NULL after data migration (task 2.5)
  name: text('name').notNull(), // internal name (e.g., 'bull_flag')
  display_name: text('display_name').notNull(), // UI label (e.g., 'Bull Flag')
  color: text('color').notNull(), // hex color for rectangle fill
  hotkey: text('hotkey'), // keyboard shortcut (e.g., '1')
  is_active: boolean('is_active').notNull().default(true), // true = active, false = inactive
  sort_order: integer('sort_order').notNull().default(0), // display order
  created_at: timestamp('created_at').notNull().defaultNow(),
}, (table) => [
  uniqueIndex('span_label_types_user_id_name_unique').on(table.user_id, table.name),
]);

export const spanAnnotations = pgTable('span_annotations', {
  id: serial('id').primaryKey(),
  user_id: uuid('user_id').references(() => users.id), // nullable for now; will be NOT NULL after data migration (task 2.5)
  chart_id: integer('chart_id').notNull().references(() => charts.id),
  start_time: timestamp('start_time').notNull(), // timestamp of first candle
  end_time: timestamp('end_time').notNull(), // timestamp of last candle
  label: text('label').notNull(), // pattern name referencing span_label_types.name
  confidence: integer('confidence'), // 1-5 scale, nullable
  outcome: text('outcome'), // 'win'|'loss'|'breakeven'|null
  notes: text('notes'), // free-text, nullable
  sub_spans: jsonb('sub_spans'), // JSON array of sub-span objects, nullable
  color: text('color').notNull().default('#2196F3'), // hex color
  source: text('source').notNull().default('human'), // 'human'|'model'|'human_correction'
  model_prediction: jsonb('model_prediction'), // JSON metadata when confirming/correcting predictions
  created_at: timestamp('created_at').notNull().defaultNow(),
});
