import { pgTable, text, timestamp, boolean, uuid } from 'drizzle-orm/pg-core';

export const tenants = pgTable('tenants', {
  id: uuid('id').defaultRandom().primaryKey(),
  name: text('name').notNull(),
  isActive: boolean('is_active').default(true),
  packageId: uuid('package_id').references(() => saasPackages.id),
  licenses: text('licenses').default('{}').notNull(),
  licenseNumber: text('license_number').unique(),
  allowCrossGroup: boolean('allow_cross_group').default(false).notNull(),
  brandColor: text('brand_color').default('#0b5cff'), // Zoom Blue
  platformName: text('platform_name').default('JUMPA.ID'),
  mediaEngineStrategy: text('media_engine_strategy').default('XCU_GLOBAL_MESH').notNull(),
  chatEngineStrategy: text('chat_engine_strategy').default('XTM_GLOBAL_MESH').notNull(),
  securityTier: text('security_tier').default('STANDARD').notNull(), // STANDARD | SOVEREIGN
  byokEnabled: boolean('byok_enabled').default(false).notNull(),
  byokKey: text('byok_key'),
  createdAt: timestamp('created_at').defaultNow().notNull(),
});

export const users = pgTable('users', {
  id: uuid('id').defaultRandom().primaryKey(),
  tenantId: uuid('tenant_id').references(() => tenants.id).notNull(),
  email: text('email').notNull().unique(),
  passwordHash: text('password_hash').notNull(),
  role: text('role').default('user').notNull(),
  licenses: text('licenses'), // User-specific override
  byokEnabled: boolean('byok_enabled').default(false).notNull(),
  byokKey: text('byok_key'),
  createdAt: timestamp('created_at').defaultNow().notNull(),
});

export const messages = pgTable('messages', {
  id: uuid('id').defaultRandom().primaryKey(),
  room: text('room').notNull(),
  sender: text('sender').notNull(),
  content: text('content').notNull(),
  status: text('status').default('sent').notNull(),
  isEdited: boolean('is_edited').default(false).notNull(),
  createdAt: timestamp('created_at').defaultNow().notNull(),
});

export const chatStatuses = pgTable('chat_statuses', {
  id: uuid('id').defaultRandom().primaryKey(),
  tenantId: uuid('tenant_id').references(() => tenants.id),
  sender: text('sender').notNull(),
  content: text('content').notNull(),
  visibility: text('visibility').default('tenant').notNull(), // 'global', 'tenant'
  expiresAt: timestamp('expires_at').notNull(),
  createdAt: timestamp('created_at').defaultNow().notNull(),
});

export const chatBroadcasts = pgTable('chat_broadcasts', {
  id: uuid('id').defaultRandom().primaryKey(),
  tenantId: uuid('tenant_id').references(() => tenants.id),
  senderName: text('sender_name').notNull(), // Usually the user's name or JUMPA Bot
  content: text('content').notNull(),
  targets: text('targets').notNull(), // JSON string of targets (rooms/users)
  scheduledAt: timestamp('scheduled_at').notNull(),
  isSent: boolean('is_sent').default(false).notNull(),
  createdAt: timestamp('created_at').defaultNow().notNull(),
});

export const guestInvites = pgTable('guest_invites', {
  id: uuid('id').defaultRandom().primaryKey(),
  room: text('room').notNull(),
  hostId: text('host_id').notNull(),
  pin: text('pin').notNull(),
  isUsed: boolean('is_used').default(false).notNull(),
  createdAt: timestamp('created_at').defaultNow().notNull(),
});

export const saasPackages = pgTable('saas_packages', {
  id: uuid('id').defaultRandom().primaryKey(),
  name: text('name').notNull(),
  price: text('price').notNull(),
  features: text('features').default('[]').notNull(),
  isHidden: boolean('is_hidden').default(false).notNull(),
  isActive: boolean('is_active').default(true).notNull(),
  createdAt: timestamp('created_at').defaultNow().notNull(),
});

export const quantumLogs = pgTable('quantum_logs', {
  id: uuid('id').defaultRandom().primaryKey(),
  tenantId: uuid('tenant_id').references(() => tenants.id), // Added for PANOPTICON isolation
  actor: text('actor').notNull(),
  action: text('action').notNull(),
  targetId: text('target_id').notNull(),
  ipAddress: text('ip_address'),
  userAgent: text('user_agent'),
  nanoTimestamp: timestamp('nano_timestamp').defaultNow().notNull(),
});

// PANOPTICON: Ultra-detailed telemetry table for XCU QUIC MoQ
export const networkTelemetry = pgTable('network_telemetry', {
  id: uuid('id').defaultRandom().primaryKey(),
  tenantId: uuid('tenant_id').references(() => tenants.id),
  userId: uuid('user_id').references(() => users.id),
  endpoint: text('endpoint').notNull(),
  method: text('method').notNull(),
  responseTimeMs: text('response_time_ms').notNull(), // using text to avoid integer limits for nano precision
  statusCode: text('status_code').notNull(),
  ipAddress: text('ip_address').notNull(),
  geoRegion: text('geo_region'), 
  trafficBytes: text('traffic_bytes').notNull(),
  timestamp: timestamp('timestamp').defaultNow().notNull(),
});

// PANOPTICON: Live Kill Switch Registry
export const liveKillSwitches = pgTable('live_kill_switches', {
  id: uuid('id').defaultRandom().primaryKey(),
  tenantId: uuid('tenant_id').references(() => tenants.id),
  targetType: text('target_type').notNull(), // 'USER', 'TENANT', 'ROOM'
  targetId: text('target_id').notNull(),
  reason: text('reason'),
  issuedBy: text('issued_by').notNull(), // actor email or ID
  expiresAt: timestamp('expires_at'), // null means permanent block
  createdAt: timestamp('created_at').defaultNow().notNull(),
});

export const systemFeatures = pgTable('system_features', {
  id: uuid('id').defaultRandom().primaryKey(),
  module: text('module').default('IAM').notNull(),
  key: text('key').notNull().unique(),
  name: text('name').notNull(),
  description: text('description'),
  defaultState: text('default_state').default('UPSELL').notNull(), // UPSELL, HIDDEN, GRANTED
  createdAt: timestamp('created_at').defaultNow().notNull(),
});
