-- NocoDB Schema for Self-Correcting Memory Architecture
-- Tables: corrections, memory_preferences, memory_episodes, memory_decisions

-- ============================================
-- TABLE: corrections
-- Stores known mistakes and auto-detected patterns
-- ============================================
CREATE TABLE IF NOT EXISTS corrections (
    id VARCHAR(50) PRIMARY KEY,
    pattern VARCHAR(500) NOT NULL,           -- Regex pattern to match
    exclude_pattern VARCHAR(500),            -- Pattern that excludes match
    severity VARCHAR(20) NOT NULL,           -- error, warning, auto-correct
    message VARCHAR(500) NOT NULL,           -- What to tell user
    suggestion VARCHAR(500),                 -- How to fix
    blocking BOOLEAN DEFAULT TRUE,           -- Block output if matched?
    auto_detected BOOLEAN DEFAULT FALSE,     -- Was this auto-detected?
    manual_entry BOOLEAN DEFAULT FALSE,      -- Was this manually added?
    hit_count INTEGER DEFAULT 0,             -- How many times triggered
    last_triggered TIMESTAMP,                -- Last time this fired
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    context VARCHAR(200)                     -- Task/workflow context
);

-- Indexes for fast lookup
CREATE INDEX IF NOT EXISTS idx_corrections_severity ON corrections(severity);
CREATE INDEX IF NOT EXISTS idx_corrections_blocking ON corrections(blocking);
CREATE INDEX IF NOT EXISTS idx_corrections_auto ON corrections(auto_detected);

-- ============================================
-- TABLE: memory_preferences
-- Persistent user preferences (Layer 2)
-- ============================================
CREATE TABLE IF NOT EXISTS memory_preferences (
    id VARCHAR(50) PRIMARY KEY,
    category VARCHAR(50) NOT NULL,           -- identity, pref, goal, knowledge
    key VARCHAR(100) NOT NULL,
    value TEXT NOT NULL,
    importance INTEGER DEFAULT 5,            -- 1-10 scale
    confidence FLOAT DEFAULT 1.0,            -- 0.0-1.0
    tags JSON,                               -- Array of tags
    access_count INTEGER DEFAULT 0,          -- How often retrieved
    last_accessed TIMESTAMP,
    confirmed_count INTEGER DEFAULT 0,       -- Times user confirmed
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_prefs_category ON memory_preferences(category);
CREATE INDEX IF NOT EXISTS idx_prefs_importance ON memory_preferences(importance);

-- ============================================
-- TABLE: memory_episodes
-- Episodic memory - what happened when
-- ============================================
CREATE TABLE IF NOT EXISTS memory_episodes (
    id VARCHAR(50) PRIMARY KEY,
    date DATE NOT NULL,
    summary TEXT NOT NULL,                   -- Brief summary
    details TEXT,                            -- Full details
    project VARCHAR(100),                    -- Which project
    outcomes JSON,                           -- What resulted
    corrections_triggered JSON,              -- Array of correction IDs
    vector_embedding JSON,                   -- For semantic search (future)
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_episodes_date ON memory_episodes(date);
CREATE INDEX IF NOT EXISTS idx_episodes_project ON memory_episodes(project);

-- ============================================
-- TABLE: memory_decisions
-- Architectural/technical decisions
-- ============================================
CREATE TABLE IF NOT EXISTS memory_decisions (
    id VARCHAR(50) PRIMARY KEY,
    date DATE NOT NULL,
    project VARCHAR(100),
    decision TEXT NOT NULL,                  -- What was decided
    alternatives JSON,                       -- What was considered
    rationale TEXT NOT NULL,                 -- Why this choice
    status VARCHAR(20) DEFAULT 'active',   -- active, reversed, deprecated
    reversed_by VARCHAR(50),                 -- If reversed, link to new decision
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_decisions_project ON memory_decisions(project);
CREATE INDEX IF NOT EXISTS idx_decisions_status ON memory_decisions(status);

-- ============================================
-- TABLE: validation_runs
-- Track critic engine performance
-- ============================================
CREATE TABLE IF NOT EXISTS validation_runs (
    id VARCHAR(50) PRIMARY KEY,
    session_id VARCHAR(50),
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    input_length INTEGER,
    output_length INTEGER,
    violations_found INTEGER DEFAULT 0,
    new_patterns_detected INTEGER DEFAULT 0,
    corrections_auto_stored INTEGER DEFAULT 0,
    processing_time_ms INTEGER,              -- How long validation took
    blocked BOOLEAN DEFAULT FALSE,           -- Was output blocked?
    workflow VARCHAR(50)                     -- Which workflow was active
);

CREATE INDEX IF NOT EXISTS idx_validation_session ON validation_runs(session_id);
CREATE INDEX IF NOT EXISTS idx_validation_timestamp ON validation_runs(timestamp);

-- ============================================
-- SEED DATA: Initial correction patterns
-- ============================================

-- Critical patterns (always blocking)
INSERT INTO corrections (id, pattern, severity, message, suggestion, blocking, auto_detected) VALUES
('uncorrected_quantitative', '\\d+\\s*(%|percent|x\\s|times|fold)', 'auto-correct', 'Quantitative claim requires evidence citation', 'Add source or measurement method', TRUE, TRUE),
('unverified_done', '\\b(done|complete|finished|shipped)\\b(?!(?:.*\\b(verified|tested|validated|checked)\\b))', 'error', '"Done" claims require verification evidence', 'Add verification steps completed', TRUE, FALSE),
('unverified_performance', '\\b\\d+\\s*(%|percent|times|x\\s)\\s*(?:faster|slower|better|improved)', 'auto-correct', 'Performance claim requires benchmark data', 'Add benchmark methodology and results', TRUE, TRUE)

ON CONFLICT (id) DO NOTHING;

-- ============================================
-- VIEWS: Useful queries
-- ============================================

-- High-impact corrections (blocking + high hit count)
CREATE OR REPLACE VIEW high_impact_corrections AS
SELECT id, pattern, message, hit_count, last_triggered, created_at
FROM corrections
WHERE blocking = TRUE AND hit_count > 5
ORDER BY hit_count DESC;

-- Recent auto-detected patterns needing review
CREATE OR REPLACE VIEW auto_patterns_for_review AS
SELECT id, pattern, message, created_at, hit_count
FROM corrections
WHERE auto_detected = TRUE AND manual_entry = FALSE
  AND created_at > CURRENT_TIMESTAMP - INTERVAL '7 days'
ORDER BY hit_count DESC;
