import type { Generated, Insertable, Selectable, Updateable } from "kysely";

/**
 * Database schema type definitions for Kysely.
 * These types provide compile-time SQL validation.
 */

// Device configuration table
export interface DeviceConfigTable {
  id: Generated<number>;
  device_id: string;
  platform: "android" | "ios";
  active_mode: string | null;
  config_json: string; // JSON blob for flexible config storage
  created_at: Generated<string>;
  updated_at: string;
}

// Installed apps cache table
interface InstalledAppsTable {
  device_id: string;
  user_id: number;
  package_name: string;
  is_system: number; // SQLite boolean (0/1)
  installed_at: number;
  last_verified_at: number;
  daemon_session_id: string | null;
  device_session_start: number | null;
}

// Performance thresholds table
export interface PerformanceThresholdsTable {
  id: Generated<number>;
  device_id: string;
  session_id: string;
  refresh_rate: number;
  frame_time_threshold_ms: number;
  p50_threshold_ms: number;
  p90_threshold_ms: number;
  p95_threshold_ms: number;
  p99_threshold_ms: number;
  jank_count_threshold: number;
  cpu_usage_threshold_percent: number;
  touch_latency_threshold_ms: number;
  weight: number;
  created_at: Generated<string>;
  ttl_hours: number;
}

// Performance audit results table
export interface PerformanceAuditResultsTable {
  id: Generated<number>;
  device_id: string;
  session_id: string;
  package_name: string;
  timestamp: string;
  passed: number;
  p50_ms: number | null;
  p90_ms: number | null;
  p95_ms: number | null;
  p99_ms: number | null;
  jank_count: number | null;
  missed_vsync_count: number | null;
  slow_ui_thread_count: number | null;
  frame_deadline_missed_count: number | null;
  cpu_usage_percent: number | null;
  touch_latency_ms: number | null;
  diagnostics_json: string | null;
  // Live metrics extension
  time_to_first_frame_ms: number | null;
  time_to_interactive_ms: number | null;
  frame_rate_fps: number | null;
  node_id: number | null; // FK to navigation_nodes.id
  created_at: Generated<string>;
}

// Navigation graph tables
export interface NavigationAppsTable {
  app_id: string;
  created_at: Generated<string>;
  updated_at: string;
}

export interface NavigationNodesTable {
  id: Generated<number>;
  app_id: string;
  screen_name: string;
  first_seen_at: number;
  last_seen_at: number;
  visit_count: number;
  back_stack_depth: number | null;
  task_id: number | null;
  screenshot_path: string | null;
  created_at: Generated<string>;
}

export interface NavigationEdgesTable {
  id: Generated<number>;
  app_id: string;
  from_screen: string;
  to_screen: string;
  tool_name: string | null;
  tool_args: string | null; // JSON blob
  timestamp: number;
  created_at: Generated<string>;
}

export interface UIElementsTable {
  id: Generated<number>;
  app_id: string;
  text: string | null;
  resource_id: string | null;
  content_description: string | null;
  class_name: string | null;
  bounds_left: number | null;
  bounds_top: number | null;
  bounds_right: number | null;
  bounds_bottom: number | null;
  clickable: number | null; // SQLite boolean (0/1)
  scrollable: number | null; // SQLite boolean (0/1)
  first_seen_at: number;
  last_seen_at: number;
  created_at: Generated<string>;
}

export interface EdgeUIElementsTable {
  edge_id: number;
  ui_element_id: number;
  selection_order: number;
}

export interface NodeModalsTable {
  node_id: number;
  modal_identifier: string;
  stack_level: number;
  created_at: Generated<string>;
}

export interface EdgeModalsTable {
  edge_id: number;
  position: string; // 'from' | 'to'
  modal_identifier: string;
  stack_level: number;
  created_at: Generated<string>;
}

export interface ScrollPositionsTable {
  edge_id: number;
  target_element_id: number;
  container_element_id: number | null;
  direction: string;
  speed: string | null;
  swipe_count: number | null;
  created_at: Generated<string>;
}

