-- SMTP relay credentials stored in the database.
-- A single active row is read at send-time for flexibility (change provider,
-- rotate passwords, etc. without touching code or env vars).

CREATE TABLE IF NOT EXISTS smtp_settings (
    id         INTEGER PRIMARY KEY CHECK (id = 1),   -- singleton row
    host       TEXT NOT NULL DEFAULT '',
    port       INTEGER NOT NULL DEFAULT 587,
    encryption TEXT NOT NULL DEFAULT 'tls',           -- 'tls' | 'ssl' | 'none'
    username   TEXT NOT NULL DEFAULT '',
    password   TEXT NOT NULL DEFAULT '',              -- stored in clear for now; encrypt later
    from_email TEXT NOT NULL DEFAULT '',
    from_name  TEXT NOT NULL DEFAULT 'Post-ERG',
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- Insert default empty row so the settings form can start working immediately.
INSERT OR IGNORE INTO smtp_settings (id) VALUES (1);

-- Helper view so callers always read the same row.
CREATE VIEW IF NOT EXISTS v_smtp_active AS
SELECT * FROM smtp_settings WHERE id = 1;
