// WhatsHooked Database Schema
// This file defines the database schema for WhatsHooked Phase 2

Table user {
  id varchar(36) [primary key, note: 'UUID']
  username varchar(255) [unique, not null]
  email varchar(255) [unique, not null]
  password varchar(255) [not null, note: 'Bcrypt hashed password']
  full_name varchar(255)
  role varchar(50) [not null, default: 'user', note: 'admin, user, viewer']
  active boolean [not null, default: true]
  created_at timestamp [not null, default: `now()`]
  updated_at timestamp [not null, default: `now()`]
  deleted_at timestamp [null, note: 'Soft delete']
  
  indexes {
    (deleted_at) [name: 'idx_users_deleted_at']
  }
}

Table api_key {
  id varchar(36) [primary key, note: 'UUID']
  user_id varchar(36) [not null, ref: > users.id]
  name varchar(255) [not null, note: 'Friendly name for the API key']
  key varchar(255) [unique, not null, note: 'Hashed API key']
  key_prefix varchar(20) [note: 'First few characters for display']
  permissions text [note: 'JSON array of permissions']
  last_used_at timestamp [null]
  expires_at timestamp [null]
  active boolean [not null, default: true]
  created_at timestamp [not null, default: `now()`]
  updated_at timestamp [not null, default: `now()`]
  deleted_at timestamp [null]
  
  indexes {
    (user_id) [name: 'idx_api_keys_user_id']
    (deleted_at) [name: 'idx_api_keys_deleted_at']
  }
}

Table hook {
  id varchar(36) [primary key, note: 'UUID']
  user_id varchar(36) [not null, ref: > users.id]
  name varchar(255) [not null]
  url text [not null]
  method varchar(10) [not null, default: 'POST', note: 'HTTP method']
  headers text [note: 'JSON encoded headers']
  events text [note: 'JSON array of event types']
  active boolean [not null, default: true]
  allow_insecure boolean [not null, default: false, note: 'Skip TLS certificate verification']
  description text
  secret varchar(255) [note: 'HMAC signature secret']
  retry_count int [not null, default: 3]
  timeout int [not null, default: 30, note: 'Timeout in seconds']
  created_at timestamp [not null, default: `now()`]
  updated_at timestamp [not null, default: `now()`]
  deleted_at timestamp [null]
  
  indexes {
    (user_id) [name: 'idx_hooks_user_id']
    (deleted_at) [name: 'idx_hooks_deleted_at']
  }
}

Table whatsapp_account {
  id varchar(36) [primary key, note: 'UUID']
  user_id varchar(36) [not null, ref: > users.id]
  account_type varchar(50) [not null, note: 'whatsmeow or business-api']
  phone_number varchar(50) [unique, not null]
  display_name varchar(255)
  session_path text
  status varchar(50) [not null, default: 'disconnected', note: 'connected, disconnected, pairing']
  last_connected_at timestamp [null]
  active boolean [not null, default: true]
  config text [note: 'JSON encoded additional config']
  created_at timestamp [not null, default: `now()`]
  updated_at timestamp [not null, default: `now()`]
  deleted_at timestamp [null]
  
  indexes {
    (user_id) [name: 'idx_whatsapp_accounts_user_id']
    (deleted_at) [name: 'idx_whatsapp_accounts_deleted_at']
  }
}

Table event_log {
  id varchar(36) [primary key, note: 'UUID']
  user_id varchar(36) [ref: > users.id, note: 'Optional user reference']
  event_type varchar(100) [not null]
  entity_type varchar(100) [note: 'user, hook, account, etc.']
  entity_id varchar(36)
  action varchar(50) [note: 'create, update, delete, read']
  data text [note: 'JSON encoded event data']
  ip_address varchar(50)
  user_agent text
  success boolean [not null, default: true]
  error text
  created_at timestamp [not null, default: `now()`]
  
  indexes {
    (user_id) [name: 'idx_event_logs_user_id']
    (event_type) [name: 'idx_event_logs_event_type']
    (entity_type) [name: 'idx_event_logs_entity_type']
    (entity_id) [name: 'idx_event_logs_entity_id']
    (created_at) [name: 'idx_event_logs_created_at']
  }
}

Table session {
  id varchar(36) [primary key, note: 'UUID']
  user_id varchar(36) [not null, ref: > users.id]
  token varchar(255) [unique, not null, note: 'Session token hash']
  ip_address varchar(50)
  user_agent text
  expires_at timestamp [not null]
  created_at timestamp [not null, default: `now()`]
  updated_at timestamp [not null, default: `now()`]
  
  indexes {
    (user_id) [name: 'idx_sessions_user_id']
    (expires_at) [name: 'idx_sessions_expires_at']
  }
}

Table message_cache {
  id varchar(36) [primary key, note: 'UUID']
  account_id varchar(36) [not null]
  message_id varchar(255) [unique, not null]
  chat_id varchar(255) [not null]
  from_me boolean [not null]
  timestamp timestamp [not null]
  message_type varchar(50) [not null, note: 'text, image, video, etc.']
  content text [not null, note: 'JSON encoded message content']
  created_at timestamp [not null, default: `now()`]
  
  indexes {
    (account_id) [name: 'idx_message_cache_account_id']
    (chat_id) [name: 'idx_message_cache_chat_id']
    (from_me) [name: 'idx_message_cache_from_me']
    (timestamp) [name: 'idx_message_cache_timestamp']
  }
}

// Reference documentation
Ref: api_keys.user_id > users.id [delete: cascade]
Ref: hooks.user_id > users.id [delete: cascade]
Ref: whatsapp_accounts.user_id > users.id [delete: cascade]
Ref: sessions.user_id > users.id [delete: cascade]
