-- PostgreSQL Database Schema
-- Database: database
-- Generated by RelSpec

-- Sequences for schema: public
-- Tables for schema: public
CREATE TABLE IF NOT EXISTS public.users (
  active boolean NOT NULL DEFAULT true,
  created_at timestamp NOT NULL DEFAULT now(),
  deleted_at timestamp,
  email varchar(255) NOT NULL,
  full_name varchar(255),
  id varchar(36) NOT NULL,
  password varchar(255) NOT NULL,
  role varchar(50) NOT NULL DEFAULT 'user',
  updated_at timestamp NOT NULL DEFAULT now(),
  username varchar(255) NOT NULL
);

CREATE TABLE IF NOT EXISTS public.api_keys (
  active boolean NOT NULL DEFAULT true,
  created_at timestamp NOT NULL DEFAULT now(),
  deleted_at timestamp,
  expires_at timestamp,
  id varchar(36) NOT NULL,
  key varchar(255) NOT NULL,
  key_prefix varchar(20),
  last_used_at timestamp,
  name varchar(255) NOT NULL,
  permissions text,
  updated_at timestamp NOT NULL DEFAULT now(),
  user_id varchar(36) NOT NULL
);

CREATE TABLE IF NOT EXISTS public.hooks (
  active boolean NOT NULL DEFAULT true,
  created_at timestamp NOT NULL DEFAULT now(),
  deleted_at timestamp,
  description text,
  events text,
  headers text,
  id varchar(36) NOT NULL,
  method varchar(10) NOT NULL DEFAULT 'POST',
  name varchar(255) NOT NULL,
  retry_count integer NOT NULL DEFAULT '3',
  secret varchar(255),
  timeout integer NOT NULL DEFAULT '30',
  updated_at timestamp NOT NULL DEFAULT now(),
  url text NOT NULL,
  user_id varchar(36) NOT NULL
);

CREATE TABLE IF NOT EXISTS public.whatsapp_accounts (
  account_type varchar(50) NOT NULL,
  active boolean NOT NULL DEFAULT true,
  config text,
  created_at timestamp NOT NULL DEFAULT now(),
  deleted_at timestamp,
  display_name varchar(255),
  id varchar(36) NOT NULL,
  last_connected_at timestamp,
  phone_number varchar(50) NOT NULL,
  session_path text,
  status varchar(50) NOT NULL DEFAULT 'disconnected',
  updated_at timestamp NOT NULL DEFAULT now(),
  user_id varchar(36) NOT NULL
);

CREATE TABLE IF NOT EXISTS public.event_logs (
  action varchar(50),
  created_at timestamp NOT NULL DEFAULT now(),
  data text,
  entity_id varchar(36),
  entity_type varchar(100),
  error text,
  event_type varchar(100) NOT NULL,
  id varchar(36) NOT NULL,
  ip_address varchar(50),
  success boolean NOT NULL DEFAULT true,
  user_agent text,
  user_id varchar(36)
);

CREATE TABLE IF NOT EXISTS public.sessions (
  created_at timestamp NOT NULL DEFAULT now(),
  expires_at timestamp NOT NULL,
  id varchar(36) NOT NULL,
  ip_address varchar(50),
  token varchar(255) NOT NULL,
  updated_at timestamp NOT NULL DEFAULT now(),
  user_agent text,
  user_id varchar(36) NOT NULL
);

CREATE TABLE IF NOT EXISTS public.message_cache (
  account_id varchar(36) NOT NULL,
  chat_id varchar(255) NOT NULL,
  content text NOT NULL,
  created_at timestamp NOT NULL DEFAULT now(),
  from_me boolean NOT NULL,
  id varchar(36) NOT NULL,
  message_id varchar(255) NOT NULL,
  message_type varchar(50) NOT NULL,
  timestamp timestamp NOT NULL
);

