-- =============================================================================
-- Migration 002: SDK States Table
--
-- Persistent storage for SDK state management.
-- Replaces the in-memory store used during development.
-- =============================================================================

CREATE TABLE IF NOT EXISTS sdk_states (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id VARCHAR(255) NOT NULL UNIQUE,
    user_id VARCHAR(255),
    state JSONB NOT NULL,
    version INTEGER DEFAULT 1,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_sdk_states_tenant ON sdk_states(tenant_id);
CREATE INDEX IF NOT EXISTS idx_sdk_states_updated ON sdk_states(updated_at);
