-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "ai";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "analytics";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "auth";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "automation";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "files";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "messaging";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "workspace";

-- CreateTable
CREATE TABLE "auth"."users" (
    "id" UUID NOT NULL,
    "email" VARCHAR(255) NOT NULL,
    "email_verified" BOOLEAN NOT NULL DEFAULT false,
    "phone" VARCHAR(30),
    "phone_verified" BOOLEAN NOT NULL DEFAULT false,
    "password_hash" VARCHAR(255),
    "display_name" VARCHAR(80) NOT NULL,
    "real_name" VARCHAR(80),
    "avatar_url" VARCHAR(500),
    "title" VARCHAR(120),
    "bio" TEXT,
    "timezone" VARCHAR(50) NOT NULL DEFAULT 'UTC',
    "locale" VARCHAR(10) NOT NULL DEFAULT 'en',
    "pronouns" VARCHAR(30),
    "status_text" VARCHAR(100),
    "status_emoji" VARCHAR(50),
    "status_expires" TIMESTAMPTZ,
    "presence" VARCHAR(15) NOT NULL DEFAULT 'offline',
    "last_active_at" TIMESTAMPTZ,
    "is_bot" BOOLEAN NOT NULL DEFAULT false,
    "is_deleted" BOOLEAN NOT NULL DEFAULT false,
    "deleted_at" TIMESTAMPTZ,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "users_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "auth"."user_identities" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "provider" VARCHAR(50) NOT NULL,
    "provider_user_id" VARCHAR(255) NOT NULL,
    "provider_email" VARCHAR(255),
    "provider_data" JSONB,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "user_identities_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "auth"."sessions" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "token_hash" VARCHAR(255) NOT NULL,
    "device_type" VARCHAR(20),
    "device_name" VARCHAR(120),
    "device_id" VARCHAR(255),
    "ip_address" VARCHAR(45),
    "user_agent" TEXT,
    "location" VARCHAR(120),
    "expires_at" TIMESTAMPTZ NOT NULL,
    "revoked_at" TIMESTAMPTZ,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "sessions_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "auth"."two_factor_auth" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "secret" VARCHAR(255) NOT NULL,
    "backup_codes" JSONB,
    "enabled" BOOLEAN NOT NULL DEFAULT false,
    "enabled_at" TIMESTAMPTZ,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "two_factor_auth_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "auth"."password_resets" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "token_hash" VARCHAR(255) NOT NULL,
    "expires_at" TIMESTAMPTZ NOT NULL,
    "used_at" TIMESTAMPTZ,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "password_resets_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."workspaces" (
    "id" UUID NOT NULL,
    "name" VARCHAR(80) NOT NULL,
    "domain" VARCHAR(50) NOT NULL,
    "logo_url" VARCHAR(500),
    "description" TEXT,
    "plan" VARCHAR(20) NOT NULL DEFAULT 'free',
    "plan_expires" TIMESTAMPTZ,
    "max_members" INTEGER NOT NULL DEFAULT 100,
    "max_file_size" BIGINT NOT NULL DEFAULT 104857600,
    "message_retention_days" INTEGER,
    "file_retention_days" INTEGER,
    "allow_create_channels" BOOLEAN NOT NULL DEFAULT true,
    "allow_custom_emoji" BOOLEAN NOT NULL DEFAULT true,
    "allow_shared_channels" BOOLEAN NOT NULL DEFAULT false,
    "enforce_2fa" BOOLEAN NOT NULL DEFAULT false,
    "sso_required" BOOLEAN NOT NULL DEFAULT false,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "workspaces_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."workspace_members" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "role" VARCHAR(20) NOT NULL DEFAULT 'member',
    "guest_type" VARCHAR(20),
    "display_name" VARCHAR(80),
    "title" VARCHAR(120),
    "joined_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "invited_by" UUID,
    "deactivated_at" TIMESTAMPTZ,
    "deactivated_by" UUID,

    CONSTRAINT "workspace_members_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."workspace_invitations" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "email" VARCHAR(255) NOT NULL,
    "role" VARCHAR(20) NOT NULL DEFAULT 'member',
    "token" VARCHAR(255) NOT NULL,
    "invited_by" UUID NOT NULL,
    "accepted_at" TIMESTAMPTZ,
    "expires_at" TIMESTAMPTZ NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "workspace_invitations_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."user_groups" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "name" VARCHAR(80) NOT NULL,
    "handle" VARCHAR(50) NOT NULL,
    "description" TEXT,
    "is_auto" BOOLEAN NOT NULL DEFAULT false,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "user_groups_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."user_group_members" (
    "user_group_id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "added_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "added_by" UUID,

    CONSTRAINT "user_group_members_pkey" PRIMARY KEY ("user_group_id","user_id")
);

-- CreateTable
CREATE TABLE "workspace"."custom_emojis" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "name" VARCHAR(50) NOT NULL,
    "image_url" VARCHAR(500) NOT NULL,
    "aliases" VARCHAR(255)[],
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "custom_emojis_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."workspace_settings" (
    "workspace_id" UUID NOT NULL,
    "default_channel" VARCHAR(80) NOT NULL DEFAULT 'general',
    "allowed_file_types" VARCHAR(255)[],
    "allowed_file_extensions" VARCHAR(255)[],
    "message_edit_window_minutes" INTEGER NOT NULL DEFAULT 0,
    "allow_message_deletion" BOOLEAN NOT NULL DEFAULT true,
    "default_retention_days" INTEGER,
    "require_link_previews" BOOLEAN NOT NULL DEFAULT true,
    "allowed_auth_domains" VARCHAR(255)[],
    "sidebar_sections" JSONB NOT NULL DEFAULT '[]',
    "custom_theme" JSONB,
    "announcement_channel" VARCHAR(80),
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "workspace_settings_pkey" PRIMARY KEY ("workspace_id")
);