-- Add missing columns for schema: public
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'users'
      AND column_name = 'active'
  ) THEN
    ALTER TABLE public.users ADD COLUMN active boolean NOT NULL DEFAULT true;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'users'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.users ADD COLUMN created_at timestamp NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'users'
      AND column_name = 'deleted_at'
  ) THEN
    ALTER TABLE public.users ADD COLUMN deleted_at timestamp;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'users'
      AND column_name = 'email'
  ) THEN
    ALTER TABLE public.users ADD COLUMN email varchar(255) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'users'
      AND column_name = 'full_name'
  ) THEN
    ALTER TABLE public.users ADD COLUMN full_name varchar(255);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'users'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.users ADD COLUMN id varchar(36) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'users'
      AND column_name = 'password'
  ) THEN
    ALTER TABLE public.users ADD COLUMN password varchar(255) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'users'
      AND column_name = 'role'
  ) THEN
    ALTER TABLE public.users ADD COLUMN role varchar(50) NOT NULL DEFAULT 'user';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'users'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.users ADD COLUMN updated_at timestamp NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'users'
      AND column_name = 'username'
  ) THEN
    ALTER TABLE public.users ADD COLUMN username varchar(255) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'api_keys'
      AND column_name = 'active'
  ) THEN
    ALTER TABLE public.api_keys ADD COLUMN active boolean NOT NULL DEFAULT true;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'api_keys'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.api_keys ADD COLUMN created_at timestamp NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'api_keys'
      AND column_name = 'deleted_at'
  ) THEN
    ALTER TABLE public.api_keys ADD COLUMN deleted_at timestamp;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'api_keys'
      AND column_name = 'expires_at'
  ) THEN
    ALTER TABLE public.api_keys ADD COLUMN expires_at timestamp;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'api_keys'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.api_keys ADD COLUMN id varchar(36) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'api_keys'
      AND column_name = 'key'
  ) THEN
    ALTER TABLE public.api_keys ADD COLUMN key varchar(255) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'api_keys'
      AND column_name = 'key_prefix'
  ) THEN
    ALTER TABLE public.api_keys ADD COLUMN key_prefix varchar(20);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'api_keys'
      AND column_name = 'last_used_at'
  ) THEN
    ALTER TABLE public.api_keys ADD COLUMN last_used_at timestamp;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'api_keys'
      AND column_name = 'name'
  ) THEN
    ALTER TABLE public.api_keys ADD COLUMN name varchar(255) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'api_keys'
      AND column_name = 'permissions'
  ) THEN
    ALTER TABLE public.api_keys ADD COLUMN permissions text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'api_keys'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.api_keys ADD COLUMN updated_at timestamp NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'api_keys'
      AND column_name = 'user_id'
  ) THEN
    ALTER TABLE public.api_keys ADD COLUMN user_id varchar(36) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND column_name = 'active'
  ) THEN
    ALTER TABLE public.hooks ADD COLUMN active boolean NOT NULL DEFAULT true;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.hooks ADD COLUMN created_at timestamp NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND column_name = 'deleted_at'
  ) THEN
    ALTER TABLE public.hooks ADD COLUMN deleted_at timestamp;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND column_name = 'description'
  ) THEN
    ALTER TABLE public.hooks ADD COLUMN description text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND column_name = 'events'
  ) THEN
    ALTER TABLE public.hooks ADD COLUMN events text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND column_name = 'headers'
  ) THEN
    ALTER TABLE public.hooks ADD COLUMN headers text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.hooks ADD COLUMN id varchar(36) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND column_name = 'method'
  ) THEN
    ALTER TABLE public.hooks ADD COLUMN method varchar(10) NOT NULL DEFAULT 'POST';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND column_name = 'name'
  ) THEN
    ALTER TABLE public.hooks ADD COLUMN name varchar(255) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND column_name = 'retry_count'
  ) THEN
    ALTER TABLE public.hooks ADD COLUMN retry_count integer NOT NULL DEFAULT '3';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND column_name = 'secret'
  ) THEN
    ALTER TABLE public.hooks ADD COLUMN secret varchar(255);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND column_name = 'timeout'
  ) THEN
    ALTER TABLE public.hooks ADD COLUMN timeout integer NOT NULL DEFAULT '30';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.hooks ADD COLUMN updated_at timestamp NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND column_name = 'url'
  ) THEN
    ALTER TABLE public.hooks ADD COLUMN url text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND column_name = 'user_id'
  ) THEN
    ALTER TABLE public.hooks ADD COLUMN user_id varchar(36) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND column_name = 'account_type'
  ) THEN
    ALTER TABLE public.whatsapp_accounts ADD COLUMN account_type varchar(50) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND column_name = 'active'
  ) THEN
    ALTER TABLE public.whatsapp_accounts ADD COLUMN active boolean NOT NULL DEFAULT true;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND column_name = 'config'
  ) THEN
    ALTER TABLE public.whatsapp_accounts ADD COLUMN config text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.whatsapp_accounts ADD COLUMN created_at timestamp NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND column_name = 'deleted_at'
  ) THEN
    ALTER TABLE public.whatsapp_accounts ADD COLUMN deleted_at timestamp;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND column_name = 'display_name'
  ) THEN
    ALTER TABLE public.whatsapp_accounts ADD COLUMN display_name varchar(255);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.whatsapp_accounts ADD COLUMN id varchar(36) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND column_name = 'last_connected_at'
  ) THEN
    ALTER TABLE public.whatsapp_accounts ADD COLUMN last_connected_at timestamp;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND column_name = 'phone_number'
  ) THEN
    ALTER TABLE public.whatsapp_accounts ADD COLUMN phone_number varchar(50) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND column_name = 'session_path'
  ) THEN
    ALTER TABLE public.whatsapp_accounts ADD COLUMN session_path text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND column_name = 'status'
  ) THEN
    ALTER TABLE public.whatsapp_accounts ADD COLUMN status varchar(50) NOT NULL DEFAULT 'disconnected';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.whatsapp_accounts ADD COLUMN updated_at timestamp NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND column_name = 'user_id'
  ) THEN
    ALTER TABLE public.whatsapp_accounts ADD COLUMN user_id varchar(36) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'event_logs'
      AND column_name = 'action'
  ) THEN
    ALTER TABLE public.event_logs ADD COLUMN action varchar(50);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'event_logs'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.event_logs ADD COLUMN created_at timestamp NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'event_logs'
      AND column_name = 'data'
  ) THEN
    ALTER TABLE public.event_logs ADD COLUMN data text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'event_logs'
      AND column_name = 'entity_id'
  ) THEN
    ALTER TABLE public.event_logs ADD COLUMN entity_id varchar(36);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'event_logs'
      AND column_name = 'entity_type'
  ) THEN
    ALTER TABLE public.event_logs ADD COLUMN entity_type varchar(100);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'event_logs'
      AND column_name = 'error'
  ) THEN
    ALTER TABLE public.event_logs ADD COLUMN error text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'event_logs'
      AND column_name = 'event_type'
  ) THEN
    ALTER TABLE public.event_logs ADD COLUMN event_type varchar(100) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'event_logs'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.event_logs ADD COLUMN id varchar(36) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'event_logs'
      AND column_name = 'ip_address'
  ) THEN
    ALTER TABLE public.event_logs ADD COLUMN ip_address varchar(50);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'event_logs'
      AND column_name = 'success'
  ) THEN
    ALTER TABLE public.event_logs ADD COLUMN success boolean NOT NULL DEFAULT true;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'event_logs'
      AND column_name = 'user_agent'
  ) THEN
    ALTER TABLE public.event_logs ADD COLUMN user_agent text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'event_logs'
      AND column_name = 'user_id'
  ) THEN
    ALTER TABLE public.event_logs ADD COLUMN user_id varchar(36);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'sessions'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.sessions ADD COLUMN created_at timestamp NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'sessions'
      AND column_name = 'expires_at'
  ) THEN
    ALTER TABLE public.sessions ADD COLUMN expires_at timestamp NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'sessions'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.sessions ADD COLUMN id varchar(36) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'sessions'
      AND column_name = 'ip_address'
  ) THEN
    ALTER TABLE public.sessions ADD COLUMN ip_address varchar(50);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'sessions'
      AND column_name = 'token'
  ) THEN
    ALTER TABLE public.sessions ADD COLUMN token varchar(255) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'sessions'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.sessions ADD COLUMN updated_at timestamp NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'sessions'
      AND column_name = 'user_agent'
  ) THEN
    ALTER TABLE public.sessions ADD COLUMN user_agent text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'sessions'
      AND column_name = 'user_id'
  ) THEN
    ALTER TABLE public.sessions ADD COLUMN user_id varchar(36) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'message_cache'
      AND column_name = 'account_id'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN account_id varchar(36) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'message_cache'
      AND column_name = 'chat_id'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN chat_id varchar(255) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'message_cache'
      AND column_name = 'content'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN content text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'message_cache'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN created_at timestamp NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'message_cache'
      AND column_name = 'from_me'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN from_me boolean NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'message_cache'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN id varchar(36) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'message_cache'
      AND column_name = 'message_id'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN message_id varchar(255) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'message_cache'
      AND column_name = 'message_type'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN message_type varchar(50) NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'message_cache'
      AND column_name = 'timestamp'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN timestamp timestamp NOT NULL;
  END IF;
