-- 0004_playground_fixtures.sql — saved Playground inputs for V5 (Studio).
-- Spec: docs/arquitectura/specs/stackbone-agent-protocol-v1.md §6.10.
-- Task: docs/tasks/Studio V5 - Playground e Invoke.md (V5#03).
--
-- The table holds JSON inputs the creator pinned for repeated invocations
-- from the Studio Playground. Cloud-only — local fixtures live in
-- `~/.stackbone/dev/fixtures/<agentId>.jsonl` for filesystem parity with the
-- rest of the local datapath. Workspace-shared fixtures and version history
-- land in V1+ (today MVP only stores the owner's saved inputs).
--
-- The `(agent_id, name)` unique constraint backs the `409 fixture_name_conflict`
-- response defined in the protocol spec — the partial index keeps the
-- agentless rows (single-agent local emulator parity) from blocking each
-- other on `name`.

CREATE TABLE IF NOT EXISTS stackbone_platform.playground_fixtures (
    id              uuid        PRIMARY KEY DEFAULT gen_random_uuid(),
    -- Nullable in local (single-agent emulator). In cloud the saga sets it
    -- to the install's agent_id so different agents in the same workspace
    -- can reuse fixture names.
    agent_id        text,
    name            text        NOT NULL,
    input_json      jsonb       NOT NULL,
    description     text,
    -- The owner's better-auth user id at save time. Kept text to match the
    -- existing `workspaces.owner_user_id` column type.
    created_by      text,
    created_at      timestamptz NOT NULL DEFAULT now()
);

CREATE UNIQUE INDEX IF NOT EXISTS playground_fixtures_agent_name_uq
    ON stackbone_platform.playground_fixtures (agent_id, name)
    WHERE agent_id IS NOT NULL;

CREATE UNIQUE INDEX IF NOT EXISTS playground_fixtures_name_uq
    ON stackbone_platform.playground_fixtures (name)
    WHERE agent_id IS NULL;

CREATE INDEX IF NOT EXISTS playground_fixtures_created_at_idx
    ON stackbone_platform.playground_fixtures (created_at DESC, id DESC);
