-- Add mobile app configs table for per-user mobile app management
CREATE TABLE IF NOT EXISTS mobile_app_configs (
  id VARCHAR PRIMARY KEY,
  user_id VARCHAR NOT NULL,
  token VARCHAR NOT NULL UNIQUE,
  name VARCHAR(255) NOT NULL,
  agent_id VARCHAR,
  status VARCHAR(50) NOT NULL DEFAULT 'active',
  icon_url TEXT,
  icon_path TEXT,
  brand_name VARCHAR(255),
  primary_color VARCHAR(50) DEFAULT '#6366F1',
  accent_color VARCHAR(50) DEFAULT '#1E40AF',
  onboarding_title TEXT,
  onboarding_footer TEXT,
  feature1_title VARCHAR(255),
  feature1_desc TEXT,
  feature2_title VARCHAR(255),
  feature2_desc TEXT,
  feature3_title VARCHAR(255),
  feature3_desc TEXT,
  feature4_title VARCHAR(255),
  feature4_desc TEXT,
  appointment_booking_enabled BOOLEAN DEFAULT false,
  total_calls INTEGER DEFAULT 0,
  total_minutes INTEGER DEFAULT 0,
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- Add mobile call sessions table for tracking mobile app calls
CREATE TABLE IF NOT EXISTS mobile_call_sessions (
  id VARCHAR PRIMARY KEY,
  mobile_config_id VARCHAR NOT NULL REFERENCES mobile_app_configs(id) ON DELETE CASCADE,
  user_id VARCHAR NOT NULL,
  session_token TEXT NOT NULL UNIQUE,
  visitor_ip TEXT,
  status TEXT NOT NULL DEFAULT 'pending',
  duration INTEGER,
  recording_url TEXT,
  transcript TEXT,
  ai_summary TEXT,
  sentiment TEXT,
  openai_session_id TEXT,
  openai_credential_id VARCHAR,
  started_at TIMESTAMP,
  ended_at TIMESTAMP,
  created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- Create indexes for faster lookups
CREATE INDEX IF NOT EXISTS idx_mobile_app_configs_user_id ON mobile_app_configs(user_id);
CREATE INDEX IF NOT EXISTS idx_mobile_app_configs_token ON mobile_app_configs(token);
CREATE INDEX IF NOT EXISTS idx_mobile_call_sessions_config_id ON mobile_call_sessions(mobile_config_id);
CREATE INDEX IF NOT EXISTS idx_mobile_call_sessions_token ON mobile_call_sessions(session_token);