END;
$$;

-- Primary keys for schema: public
DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'users'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('users_pkey', 'public_users_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.users DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'users'
      AND constraint_name = 'pk_public_users'
  ) THEN
    ALTER TABLE public.users ADD CONSTRAINT pk_public_users PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'api_keys'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('api_keys_pkey', 'public_api_keys_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.api_keys DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'api_keys'
      AND constraint_name = 'pk_public_api_keys'
  ) THEN
    ALTER TABLE public.api_keys ADD CONSTRAINT pk_public_api_keys PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'hooks'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('hooks_pkey', 'public_hooks_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.hooks DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND constraint_name = 'pk_public_hooks'
  ) THEN
    ALTER TABLE public.hooks ADD CONSTRAINT pk_public_hooks PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'whatsapp_accounts'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('whatsapp_accounts_pkey', 'public_whatsapp_accounts_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.whatsapp_accounts DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.whatsapp_accounts ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'event_logs'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('event_logs_pkey', 'public_event_logs_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.event_logs DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'event_logs'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.event_logs ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'sessions'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('sessions_pkey', 'public_sessions_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.sessions DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'sessions'
      AND constraint_name = 'pk_public_sessions'
  ) THEN
    ALTER TABLE public.sessions ADD CONSTRAINT pk_public_sessions PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'message_cache'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('message_cache_pkey', 'public_message_cache_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.message_cache DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'message_cache'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.message_cache ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

-- Indexes for schema: public
CREATE INDEX IF NOT EXISTS idx_users_deleted_at
  ON public.users USING btree (deleted_at);

CREATE INDEX IF NOT EXISTS idx_api_keys_deleted_at
  ON public.api_keys USING btree (deleted_at);

CREATE INDEX IF NOT EXISTS idx_api_keys_user_id
  ON public.api_keys USING btree (user_id);

CREATE INDEX IF NOT EXISTS idx_hooks_deleted_at
  ON public.hooks USING btree (deleted_at);

CREATE INDEX IF NOT EXISTS idx_hooks_user_id
  ON public.hooks USING btree (user_id);

CREATE INDEX IF NOT EXISTS idx_whatsapp_accounts_deleted_at
  ON public.whatsapp_accounts USING btree (deleted_at);

CREATE INDEX IF NOT EXISTS idx_whatsapp_accounts_user_id
  ON public.whatsapp_accounts USING btree (user_id);

CREATE INDEX IF NOT EXISTS idx_event_logs_created_at
  ON public.event_logs USING btree (created_at);

CREATE INDEX IF NOT EXISTS idx_event_logs_entity_id
  ON public.event_logs USING btree (entity_id);

CREATE INDEX IF NOT EXISTS idx_event_logs_entity_type
  ON public.event_logs USING btree (entity_type);

CREATE INDEX IF NOT EXISTS idx_event_logs_event_type
  ON public.event_logs USING btree (event_type);

CREATE INDEX IF NOT EXISTS idx_event_logs_user_id
  ON public.event_logs USING btree (user_id);

CREATE INDEX IF NOT EXISTS idx_sessions_expires_at
  ON public.sessions USING btree (expires_at);

CREATE INDEX IF NOT EXISTS idx_sessions_user_id
  ON public.sessions USING btree (user_id);

CREATE INDEX IF NOT EXISTS idx_message_cache_account_id
  ON public.message_cache USING btree (account_id);

CREATE INDEX IF NOT EXISTS idx_message_cache_chat_id
  ON public.message_cache USING btree (chat_id);

CREATE INDEX IF NOT EXISTS idx_message_cache_from_me
  ON public.message_cache USING btree (from_me);

CREATE INDEX IF NOT EXISTS idx_message_cache_timestamp
  ON public.message_cache USING btree (timestamp);

-- Unique constraints for schema: public
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'users'
      AND constraint_name = 'ukey_users_email'
  ) THEN
    ALTER TABLE public.users ADD CONSTRAINT ukey_users_email UNIQUE (email);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'users'
      AND constraint_name = 'ukey_users_username'
  ) THEN
    ALTER TABLE public.users ADD CONSTRAINT ukey_users_username UNIQUE (username);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'api_keys'
      AND constraint_name = 'ukey_api_keys_key'
  ) THEN
    ALTER TABLE public.api_keys ADD CONSTRAINT ukey_api_keys_key UNIQUE (key);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND constraint_name = 'ukey_whatsapp_accounts_phone_number'
  ) THEN
    ALTER TABLE public.whatsapp_accounts ADD CONSTRAINT ukey_whatsapp_accounts_phone_number UNIQUE (phone_number);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'sessions'
      AND constraint_name = 'ukey_sessions_token'
  ) THEN
    ALTER TABLE public.sessions ADD CONSTRAINT ukey_sessions_token UNIQUE (token);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'message_cache'
      AND constraint_name = 'ukey_message_cache_message_id'
  ) THEN
    ALTER TABLE public.message_cache ADD CONSTRAINT ukey_message_cache_message_id UNIQUE (message_id);
  END IF;