// Navigation node fingerprints table - tracks fingerprints associated with named nodes
interface NavigationNodeFingerprintsTable {
  id: Generated<number>;
  app_id: string; // Scoped per app to prevent cross-app collisions
  node_id: number;
  fingerprint_hash: string; // Unique per app (not globally)
  fingerprint_data: string; // Full fingerprint JSON
  first_seen_at: number;
  last_seen_at: number;
  occurrence_count: number;
  created_at: Generated<string>;
}

// Navigation suggestions table - uncorrelated fingerprints waiting for promotion
interface NavigationSuggestionsTable {
  id: Generated<number>;
  app_id: string;
  fingerprint_hash: string;
  fingerprint_data: string;
  first_seen_at: number;
  last_seen_at: number;
  occurrence_count: number;
  promoted_to_fingerprint_id: number | null;
  created_at: Generated<string>;
}

// Prediction accuracy tables
export interface PredictionOutcomesTable {
  id: Generated<number>;
  app_id: string;
  prediction_id: string;
  timestamp: number;
  from_screen: string;
  predicted_screen: string;
  actual_screen: string;
  tool_name: string;
  tool_args: string;
  predicted_elements: string | null;
  found_elements: string | null;
  confidence: number;
  match_score: number;
  correct: number;
  partial_match: number;
  error_type: string | null;
  created_at: Generated<string>;
}

export interface PredictionTransitionStatsTable {
  id: Generated<number>;
  app_id: string;
  from_screen: string;
  to_screen: string;
  tool_name: string;
  tool_args: string;
  attempts: number;
  successes: number;
  total_confidence: number;
  brier_score_sum: number;
  updated_at: string;
  created_at: Generated<string>;
}

// Tool call tracking table
export interface ToolCallsTable {
  id: Generated<number>;
  tool_name: string;
  timestamp: string;
  session_uuid: string | null;
  status: string | null; // success, failure
  error_message: string | null;
  error_type: string | null;
  device_id: string | null;
  package_name: string | null;
  duration_ms: number | null;
  tool_args: string | null; // JSON blob
  created_at: Generated<string>;
}

// Crash tracking table
interface CrashesTable {
  id: Generated<number>;
  device_id: string;
  package_name: string;
  crash_type: "java" | "native" | "system";
  timestamp: number;
  process_name: string | null;
  pid: number | null;
  exception_class: string | null;
  exception_message: string | null;
  stacktrace: string | null;
  signal: string | null; // For native crashes
  fault_address: string | null; // For native crashes
  tombstone_path: string | null;
  detection_source: "sdk_websocket" | "logcat" | "tombstone" | "dropbox" | "accessibility" | "process_monitor";
  raw_log: string | null;
  navigation_node_id: number | null;
  test_execution_id: number | null;
  session_uuid: string | null;
  created_at: Generated<string>;
}

// ANR tracking table
interface AnrsTable {
  id: Generated<number>;
  device_id: string;
  package_name: string;
  timestamp: number;
  process_name: string | null;
  pid: number | null;
  reason: string | null;
  activity: string | null;
  wait_duration_ms: number | null;
  cpu_usage: string | null;
  main_thread_state: string | null;
  stacktrace: string | null;
  detection_source: "sdk_websocket" | "logcat" | "dropbox" | "accessibility";
  raw_log: string | null;
  navigation_node_id: number | null;
  test_execution_id: number | null;
  session_uuid: string | null;
  created_at: Generated<string>;
}

// Accessibility baseline tables
interface AccessibilityBaselinesTable {
  id: Generated<number>;
  screen_id: string;
  violations_json: string; // JSON blob of WcagViolation[]
  created_at: Generated<string>;
  updated_at: string;
}

// Memory audit tables
interface MemoryThresholdsTable {
  id: Generated<number>;
  device_id: string;
  package_name: string;
  heap_growth_threshold_mb: number;
  native_heap_growth_threshold_mb: number;
  gc_count_threshold: number;
  gc_duration_threshold_ms: number;
  unreachable_objects_threshold: number;
  weight: number;
  created_at: Generated<string>;
  ttl_hours: number;
}

