CREATE TABLE IF NOT EXISTS asterisk_event_log (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  bridge_event_id TEXT NOT NULL UNIQUE,
  ami_event_name TEXT NOT NULL,
  call_id TEXT NOT NULL,
  linked_id TEXT,
  interaction_id TEXT,
  recording_id TEXT,
  forward_status TEXT NOT NULL,
  payload_json TEXT NOT NULL DEFAULT '{}',
  last_error TEXT,
  created_at TEXT NOT NULL,
  updated_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_asterisk_event_log_bridge_event_id ON asterisk_event_log(bridge_event_id);
CREATE INDEX IF NOT EXISTS idx_asterisk_event_log_ami_event_name ON asterisk_event_log(ami_event_name);
CREATE INDEX IF NOT EXISTS idx_asterisk_event_log_call_id ON asterisk_event_log(call_id);
CREATE INDEX IF NOT EXISTS idx_asterisk_event_log_linked_id ON asterisk_event_log(linked_id);
CREATE INDEX IF NOT EXISTS idx_asterisk_event_log_interaction_id ON asterisk_event_log(interaction_id);
CREATE INDEX IF NOT EXISTS idx_asterisk_event_log_recording_id ON asterisk_event_log(recording_id);
CREATE INDEX IF NOT EXISTS idx_asterisk_event_log_forward_status ON asterisk_event_log(forward_status);
CREATE INDEX IF NOT EXISTS idx_asterisk_event_log_created_at ON asterisk_event_log(created_at);
CREATE INDEX IF NOT EXISTS idx_asterisk_event_log_updated_at ON asterisk_event_log(updated_at);

CREATE TABLE IF NOT EXISTS asterisk_call_links (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  call_id TEXT NOT NULL UNIQUE,
  linked_id TEXT,
  queue_code TEXT NOT NULL,
  queue_id TEXT NOT NULL,
  interaction_id TEXT NOT NULL,
  caller_number TEXT,
  caller_name TEXT,
  status TEXT NOT NULL,
  started_at TEXT NOT NULL,
  ended_at TEXT,
  updated_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_asterisk_call_links_call_id ON asterisk_call_links(call_id);
CREATE INDEX IF NOT EXISTS idx_asterisk_call_links_linked_id ON asterisk_call_links(linked_id);
CREATE INDEX IF NOT EXISTS idx_asterisk_call_links_queue_code ON asterisk_call_links(queue_code);
CREATE INDEX IF NOT EXISTS idx_asterisk_call_links_queue_id ON asterisk_call_links(queue_id);
CREATE INDEX IF NOT EXISTS idx_asterisk_call_links_interaction_id ON asterisk_call_links(interaction_id);
CREATE INDEX IF NOT EXISTS idx_asterisk_call_links_caller_number ON asterisk_call_links(caller_number);
CREATE INDEX IF NOT EXISTS idx_asterisk_call_links_status ON asterisk_call_links(status);
CREATE INDEX IF NOT EXISTS idx_asterisk_call_links_started_at ON asterisk_call_links(started_at);
CREATE INDEX IF NOT EXISTS idx_asterisk_call_links_ended_at ON asterisk_call_links(ended_at);
CREATE INDEX IF NOT EXISTS idx_asterisk_call_links_updated_at ON asterisk_call_links(updated_at);
