-- SQLite Database Schema
-- Adapted from PostgreSQL schema for Phase 2

-- Users table
CREATE TABLE IF NOT EXISTS users (
  id VARCHAR(36) PRIMARY KEY,
  username VARCHAR(255) NOT NULL UNIQUE,
  email VARCHAR(255) NOT NULL UNIQUE,
  password VARCHAR(255) NOT NULL,
  full_name VARCHAR(255),
  role VARCHAR(50) NOT NULL DEFAULT 'user',
  active BOOLEAN NOT NULL DEFAULT 1,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  deleted_at TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_users_deleted_at ON users(deleted_at);

-- API Keys table
CREATE TABLE IF NOT EXISTS api_keys (
  id VARCHAR(36) PRIMARY KEY,
  user_id VARCHAR(36) NOT NULL,
  name VARCHAR(255) NOT NULL,
  key VARCHAR(255) NOT NULL UNIQUE,
  key_prefix VARCHAR(20),
  permissions TEXT,
  active BOOLEAN NOT NULL DEFAULT 1,
  expires_at TIMESTAMP,
  last_used_at TIMESTAMP,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  deleted_at TIMESTAMP,
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE NO ACTION
);

CREATE INDEX IF NOT EXISTS idx_api_keys_user_id ON api_keys(user_id);
CREATE INDEX IF NOT EXISTS idx_api_keys_deleted_at ON api_keys(deleted_at);

-- Hooks table
CREATE TABLE IF NOT EXISTS hooks (
  id VARCHAR(36) PRIMARY KEY,
  user_id VARCHAR(36) NOT NULL,
  name VARCHAR(255) NOT NULL,
  url TEXT NOT NULL,
  method VARCHAR(10) NOT NULL DEFAULT 'POST',
  description TEXT,
  secret VARCHAR(255),
  headers TEXT,
  events TEXT,
  retry_count INTEGER NOT NULL DEFAULT 3,
  timeout INTEGER NOT NULL DEFAULT 30,
  active BOOLEAN NOT NULL DEFAULT 1,
  allow_insecure BOOLEAN NOT NULL DEFAULT 0,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  deleted_at TIMESTAMP,
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE NO ACTION
);

CREATE INDEX IF NOT EXISTS idx_hooks_user_id ON hooks(user_id);
CREATE INDEX IF NOT EXISTS idx_hooks_deleted_at ON hooks(deleted_at);

-- WhatsApp Accounts table
CREATE TABLE IF NOT EXISTS whatsapp_account (
  id VARCHAR(36) PRIMARY KEY,
  user_id VARCHAR(36) NOT NULL,
  phone_number VARCHAR(50) NOT NULL UNIQUE,
  display_name VARCHAR(255),
  account_type VARCHAR(50) NOT NULL DEFAULT 'whatsmeow',
  config TEXT,
  session_path TEXT,
  status VARCHAR(50) NOT NULL DEFAULT 'disconnected',
  active BOOLEAN NOT NULL DEFAULT 1,
  last_connected_at TIMESTAMP,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  deleted_at TIMESTAMP,
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE NO ACTION
);

CREATE INDEX IF NOT EXISTS idx_whatsapp_account_user_id ON whatsapp_account(user_id);
CREATE INDEX IF NOT EXISTS idx_whatsapp_account_deleted_at ON whatsapp_account(deleted_at);

-- Event Logs table
CREATE TABLE IF NOT EXISTS event_logs (
  id VARCHAR(36) PRIMARY KEY,
  event_type VARCHAR(100) NOT NULL,
  action VARCHAR(50),
  entity_type VARCHAR(100),
  entity_id VARCHAR(36),
  user_id VARCHAR(36),
  data TEXT,
  error TEXT,
  success BOOLEAN NOT NULL DEFAULT 1,
  ip_address VARCHAR(50),
  user_agent TEXT,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE NO ACTION
);

CREATE INDEX IF NOT EXISTS idx_event_logs_event_type ON event_logs(event_type);
CREATE INDEX IF NOT EXISTS idx_event_logs_entity_type ON event_logs(entity_type);
CREATE INDEX IF NOT EXISTS idx_event_logs_entity_id ON event_logs(entity_id);
CREATE INDEX IF NOT EXISTS idx_event_logs_user_id ON event_logs(user_id);
CREATE INDEX IF NOT EXISTS idx_event_logs_created_at ON event_logs(created_at);

-- Sessions table
CREATE TABLE IF NOT EXISTS sessions (
  id VARCHAR(36) PRIMARY KEY,
  user_id VARCHAR(36) NOT NULL,
  token VARCHAR(255) NOT NULL UNIQUE,
  ip_address VARCHAR(50),
  user_agent TEXT,
  expires_at TIMESTAMP NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE NO ACTION
);

CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions(user_id);
CREATE INDEX IF NOT EXISTS idx_sessions_expires_at ON sessions(expires_at);

-- Message Cache table
CREATE TABLE IF NOT EXISTS message_cache (
  id VARCHAR(128) PRIMARY KEY,
  account_id VARCHAR(64) NOT NULL DEFAULT '',
  event_type VARCHAR(100) NOT NULL,
  event_data TEXT NOT NULL,
  message_id VARCHAR(255) NOT NULL DEFAULT '',
  from_number VARCHAR(64) NOT NULL DEFAULT '',
  to_number VARCHAR(64) NOT NULL DEFAULT '',
  reason TEXT NOT NULL DEFAULT '',
  attempts INTEGER NOT NULL DEFAULT 0,
  timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  last_attempt TIMESTAMP,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_message_cache_timestamp ON message_cache(timestamp DESC);
CREATE INDEX IF NOT EXISTS idx_message_cache_event_type ON message_cache(event_type);