interface MemoryBaselinesTable {
  id: Generated<number>;
  device_id: string;
  package_name: string;
  tool_name: string;
  java_heap_baseline_mb: number;
  native_heap_baseline_mb: number;
  gc_count_baseline: number;
  gc_duration_baseline_ms: number;
  unreachable_objects_baseline: number;
  sample_count: number;
  last_updated: string;
  created_at: Generated<string>;
}

interface MemoryAuditResultsTable {
  id: Generated<number>;
  device_id: string;
  session_id: string;
  package_name: string;
  tool_name: string;
  tool_args: string | null;
  timestamp: string;
  passed: number;
  pre_java_heap_mb: number | null;
  pre_native_heap_mb: number | null;
  pre_total_pss_mb: number | null;
  post_java_heap_mb: number | null;
  post_native_heap_mb: number | null;
  post_total_pss_mb: number | null;
  java_heap_growth_mb: number | null;
  native_heap_growth_mb: number | null;
  total_pss_growth_mb: number | null;
  gc_count: number | null;
  gc_total_duration_ms: number | null;
  unreachable_objects_count: number | null;
  violations_json: string | null;
  diagnostics_json: string | null;
  created_at: Generated<string>;
}

// Recomposition metrics tables
interface RecompositionMetricsTable {
  id: Generated<number>;
  device_id: string;
  session_id: string;
  package_name: string;
  composable_id: string;
  composable_name: string | null;
  resource_id: string | null;
  test_tag: string | null;
  total_count: number;
  skip_count: number;
  rolling_1s_avg: number | null;
  duration_ms: number | null;
  likely_cause: string | null;
  parent_chain_json: string | null;
  stable_annotated: number | null;
  remembered_count: number | null;
  timestamp: string;
  created_at: Generated<string>;
}

// Test execution timing table
interface TestExecutionsTable {
  id: Generated<number>;
  test_class: string;
  test_method: string;
  duration_ms: number;
  status: "passed" | "failed" | "skipped";
  timestamp: number;
  device_id: string | null;
  device_name: string | null;
  device_platform: "android" | "ios" | null;
  device_type: "emulator" | "simulator" | "device" | null;
  app_version: string | null;
  git_commit: string | null;
  target_sdk: number | null;
  jdk_version: string | null;
  jvm_target: string | null;
  gradle_version: string | null;
  is_ci: number | null;
  session_uuid: string | null;
  error_message: string | null;
  video_path: string | null;
  snapshot_path: string | null;
  created_at: Generated<string>;
}

// Test execution step details
interface TestExecutionStepsTable {
  id: Generated<number>;
  execution_id: number;
  step_index: number;
  action: string;
  target: string | null;
  status: "completed" | "failed" | "skipped";
  duration_ms: number;
  screen_name: string | null;
  screenshot_path: string | null;
  error_message: string | null;
  details_json: string | null;
  created_at: Generated<string>;
}

// Screens visited during test execution
interface TestExecutionScreensTable {
  id: Generated<number>;
  execution_id: number;
  screen_name: string;
  visit_order: number;
  timestamp: number;
  created_at: Generated<string>;
}

// Test coverage tables
interface TestCoverageSessionsTable {
  id: Generated<number>;
  session_uuid: string;
  app_id: string;
  start_time: number;
  end_time: number | null;
  total_nodes_visited: number;
  total_edges_traversed: number;
  created_at: Generated<string>;
}

interface TestNodeCoverageTable {
  id: Generated<number>;
  session_id: number;
  node_id: number;
  visit_count: number;
  first_visit_time: number;
  last_visit_time: number;
  created_at: Generated<string>;
}

interface TestEdgeCoverageTable {
  id: Generated<number>;
  session_id: number;
  edge_id: number;
  traversal_count: number;
  first_traversal_time: number;
  last_traversal_time: number;
  created_at: Generated<string>;
}

