-- Impact Analysis Schema
-- Tracks system components, dependencies, and verification checks

-- Components registry
CREATE TABLE IF NOT EXISTS components (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    type TEXT NOT NULL CHECK (type IN ('service', 'script', 'config', 'database', 'api', 'ui', 'library')),
    path TEXT,
    repo TEXT,
    description TEXT,
    health_check TEXT,
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);

-- Component dependencies (directed graph)
CREATE TABLE IF NOT EXISTS component_dependencies (
    id SERIAL PRIMARY KEY,
    component_id TEXT NOT NULL REFERENCES components(id) ON DELETE CASCADE,
    depends_on TEXT NOT NULL REFERENCES components(id) ON DELETE CASCADE,
    dependency_type TEXT NOT NULL CHECK (dependency_type IN ('hard', 'soft', 'config', 'data')),
    description TEXT,
    created_at TIMESTAMP DEFAULT NOW(),
    UNIQUE(component_id, depends_on)
);

-- File-to-component mapping (for git diff analysis)
CREATE TABLE IF NOT EXISTS component_files (
    id SERIAL PRIMARY KEY,
    component_id TEXT NOT NULL REFERENCES components(id) ON DELETE CASCADE,
    file_pattern TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT NOW(),
    UNIQUE(component_id, file_pattern)
);

-- Verification checks per component
CREATE TABLE IF NOT EXISTS verification_checks (
    id SERIAL PRIMARY KEY,
    component_id TEXT NOT NULL REFERENCES components(id) ON DELETE CASCADE,
    name TEXT NOT NULL,
    check_type TEXT NOT NULL CHECK (check_type IN ('command', 'http', 'tcp', 'file')),
    check_command TEXT NOT NULL,
    expected_result TEXT,
    timeout_seconds INTEGER DEFAULT 30,
    created_at TIMESTAMP DEFAULT NOW()
);

-- Historical change impacts (learned from errors)
CREATE TABLE IF NOT EXISTS change_impacts (
    id SERIAL PRIMARY KEY,
    changed_component TEXT NOT NULL REFERENCES components(id) ON DELETE CASCADE,
    affected_component TEXT NOT NULL REFERENCES components(id) ON DELETE CASCADE,
    impact_description TEXT NOT NULL,
    error_id TEXT,
    task_id TEXT,
    learned_at TIMESTAMP DEFAULT NOW()
);

-- Impact analysis runs (audit log)
CREATE TABLE IF NOT EXISTS impact_analysis_runs (
    id SERIAL PRIMARY KEY,
    task_id TEXT,
    triggered_by TEXT NOT NULL CHECK (triggered_by IN ('task_close', 'manual', 'git_push')),
    components_analyzed INTEGER DEFAULT 0,
    issues_found INTEGER DEFAULT 0,
    verification_passed BOOLEAN,
    details JSONB,
    created_at TIMESTAMP DEFAULT NOW()
);

-- Indexes for performance
CREATE INDEX IF NOT EXISTS idx_component_deps_component ON component_dependencies(component_id);
CREATE INDEX IF NOT EXISTS idx_component_deps_depends ON component_dependencies(depends_on);
CREATE INDEX IF NOT EXISTS idx_component_files_component ON component_files(component_id);
CREATE INDEX IF NOT EXISTS idx_change_impacts_changed ON change_impacts(changed_component);
CREATE INDEX IF NOT EXISTS idx_change_impacts_affected ON change_impacts(affected_component);
CREATE INDEX IF NOT EXISTS idx_impact_runs_task ON impact_analysis_runs(task_id);