-- CreateTable
CREATE TABLE "workspace"."lists" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "channel_id" UUID,
    "name" VARCHAR(120) NOT NULL,
    "description" TEXT,
    "view_type" VARCHAR(20) NOT NULL DEFAULT 'table',
    "group_by_field" VARCHAR(50),
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "lists_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."list_fields" (
    "id" UUID NOT NULL,
    "list_id" UUID NOT NULL,
    "name" VARCHAR(80) NOT NULL,
    "field_type" VARCHAR(20) NOT NULL,
    "options" JSONB,
    "order_index" INTEGER NOT NULL DEFAULT 0,
    "is_required" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "list_fields_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."list_items" (
    "id" UUID NOT NULL,
    "list_id" UUID NOT NULL,
    "name" VARCHAR(255) NOT NULL,
    "field_values" JSONB NOT NULL DEFAULT '{}',
    "order_index" INTEGER NOT NULL DEFAULT 0,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completed_at" TIMESTAMPTZ,

    CONSTRAINT "list_items_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."list_item_comments" (
    "id" UUID NOT NULL,
    "list_item_id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "text" TEXT NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "list_item_comments_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."canvases" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "channel_id" UUID,
    "title" VARCHAR(200) NOT NULL,
    "content" JSONB NOT NULL DEFAULT '{}',
    "document_id" UUID NOT NULL,
    "version" INTEGER NOT NULL DEFAULT 1,
    "is_template" BOOLEAN NOT NULL DEFAULT false,
    "template_category" VARCHAR(50),
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "last_edited_by" UUID,

    CONSTRAINT "canvases_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."canvas_collaborators" (
    "canvas_id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "permission" VARCHAR(20) NOT NULL DEFAULT 'edit',
    "added_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "canvas_collaborators_pkey" PRIMARY KEY ("canvas_id","user_id")
);