// Feature flags table
export interface FeatureFlagsTable {
  key: string;
  enabled: number; // SQLite boolean (0/1)
  config_json: string | null;
  created_at: Generated<string>;
  updated_at: string;
}

// Device snapshot tables
export interface DeviceSnapshotsTable {
  snapshot_name: string;
  device_id: string;
  device_name: string;
  platform: "android" | "ios";
  snapshot_type: string;
  include_app_data: number;
  include_settings: number;
  created_at: string;
  last_accessed_at: string;
  size_bytes: number;
  manifest_json: string;
}

export interface DeviceSnapshotConfigsTable {
  key: string;
  config_json: string;
  updated_at: string;
  created_at: Generated<string>;
}

// Video recording tables
export interface VideoRecordingsTable {
  recording_id: string;
  device_id: string;
  platform: "android" | "ios";
  status: string;
  output_name: string | null;
  file_name: string;
  file_path: string;
  format: string;
  size_bytes: number;
  duration_ms: number | null;
  codec: string | null;
  created_at: string;
  started_at: string;
  ended_at: string | null;
  last_accessed_at: string;
  config_json: string;
  highlights_json: string | null;
}

export interface VideoRecordingConfigsTable {
  key: string;
  config_json: string;
  updated_at: string;
  created_at: Generated<string>;
}

export interface AppearanceConfigsTable {
  key: string;
  config_json: string;
  updated_at: string;
  created_at: Generated<string>;
}

// Telemetry tables
export interface NetworkEventsTable {
  id: Generated<number>;
  device_id: string | null;
  timestamp: number;
  application_id: string | null;
  session_id: string | null;
  url: string;
  method: string;
  status_code: number;
  duration_ms: number;
  request_body_size: number | null;
  response_body_size: number | null;
  protocol: string | null;
  host: string | null;
  path: string | null;
  error: string | null;
  request_headers_json: string | null;
  response_headers_json: string | null;
  request_body: string | null;
  response_body: string | null;
  content_type: string | null;
  created_at: Generated<string>;
}

export interface LogEventsTable {
  id: Generated<number>;
  device_id: string | null;
  timestamp: number;
  application_id: string | null;
  session_id: string | null;
  level: number;
  tag: string;
  message: string;
  filter_name: string | null;
  created_at: Generated<string>;
}

export interface OsEventsTable {
  id: Generated<number>;
  device_id: string | null;
  timestamp: number;
  application_id: string | null;
  session_id: string | null;
  category: string;
  kind: string;
  details_json: string | null;
  created_at: Generated<string>;
}

export interface NavigationEventsTable {
  id: Generated<number>;
  device_id: string | null;
  timestamp: number;
  application_id: string | null;
  session_id: string | null;
  destination: string;
  source: string | null;
  arguments_json: string | null;
  metadata_json: string | null;
  created_at: Generated<string>;
}

export interface StorageEventsTable {
  id: Generated<number>;
  device_id: string | null;
  timestamp: number;
  application_id: string | null;
  session_id: string | null;
  file_name: string;
  key: string | null;
  value: string | null;
  value_type: string | null;
  change_type: string;
  previous_value: string | null;
  created_at: Generated<string>;
}

export interface LayoutEventsTable {
  id: Generated<number>;
  device_id: string | null;
  timestamp: number;
  application_id: string | null;
  session_id: string | null;
  sub_type: string;
  composable_name: string | null;
  composable_id: string | null;
  recomposition_count: number | null;
  duration_ms: number | null;
  likely_cause: string | null;
  details_json: string | null;
  screen_name: string | null;
  created_at: Generated<string>;
}

