-- Integrations catalog bootstrap schema for compose environments.
-- Creates the EF-backed integrations table when running without EF migrations.

CREATE TABLE IF NOT EXISTS integrations
(
    id UUID PRIMARY KEY,
    name VARCHAR(256) NOT NULL,
    description VARCHAR(1024),
    type INTEGER NOT NULL,
    provider INTEGER NOT NULL,
    status INTEGER NOT NULL,
    endpoint VARCHAR(2048) NOT NULL,
    auth_ref_uri VARCHAR(1024),
    organization_id VARCHAR(256),
    config_json JSONB,
    last_health_status INTEGER NOT NULL DEFAULT 0,
    last_health_check_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL,
    updated_at TIMESTAMPTZ NOT NULL,
    created_by VARCHAR(256),
    updated_by VARCHAR(256),
    tenant_id VARCHAR(128),
    tags JSONB,
    is_deleted BOOLEAN NOT NULL DEFAULT FALSE
);

CREATE INDEX IF NOT EXISTS ix_integrations_type
    ON integrations (type);

CREATE INDEX IF NOT EXISTS ix_integrations_provider
    ON integrations (provider);

CREATE INDEX IF NOT EXISTS ix_integrations_status
    ON integrations (status);

CREATE INDEX IF NOT EXISTS ix_integrations_tenant
    ON integrations (tenant_id);

CREATE UNIQUE INDEX IF NOT EXISTS ix_integrations_tenant_name_active
    ON integrations (tenant_id, name)
    WHERE is_deleted = FALSE;