-- CreateTable
CREATE TABLE "workspace"."templates" (
    "id" UUID NOT NULL,
    "workspace_id" UUID,
    "name" VARCHAR(120) NOT NULL,
    "description" TEXT,
    "category" VARCHAR(50),
    "content" JSONB NOT NULL,
    "type" VARCHAR(20) NOT NULL,
    "created_by" UUID,
    "is_custom" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "templates_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."huddles" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "channel_id" UUID NOT NULL,
    "started_by" UUID NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'active',
    "room_id" VARCHAR(255),
    "is_video" BOOLEAN NOT NULL DEFAULT false,
    "started_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "ended_at" TIMESTAMPTZ,
    "participant_count" INTEGER NOT NULL DEFAULT 0,
    "max_participants" INTEGER NOT NULL DEFAULT 0,

    CONSTRAINT "huddles_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."huddle_participants" (
    "id" UUID NOT NULL,
    "huddle_id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "joined_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "left_at" TIMESTAMPTZ,
    "had_video" BOOLEAN NOT NULL DEFAULT false,
    "had_audio" BOOLEAN NOT NULL DEFAULT true,
    "was_screensharing" BOOLEAN NOT NULL DEFAULT false,

    CONSTRAINT "huddle_participants_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."huddle_notes" (
    "id" UUID NOT NULL,
    "huddle_id" UUID NOT NULL,
    "transcript" TEXT,
    "summary" TEXT,
    "actionItems" JSONB,
    "key_takeaways" TEXT[],
    "generated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "huddle_notes_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."notification_preferences" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "channel_id" UUID,
    "notify_mentions" BOOLEAN NOT NULL DEFAULT true,
    "notify_all_messages" BOOLEAN NOT NULL DEFAULT false,
    "notify_threads" VARCHAR(20) NOT NULL DEFAULT 'all',
    "notify_dms" BOOLEAN NOT NULL DEFAULT true,
    "notify_huddles" BOOLEAN NOT NULL DEFAULT true,
    "push_enabled" BOOLEAN NOT NULL DEFAULT true,
    "email_enabled" BOOLEAN NOT NULL DEFAULT true,
    "email_frequency" VARCHAR(20) NOT NULL DEFAULT 'hourly',
    "desktop_enabled" BOOLEAN NOT NULL DEFAULT true,
    "mobile_enabled" BOOLEAN NOT NULL DEFAULT true,
    "sound_enabled" BOOLEAN NOT NULL DEFAULT true,
    "dnd_enabled" BOOLEAN NOT NULL DEFAULT false,
    "dnd_start" TIME,
    "dnd_end" TIME,
    "dnd_timezone" VARCHAR(50),
    "keywords" TEXT[],
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "notification_preferences_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."push_tokens" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "platform" VARCHAR(20) NOT NULL,
    "token" VARCHAR(500) NOT NULL,
    "device_id" VARCHAR(255),
    "app_version" VARCHAR(50),
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "push_tokens_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."data_retention_policies" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "scope" VARCHAR(20) NOT NULL,
    "channel_id" UUID,
    "message_retention_days" INTEGER,
    "file_retention_days" INTEGER,
    "retain_threads" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "data_retention_policies_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."legal_holds" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "name" VARCHAR(120) NOT NULL,
    "description" TEXT,
    "user_ids" UUID[],
    "channel_ids" UUID[],
    "start_date" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "end_date" TIMESTAMPTZ,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "legal_holds_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "workspace"."information_barriers" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "name" VARCHAR(120) NOT NULL,
    "group_a_type" VARCHAR(20) NOT NULL,
    "group_a_ids" UUID[],
    "group_b_type" VARCHAR(20) NOT NULL,
    "group_b_ids" UUID[],
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "is_active" BOOLEAN NOT NULL DEFAULT true,

    CONSTRAINT "information_barriers_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "messaging"."channels" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "name" VARCHAR(80) NOT NULL,
    "handle" VARCHAR(80) NOT NULL,
    "type" VARCHAR(20) NOT NULL,
    "topic" TEXT,
    "topic_set_by" UUID,
    "topic_set_at" TIMESTAMPTZ,
    "purpose" TEXT,
    "purpose_set_by" UUID,
    "purpose_set_at" TIMESTAMPTZ,
    "is_archived" BOOLEAN NOT NULL DEFAULT false,
    "archived_at" TIMESTAMPTZ,
    "archived_by" UUID,
    "is_default" BOOLEAN NOT NULL DEFAULT false,
    "is_read_only" BOOLEAN NOT NULL DEFAULT false,
    "is_shared" BOOLEAN NOT NULL DEFAULT false,
    "is_org_wide" BOOLEAN NOT NULL DEFAULT false,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "last_message_at" TIMESTAMPTZ,
    "member_count" INTEGER NOT NULL DEFAULT 0,

    CONSTRAINT "channels_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "messaging"."channel_members" (
    "id" UUID NOT NULL,
    "channel_id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "role" VARCHAR(20) NOT NULL DEFAULT 'member',
    "notification_pref" VARCHAR(20) NOT NULL DEFAULT 'all',
    "muted" BOOLEAN NOT NULL DEFAULT false,
    "muted_until" TIMESTAMPTZ,
    "last_read_ts" TIMESTAMPTZ,
    "unread_count" INTEGER NOT NULL DEFAULT 0,
    "unread_mention_count" INTEGER NOT NULL DEFAULT 0,
    "joined_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "invited_by" UUID,
    "left_at" TIMESTAMPTZ,

    CONSTRAINT "channel_members_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "messaging"."messages" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "channel_id" UUID NOT NULL,
    "user_id" UUID,
    "bot_id" UUID,
    "thread_id" UUID,
    "thread_root_id" UUID,
    "text" TEXT,
    "blocks" JSONB,
    "attachments" JSONB,
    "mentioned_users" UUID[],
    "mentioned_channels" UUID[],
    "mentioned_groups" UUID[],
    "link_previews" JSONB,
    "is_edited" BOOLEAN NOT NULL DEFAULT false,
    "edited_at" TIMESTAMPTZ,
    "edited_by" UUID,
    "is_deleted" BOOLEAN NOT NULL DEFAULT false,
    "deleted_at" TIMESTAMPTZ,
    "deleted_by" UUID,
    "is_pinned" BOOLEAN NOT NULL DEFAULT false,
    "is_system" BOOLEAN NOT NULL DEFAULT false,
    "system_type" VARCHAR(50),
    "is_scheduled" BOOLEAN NOT NULL DEFAULT false,
    "scheduled_for" TIMESTAMPTZ,
    "reply_count" INTEGER NOT NULL DEFAULT 0,
    "reply_users_count" INTEGER NOT NULL DEFAULT 0,
    "reply_users" UUID[],
    "latest_reply_ts" TIMESTAMPTZ,
    "ts" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "messages_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "messaging"."message_reactions" (
    "id" UUID NOT NULL,
    "message_id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "emoji" VARCHAR(50) NOT NULL,
    "emoji_type" VARCHAR(20) NOT NULL DEFAULT 'unicode',
    "custom_emoji_id" UUID,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "message_reactions_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "messaging"."message_pins" (
    "id" UUID NOT NULL,
    "channel_id" UUID NOT NULL,
    "message_id" UUID NOT NULL,
    "pinned_by" UUID NOT NULL,
    "pinned_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "message_pins_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "messaging"."channel_bookmarks" (
    "id" UUID NOT NULL,
    "channel_id" UUID NOT NULL,
    "type" VARCHAR(20) NOT NULL,
    "title" VARCHAR(120),
    "url" VARCHAR(500),
    "target_channel_id" UUID,
    "target_message_id" UUID,
    "emoji" VARCHAR(50),
    "order_index" INTEGER NOT NULL DEFAULT 0,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "channel_bookmarks_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "messaging"."saved_messages" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "message_id" UUID NOT NULL,
    "saved_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "saved_messages_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "messaging"."scheduled_messages" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "channel_id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "text" TEXT,
    "blocks" JSONB,
    "scheduled_for" TIMESTAMPTZ NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'pending',
    "sent_message_id" UUID,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "scheduled_messages_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "messaging"."shared_channels" (
    "id" UUID NOT NULL,
    "channel_id" UUID NOT NULL,
    "source_workspace_id" UUID NOT NULL,
    "target_workspace_id" UUID NOT NULL,
    "target_channel_name" VARCHAR(80),
    "target_channel_topic" TEXT,
    "target_is_private" BOOLEAN NOT NULL DEFAULT false,
    "status" VARCHAR(20) NOT NULL DEFAULT 'pending',
    "accepted_at" TIMESTAMPTZ,
    "accepted_by" UUID,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "shared_channels_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "files"."files" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "uploader_id" UUID NOT NULL,
    "filename" VARCHAR(255) NOT NULL,
    "file_type" VARCHAR(50) NOT NULL,
    "file_extension" VARCHAR(20),
    "mime_type" VARCHAR(100) NOT NULL,
    "size_bytes" BIGINT NOT NULL,
    "storage_key" VARCHAR(500) NOT NULL,
    "storage_bucket" VARCHAR(100) NOT NULL,
    "storage_provider" VARCHAR(20) NOT NULL DEFAULT 's3',
    "thumbnail_key" VARCHAR(500),
    "thumbnail_sizes" JSONB,
    "image_width" INTEGER,
    "image_height" INTEGER,
    "duration_seconds" DOUBLE PRECISION,
    "page_count" INTEGER,
    "is_public" BOOLEAN NOT NULL DEFAULT false,
    "public_url" VARCHAR(500),
    "is_deleted" BOOLEAN NOT NULL DEFAULT false,
    "deleted_at" TIMESTAMPTZ,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "files_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "files"."file_shares" (
    "id" UUID NOT NULL,
    "file_id" UUID NOT NULL,
    "channel_id" UUID,
    "message_id" UUID,
    "shared_by" UUID NOT NULL,
    "shared_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "file_shares_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "files"."file_comments" (
    "id" UUID NOT NULL,
    "file_id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "text" TEXT NOT NULL,
    "blocks" JSONB,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "is_deleted" BOOLEAN NOT NULL DEFAULT false,
    "deleted_at" TIMESTAMPTZ,

    CONSTRAINT "file_comments_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "automation"."workflows" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "name" VARCHAR(120) NOT NULL,
    "description" TEXT,
    "status" VARCHAR(20) NOT NULL DEFAULT 'draft',
    "trigger_type" VARCHAR(50) NOT NULL,
    "trigger_config" JSONB NOT NULL,
    "steps" JSONB NOT NULL DEFAULT '[]',
    "variables" JSONB DEFAULT '{}',
    "execution_count" INTEGER NOT NULL DEFAULT 0,
    "failure_count" INTEGER NOT NULL DEFAULT 0,
    "last_executed" TIMESTAMPTZ,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "workflows_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "automation"."workflow_executions" (
    "id" UUID NOT NULL,
    "workflow_id" UUID NOT NULL,
    "trigger_data" JSONB,
    "context" JSONB,
    "current_step" INTEGER NOT NULL DEFAULT 0,
    "status" VARCHAR(20) NOT NULL DEFAULT 'running',
    "error_message" TEXT,
    "started_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completed_at" TIMESTAMPTZ,

    CONSTRAINT "workflow_executions_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "automation"."apps" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "app_name" VARCHAR(80) NOT NULL,
    "app_slug" VARCHAR(80) NOT NULL,
    "description" TEXT,
    "icon_url" VARCHAR(500),
    "developer_name" VARCHAR(120),
    "is_internal" BOOLEAN NOT NULL DEFAULT false,
    "scopes" TEXT[],
    "oauth_token_encrypted" TEXT,
    "oauth_refresh_token_encrypted" TEXT,
    "installed_by" UUID NOT NULL,
    "installed_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "config" JSONB NOT NULL DEFAULT '{}',

    CONSTRAINT "apps_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "automation"."bot_users" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "app_id" UUID,
    "name" VARCHAR(80) NOT NULL,
    "display_name" VARCHAR(80) NOT NULL,
    "avatar_url" VARCHAR(500),
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "bot_users_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "automation"."slash_commands" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "app_id" UUID,
    "command" VARCHAR(50) NOT NULL,
    "description" TEXT,
    "usage_hint" VARCHAR(200),
    "url" VARCHAR(500),
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "slash_commands_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "automation"."incoming_webhooks" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "app_id" UUID,
    "channel_id" UUID,
    "webhook_url" VARCHAR(500) NOT NULL,
    "webhook_token" VARCHAR(255) NOT NULL,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "is_active" BOOLEAN NOT NULL DEFAULT true,

    CONSTRAINT "incoming_webhooks_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "automation"."event_subscriptions" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "app_id" UUID,
    "callback_url" VARCHAR(500) NOT NULL,
    "event_types" TEXT[],
    "secret" VARCHAR(255) NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "event_subscriptions_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "analytics"."audit_logs" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "actor_id" UUID,
    "actor_type" VARCHAR(20) NOT NULL DEFAULT 'user',
    "action" VARCHAR(80) NOT NULL,
    "entity_type" VARCHAR(50),
    "entity_id" UUID,
    "details" JSONB,
    "ip_address" VARCHAR(45),
    "user_agent" TEXT,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "audit_logs_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "analytics"."usage_stats" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "stat_date" DATE NOT NULL,
    "daily_active_users" INTEGER NOT NULL DEFAULT 0,
    "weekly_active_users" INTEGER NOT NULL DEFAULT 0,
    "monthly_active_users" INTEGER NOT NULL DEFAULT 0,
    "messages_sent" INTEGER NOT NULL DEFAULT 0,
    "channels_active" INTEGER NOT NULL DEFAULT 0,
    "huddles_count" INTEGER NOT NULL DEFAULT 0,
    "huddle_minutes" INTEGER NOT NULL DEFAULT 0,
    "files_uploaded" INTEGER NOT NULL DEFAULT 0,
    "apps_active" INTEGER NOT NULL DEFAULT 0,
    "workflows_executed" INTEGER NOT NULL DEFAULT 0,
    "ai_queries" INTEGER NOT NULL DEFAULT 0,

    CONSTRAINT "usage_stats_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "analytics"."message_activity" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "channel_id" UUID NOT NULL,
    "stat_date" DATE NOT NULL,
    "messages_count" INTEGER NOT NULL DEFAULT 0,
    "unique_posters" INTEGER NOT NULL DEFAULT 0,
    "reactions_count" INTEGER NOT NULL DEFAULT 0,
    "thread_replies" INTEGER NOT NULL DEFAULT 0,

    CONSTRAINT "message_activity_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "ai"."message_embeddings" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "message_id" UUID NOT NULL,
    "channel_id" UUID NOT NULL,
    "content" TEXT,
    "embedding" vector(1536) NOT NULL,
    "model" VARCHAR(50) NOT NULL DEFAULT 'text-embedding-3-small',
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "message_embeddings_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "ai"."ai_summaries" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "type" VARCHAR(30) NOT NULL,
    "target_id" UUID NOT NULL,
    "summary" TEXT NOT NULL,
    "key_points" TEXT[],
    "action_items" JSONB,
    "period_start" TIMESTAMPTZ,
    "period_end" TIMESTAMPTZ,
    "generated_for" UUID NOT NULL,
    "model" VARCHAR(50) NOT NULL DEFAULT 'gpt-4o',
    "tokens_used" INTEGER,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "ai_summaries_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "ai"."ai_conversations" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "messages" JSONB NOT NULL DEFAULT '[]',
    "context_refs" UUID[],
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "ai_conversations_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "ai"."ai_search_cache" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "query" TEXT NOT NULL,
    "answer" TEXT NOT NULL,
    "source_message_ids" UUID[],
    "model" VARCHAR(50),
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "ai_search_cache_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "auth"."users"("email");

-- CreateIndex
CREATE INDEX "idx_users_email" ON "auth"."users"("email");

-- CreateIndex
CREATE INDEX "idx_users_last_active" ON "auth"."users"("last_active_at");

-- CreateIndex
CREATE INDEX "idx_user_identities_user" ON "auth"."user_identities"("user_id");

-- CreateIndex
CREATE INDEX "idx_user_identities_provider" ON "auth"."user_identities"("provider", "provider_user_id");

-- CreateIndex
CREATE UNIQUE INDEX "user_identities_provider_provider_user_id_key" ON "auth"."user_identities"("provider", "provider_user_id");

-- CreateIndex
CREATE UNIQUE INDEX "sessions_token_hash_key" ON "auth"."sessions"("token_hash");

-- CreateIndex
CREATE INDEX "idx_sessions_user" ON "auth"."sessions"("user_id");

-- CreateIndex
CREATE INDEX "idx_sessions_expires" ON "auth"."sessions"("expires_at");

-- CreateIndex
CREATE UNIQUE INDEX "two_factor_auth_user_id_key" ON "auth"."two_factor_auth"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "password_resets_token_hash_key" ON "auth"."password_resets"("token_hash");

-- CreateIndex
CREATE INDEX "idx_password_resets_user" ON "auth"."password_resets"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "workspaces_domain_key" ON "workspace"."workspaces"("domain");

-- CreateIndex
CREATE INDEX "idx_workspaces_domain" ON "workspace"."workspaces"("domain");

-- CreateIndex
CREATE INDEX "idx_workspace_members_workspace" ON "workspace"."workspace_members"("workspace_id");

-- CreateIndex
CREATE INDEX "idx_workspace_members_user" ON "workspace"."workspace_members"("user_id");

-- CreateIndex
CREATE INDEX "idx_workspace_members_role" ON "workspace"."workspace_members"("workspace_id", "role");

-- CreateIndex
CREATE UNIQUE INDEX "workspace_members_workspace_id_user_id_key" ON "workspace"."workspace_members"("workspace_id", "user_id");

-- CreateIndex
CREATE UNIQUE INDEX "workspace_invitations_token_key" ON "workspace"."workspace_invitations"("token");

-- CreateIndex
CREATE INDEX "idx_invitations_workspace" ON "workspace"."workspace_invitations"("workspace_id");

-- CreateIndex
CREATE INDEX "idx_invitations_email" ON "workspace"."workspace_invitations"("email");

-- CreateIndex
CREATE INDEX "idx_user_groups_workspace" ON "workspace"."user_groups"("workspace_id");

-- CreateIndex
CREATE UNIQUE INDEX "user_groups_workspace_id_handle_key" ON "workspace"."user_groups"("workspace_id", "handle");

-- CreateIndex
CREATE INDEX "idx_custom_emojis_workspace" ON "workspace"."custom_emojis"("workspace_id");

-- CreateIndex
CREATE UNIQUE INDEX "custom_emojis_workspace_id_name_key" ON "workspace"."custom_emojis"("workspace_id", "name");

-- CreateIndex
CREATE INDEX "idx_lists_workspace" ON "workspace"."lists"("workspace_id");

-- CreateIndex
CREATE INDEX "idx_lists_channel" ON "workspace"."lists"("channel_id");

-- CreateIndex
CREATE INDEX "idx_list_fields_list" ON "workspace"."list_fields"("list_id", "order_index");

-- CreateIndex
CREATE INDEX "idx_list_items_list" ON "workspace"."list_items"("list_id", "order_index");

-- CreateIndex
CREATE INDEX "idx_list_item_comments_item" ON "workspace"."list_item_comments"("list_item_id", "created_at");

-- CreateIndex
CREATE INDEX "idx_canvases_workspace" ON "workspace"."canvases"("workspace_id");

-- CreateIndex
CREATE INDEX "idx_canvases_channel" ON "workspace"."canvases"("channel_id");

-- CreateIndex
CREATE INDEX "idx_templates_workspace" ON "workspace"."templates"("workspace_id", "category");

-- CreateIndex
CREATE INDEX "idx_huddles_channel_active" ON "workspace"."huddles"("channel_id");

-- CreateIndex
CREATE INDEX "idx_huddle_participants_huddle" ON "workspace"."huddle_participants"("huddle_id");

-- CreateIndex
CREATE UNIQUE INDEX "huddle_participants_huddle_id_user_id_key" ON "workspace"."huddle_participants"("huddle_id", "user_id");

-- CreateIndex
CREATE INDEX "idx_notif_prefs_user_workspace" ON "workspace"."notification_preferences"("user_id", "workspace_id");

-- CreateIndex
CREATE INDEX "idx_notif_prefs_channel" ON "workspace"."notification_preferences"("channel_id");

-- CreateIndex
CREATE INDEX "idx_push_tokens_user" ON "workspace"."push_tokens"("user_id");

-- CreateIndex
CREATE INDEX "idx_channels_workspace" ON "messaging"."channels"("workspace_id", "type");

-- CreateIndex
CREATE INDEX "idx_channels_workspace_handle" ON "messaging"."channels"("workspace_id", "handle");

-- CreateIndex
CREATE INDEX "idx_channels_archived" ON "messaging"."channels"("workspace_id", "is_archived");

-- CreateIndex
CREATE INDEX "idx_channels_last_message" ON "messaging"."channels"("workspace_id", "last_message_at");

-- CreateIndex
CREATE UNIQUE INDEX "channels_workspace_id_handle_key" ON "messaging"."channels"("workspace_id", "handle");

-- CreateIndex
CREATE INDEX "idx_channel_members_user" ON "messaging"."channel_members"("user_id", "workspace_id");

-- CreateIndex
CREATE INDEX "idx_channel_members_channel" ON "messaging"."channel_members"("channel_id");

-- CreateIndex
CREATE INDEX "idx_channel_members_unread" ON "messaging"."channel_members"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "channel_members_channel_id_user_id_key" ON "messaging"."channel_members"("channel_id", "user_id");

-- CreateIndex
CREATE INDEX "idx_messages_channel_ts" ON "messaging"."messages"("channel_id", "ts");

-- CreateIndex
CREATE INDEX "idx_messages_workspace" ON "messaging"."messages"("workspace_id", "ts");

-- CreateIndex
CREATE INDEX "idx_messages_thread" ON "messaging"."messages"("thread_root_id", "ts");

-- CreateIndex
CREATE INDEX "idx_messages_user" ON "messaging"."messages"("user_id", "ts");

-- CreateIndex
CREATE INDEX "idx_messages_scheduled" ON "messaging"."messages"("is_scheduled", "scheduled_for");

-- CreateIndex
CREATE INDEX "idx_reactions_message" ON "messaging"."message_reactions"("message_id");

-- CreateIndex
CREATE INDEX "idx_reactions_user" ON "messaging"."message_reactions"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "message_reactions_message_id_user_id_emoji_key" ON "messaging"."message_reactions"("message_id", "user_id", "emoji");

-- CreateIndex
CREATE UNIQUE INDEX "message_pins_channel_id_message_id_key" ON "messaging"."message_pins"("channel_id", "message_id");

-- CreateIndex
CREATE INDEX "idx_bookmarks_channel" ON "messaging"."channel_bookmarks"("channel_id", "order_index");

-- CreateIndex
CREATE INDEX "idx_saved_messages_user" ON "messaging"."saved_messages"("user_id", "saved_at");

-- CreateIndex
CREATE UNIQUE INDEX "saved_messages_user_id_message_id_key" ON "messaging"."saved_messages"("user_id", "message_id");

-- CreateIndex
CREATE INDEX "idx_scheduled_pending" ON "messaging"."scheduled_messages"("scheduled_for");

-- CreateIndex
CREATE INDEX "idx_shared_channels_source" ON "messaging"."shared_channels"("source_workspace_id");

-- CreateIndex
CREATE INDEX "idx_shared_channels_target" ON "messaging"."shared_channels"("target_workspace_id");

-- CreateIndex
CREATE INDEX "idx_files_workspace" ON "files"."files"("workspace_id", "created_at");

-- CreateIndex
CREATE INDEX "idx_files_uploader" ON "files"."files"("uploader_id", "created_at");

-- CreateIndex
CREATE INDEX "idx_files_type" ON "files"."files"("workspace_id", "file_type");

-- CreateIndex
CREATE INDEX "idx_file_shares_file" ON "files"."file_shares"("file_id");

-- CreateIndex
CREATE INDEX "idx_file_shares_channel" ON "files"."file_shares"("channel_id");

-- CreateIndex
CREATE INDEX "idx_file_comments_file" ON "files"."file_comments"("file_id", "created_at");

-- CreateIndex
CREATE INDEX "idx_workflows_workspace" ON "automation"."workflows"("workspace_id", "status");

-- CreateIndex
CREATE INDEX "idx_workflow_exec_workflow" ON "automation"."workflow_executions"("workflow_id", "started_at");

-- CreateIndex
CREATE INDEX "idx_workflow_exec_status" ON "automation"."workflow_executions"("status");

-- CreateIndex
CREATE INDEX "idx_apps_workspace" ON "automation"."apps"("workspace_id", "is_active");

-- CreateIndex
CREATE UNIQUE INDEX "apps_workspace_id_app_slug_key" ON "automation"."apps"("workspace_id", "app_slug");

-- CreateIndex
CREATE INDEX "idx_bot_users_workspace" ON "automation"."bot_users"("workspace_id");

-- CreateIndex
CREATE UNIQUE INDEX "slash_commands_workspace_id_command_key" ON "automation"."slash_commands"("workspace_id", "command");

-- CreateIndex
CREATE UNIQUE INDEX "incoming_webhooks_webhook_url_key" ON "automation"."incoming_webhooks"("webhook_url");

-- CreateIndex
CREATE INDEX "idx_audit_workspace" ON "analytics"."audit_logs"("workspace_id", "created_at");

-- CreateIndex
CREATE INDEX "idx_audit_actor" ON "analytics"."audit_logs"("actor_id", "created_at");

-- CreateIndex
CREATE INDEX "idx_audit_action" ON "analytics"."audit_logs"("workspace_id", "action");

-- CreateIndex
CREATE INDEX "idx_audit_entity" ON "analytics"."audit_logs"("entity_type", "entity_id");

-- CreateIndex
CREATE INDEX "idx_usage_workspace_date" ON "analytics"."usage_stats"("workspace_id", "stat_date");

-- CreateIndex
CREATE UNIQUE INDEX "usage_stats_workspace_id_stat_date_key" ON "analytics"."usage_stats"("workspace_id", "stat_date");

-- CreateIndex
CREATE INDEX "idx_msg_activity_workspace" ON "analytics"."message_activity"("workspace_id", "stat_date");

-- CreateIndex
CREATE UNIQUE INDEX "message_activity_channel_id_stat_date_key" ON "analytics"."message_activity"("channel_id", "stat_date");

-- CreateIndex
CREATE INDEX "idx_embeddings_workspace" ON "ai"."message_embeddings"("workspace_id");

-- CreateIndex
CREATE INDEX "idx_embeddings_channel" ON "ai"."message_embeddings"("channel_id");

-- CreateIndex
CREATE INDEX "idx_ai_summaries_workspace" ON "ai"."ai_summaries"("workspace_id", "type", "created_at");

-- CreateIndex
CREATE INDEX "idx_ai_summaries_target" ON "ai"."ai_summaries"("target_id", "created_at");

-- CreateIndex
CREATE INDEX "idx_ai_conversations_user" ON "ai"."ai_conversations"("user_id", "updated_at");

-- CreateIndex
CREATE INDEX "idx_ai_search_workspace" ON "ai"."ai_search_cache"("workspace_id", "created_at");

-- AddForeignKey
ALTER TABLE "auth"."user_identities" ADD CONSTRAINT "user_identities_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "auth"."sessions" ADD CONSTRAINT "sessions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "auth"."two_factor_auth" ADD CONSTRAINT "two_factor_auth_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "auth"."password_resets" ADD CONSTRAINT "password_resets_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."workspaces" ADD CONSTRAINT "workspaces_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."workspace_members" ADD CONSTRAINT "workspace_members_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."workspace_members" ADD CONSTRAINT "workspace_members_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."workspace_members" ADD CONSTRAINT "workspace_members_invited_by_fkey" FOREIGN KEY ("invited_by") REFERENCES "auth"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."workspace_members" ADD CONSTRAINT "workspace_members_deactivated_by_fkey" FOREIGN KEY ("deactivated_by") REFERENCES "auth"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."workspace_invitations" ADD CONSTRAINT "workspace_invitations_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."workspace_invitations" ADD CONSTRAINT "workspace_invitations_invited_by_fkey" FOREIGN KEY ("invited_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."user_groups" ADD CONSTRAINT "user_groups_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."user_groups" ADD CONSTRAINT "user_groups_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."user_group_members" ADD CONSTRAINT "user_group_members_user_group_id_fkey" FOREIGN KEY ("user_group_id") REFERENCES "workspace"."user_groups"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."user_group_members" ADD CONSTRAINT "user_group_members_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."custom_emojis" ADD CONSTRAINT "custom_emojis_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."custom_emojis" ADD CONSTRAINT "custom_emojis_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."workspace_settings" ADD CONSTRAINT "workspace_settings_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."lists" ADD CONSTRAINT "lists_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."lists" ADD CONSTRAINT "lists_channel_id_fkey" FOREIGN KEY ("channel_id") REFERENCES "messaging"."channels"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."lists" ADD CONSTRAINT "lists_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."list_fields" ADD CONSTRAINT "list_fields_list_id_fkey" FOREIGN KEY ("list_id") REFERENCES "workspace"."lists"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."list_items" ADD CONSTRAINT "list_items_list_id_fkey" FOREIGN KEY ("list_id") REFERENCES "workspace"."lists"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."list_items" ADD CONSTRAINT "list_items_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."list_item_comments" ADD CONSTRAINT "list_item_comments_list_item_id_fkey" FOREIGN KEY ("list_item_id") REFERENCES "workspace"."list_items"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."list_item_comments" ADD CONSTRAINT "list_item_comments_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."canvases" ADD CONSTRAINT "canvases_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."canvases" ADD CONSTRAINT "canvases_channel_id_fkey" FOREIGN KEY ("channel_id") REFERENCES "messaging"."channels"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."canvases" ADD CONSTRAINT "canvases_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."canvases" ADD CONSTRAINT "canvases_last_edited_by_fkey" FOREIGN KEY ("last_edited_by") REFERENCES "auth"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."canvas_collaborators" ADD CONSTRAINT "canvas_collaborators_canvas_id_fkey" FOREIGN KEY ("canvas_id") REFERENCES "workspace"."canvases"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."canvas_collaborators" ADD CONSTRAINT "canvas_collaborators_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."templates" ADD CONSTRAINT "templates_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."templates" ADD CONSTRAINT "templates_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "auth"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."huddles" ADD CONSTRAINT "huddles_started_by_fkey" FOREIGN KEY ("started_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."huddles" ADD CONSTRAINT "huddles_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."huddles" ADD CONSTRAINT "huddles_channel_id_fkey" FOREIGN KEY ("channel_id") REFERENCES "messaging"."channels"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."huddle_participants" ADD CONSTRAINT "huddle_participants_huddle_id_fkey" FOREIGN KEY ("huddle_id") REFERENCES "workspace"."huddles"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."huddle_participants" ADD CONSTRAINT "huddle_participants_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."huddle_notes" ADD CONSTRAINT "huddle_notes_huddle_id_fkey" FOREIGN KEY ("huddle_id") REFERENCES "workspace"."huddles"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."notification_preferences" ADD CONSTRAINT "notification_preferences_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."notification_preferences" ADD CONSTRAINT "notification_preferences_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."notification_preferences" ADD CONSTRAINT "notification_preferences_channel_id_fkey" FOREIGN KEY ("channel_id") REFERENCES "messaging"."channels"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."push_tokens" ADD CONSTRAINT "push_tokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."data_retention_policies" ADD CONSTRAINT "data_retention_policies_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."data_retention_policies" ADD CONSTRAINT "data_retention_policies_channel_id_fkey" FOREIGN KEY ("channel_id") REFERENCES "messaging"."channels"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."legal_holds" ADD CONSTRAINT "legal_holds_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."legal_holds" ADD CONSTRAINT "legal_holds_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."information_barriers" ADD CONSTRAINT "information_barriers_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "workspace"."information_barriers" ADD CONSTRAINT "information_barriers_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."channels" ADD CONSTRAINT "channels_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."channels" ADD CONSTRAINT "channels_topic_set_by_fkey" FOREIGN KEY ("topic_set_by") REFERENCES "auth"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."channels" ADD CONSTRAINT "channels_purpose_set_by_fkey" FOREIGN KEY ("purpose_set_by") REFERENCES "auth"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."channels" ADD CONSTRAINT "channels_archived_by_fkey" FOREIGN KEY ("archived_by") REFERENCES "auth"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."channel_members" ADD CONSTRAINT "channel_members_channel_id_fkey" FOREIGN KEY ("channel_id") REFERENCES "messaging"."channels"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."channel_members" ADD CONSTRAINT "channel_members_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."channel_members" ADD CONSTRAINT "channel_members_invited_by_fkey" FOREIGN KEY ("invited_by") REFERENCES "auth"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."messages" ADD CONSTRAINT "messages_channel_id_fkey" FOREIGN KEY ("channel_id") REFERENCES "messaging"."channels"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."messages" ADD CONSTRAINT "messages_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."messages" ADD CONSTRAINT "messages_edited_by_fkey" FOREIGN KEY ("edited_by") REFERENCES "auth"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."messages" ADD CONSTRAINT "messages_deleted_by_fkey" FOREIGN KEY ("deleted_by") REFERENCES "auth"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."message_reactions" ADD CONSTRAINT "message_reactions_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "messaging"."messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."message_reactions" ADD CONSTRAINT "message_reactions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."message_pins" ADD CONSTRAINT "message_pins_channel_id_fkey" FOREIGN KEY ("channel_id") REFERENCES "messaging"."channels"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."message_pins" ADD CONSTRAINT "message_pins_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "messaging"."messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."message_pins" ADD CONSTRAINT "message_pins_pinned_by_fkey" FOREIGN KEY ("pinned_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."channel_bookmarks" ADD CONSTRAINT "channel_bookmarks_channel_id_fkey" FOREIGN KEY ("channel_id") REFERENCES "messaging"."channels"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."channel_bookmarks" ADD CONSTRAINT "channel_bookmarks_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."saved_messages" ADD CONSTRAINT "saved_messages_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."saved_messages" ADD CONSTRAINT "saved_messages_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "messaging"."messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."scheduled_messages" ADD CONSTRAINT "scheduled_messages_channel_id_fkey" FOREIGN KEY ("channel_id") REFERENCES "messaging"."channels"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."scheduled_messages" ADD CONSTRAINT "scheduled_messages_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."shared_channels" ADD CONSTRAINT "shared_channels_channel_id_fkey" FOREIGN KEY ("channel_id") REFERENCES "messaging"."channels"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."shared_channels" ADD CONSTRAINT "shared_channels_accepted_by_fkey" FOREIGN KEY ("accepted_by") REFERENCES "auth"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messaging"."shared_channels" ADD CONSTRAINT "shared_channels_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "files"."files" ADD CONSTRAINT "files_uploader_id_fkey" FOREIGN KEY ("uploader_id") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "files"."file_shares" ADD CONSTRAINT "file_shares_file_id_fkey" FOREIGN KEY ("file_id") REFERENCES "files"."files"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "files"."file_shares" ADD CONSTRAINT "file_shares_channel_id_fkey" FOREIGN KEY ("channel_id") REFERENCES "messaging"."channels"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "files"."file_shares" ADD CONSTRAINT "file_shares_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "messaging"."messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "files"."file_shares" ADD CONSTRAINT "file_shares_shared_by_fkey" FOREIGN KEY ("shared_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "files"."file_comments" ADD CONSTRAINT "file_comments_file_id_fkey" FOREIGN KEY ("file_id") REFERENCES "files"."files"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "files"."file_comments" ADD CONSTRAINT "file_comments_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "automation"."workflows" ADD CONSTRAINT "workflows_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "automation"."workflows" ADD CONSTRAINT "workflows_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "automation"."workflow_executions" ADD CONSTRAINT "workflow_executions_workflow_id_fkey" FOREIGN KEY ("workflow_id") REFERENCES "automation"."workflows"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "automation"."apps" ADD CONSTRAINT "apps_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "automation"."apps" ADD CONSTRAINT "apps_installed_by_fkey" FOREIGN KEY ("installed_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "automation"."bot_users" ADD CONSTRAINT "bot_users_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "automation"."bot_users" ADD CONSTRAINT "bot_users_app_id_fkey" FOREIGN KEY ("app_id") REFERENCES "automation"."apps"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "automation"."slash_commands" ADD CONSTRAINT "slash_commands_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "automation"."slash_commands" ADD CONSTRAINT "slash_commands_app_id_fkey" FOREIGN KEY ("app_id") REFERENCES "automation"."apps"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "automation"."incoming_webhooks" ADD CONSTRAINT "incoming_webhooks_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "automation"."incoming_webhooks" ADD CONSTRAINT "incoming_webhooks_app_id_fkey" FOREIGN KEY ("app_id") REFERENCES "automation"."apps"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "automation"."incoming_webhooks" ADD CONSTRAINT "incoming_webhooks_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "automation"."event_subscriptions" ADD CONSTRAINT "event_subscriptions_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspace"."workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "automation"."event_subscriptions" ADD CONSTRAINT "event_subscriptions_app_id_fkey" FOREIGN KEY ("app_id") REFERENCES "automation"."apps"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "analytics"."audit_logs" ADD CONSTRAINT "audit_logs_actor_id_fkey" FOREIGN KEY ("actor_id") REFERENCES "auth"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ai"."message_embeddings" ADD CONSTRAINT "message_embeddings_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "messaging"."messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ai"."ai_summaries" ADD CONSTRAINT "ai_summaries_generated_for_fkey" FOREIGN KEY ("generated_for") REFERENCES "auth"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ai"."ai_conversations" ADD CONSTRAINT "ai_conversations_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

