CREATE SEQUENCE IF NOT EXISTS ai_session_seq;

CREATE TABLE IF NOT EXISTS ai_session (
  "session_id" int check ("session_id" > 0) NOT NULL DEFAULT NEXTVAL ('ai_session_seq'),
  "uuid" varchar(64) NOT NULL,
  "site_id" int NOT NULL DEFAULT 0,
  "admin_id" int NOT NULL DEFAULT 0,
  "title" varchar(255) NOT NULL DEFAULT '',
  "messages" text NOT NULL DEFAULT '',
  "message_count" int NOT NULL DEFAULT 0,
  "created_at" timestamp(0) NOT NULL DEFAULT '2022-05-01 00:00:00',
  "updated_at" timestamp(0) NOT NULL DEFAULT '2022-05-01 00:00:00',
  PRIMARY KEY ("session_id")
);

CREATE UNIQUE INDEX IF NOT EXISTS "ai_session_uuid_admin_site" ON ai_session ("uuid","site_id","admin_id");
CREATE INDEX IF NOT EXISTS "ai_session_admin_site_updated" ON ai_session ("site_id","admin_id","updated_at");
