-- UP
CREATE TABLE IF NOT EXISTS _migrations (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    version TEXT NOT NULL UNIQUE,
    applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS sessions (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL DEFAULT 'default',
    user_id TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS ethical_lessons (
    id TEXT PRIMARY KEY,
    principle TEXT NOT NULL,
    description TEXT,
    weight REAL DEFAULT 1.0,
    source_task TEXT,
    outcome TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS consequences (
    id TEXT PRIMARY KEY,
    action_id TEXT NOT NULL,
    choice_made TEXT NOT NULL,
    alternatives TEXT,
    expected_risk REAL DEFAULT 0.0,
    expected_reward REAL DEFAULT 0.0,
    actual_outcome TEXT,
    surprise_factor REAL DEFAULT 0.0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS tenants (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    config TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    active INTEGER DEFAULT 1
);

CREATE INDEX IF NOT EXISTS idx_sessions_tenant ON sessions(tenant_id);
CREATE INDEX IF NOT EXISTS idx_consequences_action ON consequences(action_id);
CREATE INDEX IF NOT EXISTS idx_ethical_lessons_source ON ethical_lessons(source_task);

-- DOWN
DROP TABLE IF EXISTS tenants;
DROP TABLE IF EXISTS consequences;
DROP TABLE IF EXISTS ethical_lessons;
DROP TABLE IF EXISTS sessions;
DROP TABLE IF EXISTS _migrations;