// Main database interface - add new tables here
export interface Database {
  device_configs: DeviceConfigTable;
  installed_apps: InstalledAppsTable;
  performance_thresholds: PerformanceThresholdsTable;
  performance_audit_results: PerformanceAuditResultsTable;
  navigation_apps: NavigationAppsTable;
  navigation_nodes: NavigationNodesTable;
  navigation_edges: NavigationEdgesTable;
  ui_elements: UIElementsTable;
  edge_ui_elements: EdgeUIElementsTable;
  node_modals: NodeModalsTable;
  edge_modals: EdgeModalsTable;
  scroll_positions: ScrollPositionsTable;
  prediction_outcomes: PredictionOutcomesTable;
  prediction_transition_stats: PredictionTransitionStatsTable;
  tool_calls: ToolCallsTable;
  accessibility_baselines: AccessibilityBaselinesTable;
  memory_thresholds: MemoryThresholdsTable;
  memory_baselines: MemoryBaselinesTable;
  memory_audit_results: MemoryAuditResultsTable;
  recomposition_metrics: RecompositionMetricsTable;
  test_executions: TestExecutionsTable;
  test_execution_steps: TestExecutionStepsTable;
  test_execution_screens: TestExecutionScreensTable;
  feature_flags: FeatureFlagsTable;
  device_snapshots: DeviceSnapshotsTable;
  device_snapshot_configs: DeviceSnapshotConfigsTable;
  video_recordings: VideoRecordingsTable;
  video_recording_configs: VideoRecordingConfigsTable;
  appearance_configs: AppearanceConfigsTable;
  test_coverage_sessions: TestCoverageSessionsTable;
  test_node_coverage: TestNodeCoverageTable;
  test_edge_coverage: TestEdgeCoverageTable;
  failure_groups: FailureGroupsTable;
  failure_occurrences: FailureOccurrencesTable;
  failure_occurrence_screens: FailureOccurrenceScreensTable;
  failure_captures: FailureCapturesTable;
  failure_notifications: FailureNotificationsTable;
  crashes: CrashesTable;
  anrs: AnrsTable;
  navigation_node_fingerprints: NavigationNodeFingerprintsTable;
  navigation_suggestions: NavigationSuggestionsTable;
  network_events: NetworkEventsTable;
  log_events: LogEventsTable;

  os_events: OsEventsTable;
  navigation_events: NavigationEventsTable;
  storage_events: StorageEventsTable;
  layout_events: LayoutEventsTable;
}

// Convenience types for each table
export type InstalledApp = Selectable<InstalledAppsTable>;
export type NewInstalledApp = Insertable<InstalledAppsTable>;

export type PerformanceThresholds = Selectable<PerformanceThresholdsTable>;
export type NewPerformanceThresholds = Insertable<PerformanceThresholdsTable>;

export type NewPerformanceAuditResult = Insertable<PerformanceAuditResultsTable>;

export type NavigationApp = Selectable<NavigationAppsTable>;
export type NewNavigationApp = Insertable<NavigationAppsTable>;

export type NavigationNode = Selectable<NavigationNodesTable>;
export type NewNavigationNode = Insertable<NavigationNodesTable>;

export type NavigationEdge = Selectable<NavigationEdgesTable>;
export type NewNavigationEdge = Insertable<NavigationEdgesTable>;

export type UIElement = Selectable<UIElementsTable>;
export type NewUIElement = Insertable<UIElementsTable>;

export type NewEdgeUIElement = Insertable<EdgeUIElementsTable>;

export type NewNodeModal = Insertable<NodeModalsTable>;

export type NewEdgeModal = Insertable<EdgeModalsTable>;

export type NewScrollPosition = Insertable<ScrollPositionsTable>;

export type NewPredictionOutcome = Insertable<PredictionOutcomesTable>;

export type PredictionTransitionStats = Selectable<PredictionTransitionStatsTable>;
export type NewPredictionTransitionStats = Insertable<PredictionTransitionStatsTable>;

export type ToolCall = Selectable<ToolCallsTable>;
export type NewToolCall = Insertable<ToolCallsTable>;

export type MemoryThresholds = Selectable<MemoryThresholdsTable>;
export type NewMemoryThresholds = Insertable<MemoryThresholdsTable>;

export type MemoryBaseline = Selectable<MemoryBaselinesTable>;
export type NewMemoryBaseline = Insertable<MemoryBaselinesTable>;
export type MemoryBaselineUpdate = Updateable<MemoryBaselinesTable>;

