-- Migration 004: Cognition (classifications, executions, learning)
-- All cognition objects are dual entities (entities row + typed table).
-- Classifications persist every autonomous decision (SA5).

CREATE TABLE classifications (
    entity_id         UUID PRIMARY KEY REFERENCES entities(id),
    signal_entity_id  UUID REFERENCES signals(entity_id),
    target_entity_id  UUID REFERENCES entities(id),
    action            TEXT NOT NULL,
    recommended_action JSONB,
    risk_class        TEXT NOT NULL,
    route             TEXT NOT NULL CHECK (route IN ('auto-act','escalate','hold')),
    blast_radius      UUID[],
    pattern_confidence REAL,
    skill_id          UUID,
    autonomy_check    TEXT,
    reasoning         JSONB NOT NULL,
    correlation_id    TEXT NOT NULL,
    created_at        TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_class_signal ON classifications(signal_entity_id);
CREATE INDEX idx_class_entity ON classifications(target_entity_id);

CREATE TABLE executions (
    entity_id         UUID PRIMARY KEY REFERENCES entities(id),
    classification_id UUID REFERENCES classifications(entity_id),
    signal_entity_id  UUID REFERENCES signals(entity_id),
    target_entity_id  UUID REFERENCES entities(id),
    action            TEXT NOT NULL,
    risk_class        TEXT NOT NULL,
    approval_id       UUID REFERENCES approvals(entity_id),
    agent_id          UUID REFERENCES entities(id),
    skill_id          UUID,
    skill_version     INTEGER,
    status            TEXT NOT NULL DEFAULT 'proposed',
    result            JSONB,
    duration_ms       INTEGER,
    verified          BOOLEAN NOT NULL DEFAULT false,
    correlation_id    TEXT NOT NULL,
    started_at        TIMESTAMPTZ,
    completed_at      TIMESTAMPTZ,
    created_at        TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_exec_target ON executions(target_entity_id);
CREATE INDEX idx_exec_status ON executions(status);

CREATE TABLE feedback (
    entity_id     UUID PRIMARY KEY REFERENCES entities(id),
    execution_id  UUID NOT NULL REFERENCES executions(entity_id),
    outcome       TEXT NOT NULL CHECK (outcome IN ('success','failure','partial','unexpected')),
    observation   TEXT,
    lesson        TEXT,
    unexpected_side_effects TEXT[],
    tags          TEXT[],
    created_at    TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_feedback_ts ON feedback(created_at);

CREATE TABLE patterns (
    entity_id     UUID PRIMARY KEY REFERENCES entities(id),
    applies_type  TEXT NOT NULL REFERENCES entity_types(name),
    action        TEXT NOT NULL,
    pattern       TEXT NOT NULL,
    confidence    REAL NOT NULL DEFAULT 0,
    evidence_count INTEGER NOT NULL DEFAULT 0,
    success_count INTEGER NOT NULL DEFAULT 0,
    failure_count INTEGER NOT NULL DEFAULT 0,
    status        TEXT NOT NULL DEFAULT 'hypothesized',
    quarantined   BOOLEAN NOT NULL DEFAULT false,
    version       INTEGER NOT NULL DEFAULT 1,
    last_validated_at TIMESTAMPTZ,
    created_at    TIMESTAMPTZ NOT NULL DEFAULT now(),
    UNIQUE (applies_type, action)
);

CREATE TABLE skills (
    entity_id     UUID NOT NULL REFERENCES entities(id),
    version       INTEGER NOT NULL DEFAULT 1,
    name          TEXT NOT NULL,
    procedure     JSONB NOT NULL,
    applies_type  TEXT REFERENCES entity_types(name),
    action        TEXT NOT NULL,
    pattern_ids   UUID[],
    status        TEXT NOT NULL DEFAULT 'drafted',
    success_rate  REAL,
    changed_by    UUID,
    change_reason TEXT,
    last_used_at  TIMESTAMPTZ,
    created_at    TIMESTAMPTZ NOT NULL DEFAULT now(),
    PRIMARY KEY (entity_id, version)
);
