-- Migration 004: Create prompt dependencies table
-- Implements FR-6: Dependency Tracking
-- FR-6.1: Directed dependency edges between artifacts
-- FR-6.2: Cross-space dependency graph
-- Date: 2026-02-08

-- Prompt dependency edges table
CREATE TABLE IF NOT EXISTS prompt_dependencies (
    id TEXT PRIMARY KEY,
    source_artifact_id TEXT NOT NULL,
    target_artifact_id TEXT NOT NULL,
    run_id TEXT NOT NULL,
    edge_type TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(source_artifact_id, target_artifact_id, run_id)
);

-- Note: Foreign keys documented but not enforced (matches existing pattern)
-- source_artifact_id REFERENCES prompt_artifacts(id)
-- target_artifact_id REFERENCES prompt_artifacts(id)
-- run_id REFERENCES prompt_runs(id)

-- Indexes for performance
CREATE INDEX IF NOT EXISTS idx_deps_source ON prompt_dependencies(source_artifact_id);
CREATE INDEX IF NOT EXISTS idx_deps_target ON prompt_dependencies(target_artifact_id);
CREATE INDEX IF NOT EXISTS idx_deps_run ON prompt_dependencies(run_id);
CREATE INDEX IF NOT EXISTS idx_deps_type ON prompt_dependencies(edge_type);
CREATE INDEX IF NOT EXISTS idx_deps_source_target ON prompt_dependencies(source_artifact_id, target_artifact_id);

-- Column documentation:
-- id: Unique UUID for this dependency edge
-- source_artifact_id: Artifact that depends on the target
-- target_artifact_id: Artifact that is depended upon
-- run_id: The execution run that established this dependency
-- edge_type: requires, generates, includes
-- created_at: When this dependency was recorded