export type NewMemoryAuditResult = Insertable<MemoryAuditResultsTable>;

export type NewRecompositionMetrics = Insertable<RecompositionMetricsTable>;

export type DeviceSnapshot = Selectable<DeviceSnapshotsTable>;
export type NewDeviceSnapshot = Insertable<DeviceSnapshotsTable>;
export type DeviceSnapshotUpdate = Updateable<DeviceSnapshotsTable>;

export type VideoRecording = Selectable<VideoRecordingsTable>;
export type NewVideoRecording = Insertable<VideoRecordingsTable>;
export type VideoRecordingUpdate = Updateable<VideoRecordingsTable>;

export type NewTestExecution = Insertable<TestExecutionsTable>;

export type NewTestExecutionStep = Insertable<TestExecutionStepsTable>;

export type NewTestExecutionScreen = Insertable<TestExecutionScreensTable>;

export type TestCoverageSession = Selectable<TestCoverageSessionsTable>;
export type NewTestCoverageSession = Insertable<TestCoverageSessionsTable>;

export type TestNodeCoverage = Selectable<TestNodeCoverageTable>;
export type NewTestNodeCoverage = Insertable<TestNodeCoverageTable>;

export type TestEdgeCoverage = Selectable<TestEdgeCoverageTable>;
export type NewTestEdgeCoverage = Insertable<TestEdgeCoverageTable>;

// Failure tracking tables
interface FailureGroupsTable {
  id: string;
  type: "crash" | "anr" | "tool_failure";
  signature: string;
  title: string;
  message: string;
  severity: "critical" | "high" | "medium" | "low";
  first_occurrence: number;
  last_occurrence: number;
  total_count: number;
  unique_sessions: number;
  stack_trace_json: string | null;
  tool_call_info_json: string | null;
  created_at: Generated<string>;
  updated_at: string;
}

interface FailureOccurrencesTable {
  id: string;
  group_id: string;
  timestamp: number;
  device_id: string | null;
  device_model: string;
  os: string;
  app_version: string;
  session_id: string;
  screen_at_failure: string | null;
  test_name: string | null;
  test_execution_id: number | null;
  error_code: string | null;
  duration_ms: number | null;
  tool_args_json: string | null;
  created_at: Generated<string>;
}

interface FailureOccurrenceScreensTable {
  id: Generated<number>;
  occurrence_id: string;
  screen_name: string;
  visit_order: number;
  created_at: Generated<string>;
}

interface FailureCapturesTable {
  id: string;
  occurrence_id: string;
  type: "screenshot" | "video";
  path: string;
  timestamp: number;
  device_model: string;
  created_at: Generated<string>;
}

interface FailureNotificationsTable {
  id: Generated<number>;
  occurrence_id: string;
  group_id: string;
  type: "crash" | "anr" | "tool_failure";
  severity: "critical" | "high" | "medium" | "low";
  title: string;
  timestamp: number;
  acknowledged: number; // SQLite boolean (0/1)
  created_at: Generated<string>;
}

export type NewFailureGroup = Insertable<FailureGroupsTable>;
export type FailureGroupUpdate = Updateable<FailureGroupsTable>;

export type NewFailureOccurrence = Insertable<FailureOccurrencesTable>;

export type NewFailureOccurrenceScreen = Insertable<FailureOccurrenceScreensTable>;

export type NewFailureCapture = Insertable<FailureCapturesTable>;

export type NewFailureNotification = Insertable<FailureNotificationsTable>;

export type Crash = Selectable<CrashesTable>;
export type NewCrash = Insertable<CrashesTable>;

export type Anr = Selectable<AnrsTable>;
export type NewAnr = Insertable<AnrsTable>;

export type NavigationNodeFingerprint = Selectable<NavigationNodeFingerprintsTable>;
export type NewNavigationNodeFingerprint = Insertable<NavigationNodeFingerprintsTable>;

export type NavigationSuggestion = Selectable<NavigationSuggestionsTable>;
export type NewNavigationSuggestion = Insertable<NavigationSuggestionsTable>;
