-- 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,
  allow_insecure boolean NOT NULL DEFAULT false,
  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_account (
  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 (
  id varchar(128) NOT NULL,
  account_id varchar(64) NOT NULL DEFAULT '',
  event_type varchar(100) NOT NULL,
  event_data jsonb NOT NULL DEFAULT '{}'::jsonb,
  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 timestamptz NOT NULL DEFAULT now(),
  last_attempt timestamptz,
  created_at timestamptz NOT NULL DEFAULT now()
);

-- 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_account'
      AND column_name = 'account_type'
  ) THEN
    ALTER TABLE public.whatsapp_account 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_account'
      AND column_name = 'active'
  ) THEN
    ALTER TABLE public.whatsapp_account 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_account'
      AND column_name = 'config'
  ) THEN
    ALTER TABLE public.whatsapp_account 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_account'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.whatsapp_account 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_account'
      AND column_name = 'deleted_at'
  ) THEN
    ALTER TABLE public.whatsapp_account 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_account'
      AND column_name = 'display_name'
  ) THEN
    ALTER TABLE public.whatsapp_account 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_account'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.whatsapp_account 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_account'
      AND column_name = 'last_connected_at'
  ) THEN
    ALTER TABLE public.whatsapp_account 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_account'
      AND column_name = 'phone_number'
  ) THEN
    ALTER TABLE public.whatsapp_account 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_account'
      AND column_name = 'session_path'
  ) THEN
    ALTER TABLE public.whatsapp_account 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_account'
      AND column_name = 'status'
  ) THEN
    ALTER TABLE public.whatsapp_account 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_account'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.whatsapp_account 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_account'
      AND column_name = 'user_id'
  ) THEN
    ALTER TABLE public.whatsapp_account 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 = 'id'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN id varchar(128) 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(64) NOT NULL DEFAULT '';
  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 = 'event_type'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN event_type varchar(100) NOT NULL DEFAULT '';
  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 = 'event_data'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN event_data jsonb NOT NULL DEFAULT '{}'::jsonb;
  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 DEFAULT '';
  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_number'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN from_number varchar(64) NOT NULL DEFAULT '';
  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 = 'to_number'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN to_number varchar(64) NOT NULL DEFAULT '';
  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 = 'reason'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN reason text NOT NULL DEFAULT '';
  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 = 'attempts'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN attempts integer NOT NULL DEFAULT 0;
  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 timestamptz 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 = 'last_attempt'
  ) THEN
    ALTER TABLE public.message_cache ADD COLUMN last_attempt timestamptz;
  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 timestamptz NOT NULL DEFAULT now();
  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_account'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('whatsapp_account_pkey', 'public_whatsapp_account_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.whatsapp_account 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_account'
      AND constraint_name = 'pk_public_whatsapp_account'
  ) THEN
    ALTER TABLE public.whatsapp_account ADD CONSTRAINT pk_public_whatsapp_account 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 = 'pk_public_event_logs'
  ) THEN
    ALTER TABLE public.event_logs ADD CONSTRAINT pk_public_event_logs 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 = 'pk_public_message_cache'
  ) THEN
    ALTER TABLE public.message_cache ADD CONSTRAINT pk_public_message_cache 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_account_deleted_at
  ON public.whatsapp_account USING btree (deleted_at);

CREATE INDEX IF NOT EXISTS idx_whatsapp_account_user_id
  ON public.whatsapp_account 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_timestamp
  ON public.message_cache USING btree (timestamp DESC);

CREATE INDEX IF NOT EXISTS idx_message_cache_event_type
  ON public.message_cache USING btree (event_type);

-- 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_account'
      AND constraint_name = 'ukey_whatsapp_account_phone_number'
  ) THEN
    ALTER TABLE public.whatsapp_account ADD CONSTRAINT ukey_whatsapp_account_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;
$$;

-- 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_account'
      AND constraint_name = 'fk_whatsapp_account_user_id'
  ) THEN
    ALTER TABLE public.whatsapp_account
      ADD CONSTRAINT fk_whatsapp_account_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