END;
$$;

-- Check constraints for schema: public
-- Foreign keys for schema: public
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'api_keys'
      AND constraint_name = 'fk_api_keys_user_id'
  ) THEN
    ALTER TABLE public.api_keys
      ADD CONSTRAINT fk_api_keys_user_id
      FOREIGN KEY (user_id)
      REFERENCES public.users (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'hooks'
      AND constraint_name = 'fk_hooks_user_id'
  ) THEN
    ALTER TABLE public.hooks
      ADD CONSTRAINT fk_hooks_user_id
      FOREIGN KEY (user_id)
      REFERENCES public.users (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'whatsapp_accounts'
      AND constraint_name = 'fk_whatsapp_accounts_user_id'
  ) THEN
    ALTER TABLE public.whatsapp_accounts
      ADD CONSTRAINT fk_whatsapp_accounts_user_id
      FOREIGN KEY (user_id)
      REFERENCES public.users (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'event_logs'
      AND constraint_name = 'fk_event_logs_user_id'
  ) THEN
    ALTER TABLE public.event_logs
      ADD CONSTRAINT fk_event_logs_user_id
      FOREIGN KEY (user_id)
      REFERENCES public.users (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'sessions'
      AND constraint_name = 'fk_sessions_user_id'
  ) THEN
    ALTER TABLE public.sessions
      ADD CONSTRAINT fk_sessions_user_id
      FOREIGN KEY (user_id)
      REFERENCES public.users (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;-- Set sequence values for schema: public
-- Comments for schema: public







