-- KeyHunter database schema
-- Version: 1

CREATE TABLE IF NOT EXISTS scans (
    id            INTEGER PRIMARY KEY AUTOINCREMENT,
    started_at    DATETIME NOT NULL,
    finished_at   DATETIME,
    source_path   TEXT,
    finding_count INTEGER DEFAULT 0,
    created_at    DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS findings (
    id                   INTEGER PRIMARY KEY AUTOINCREMENT,
    scan_id              INTEGER REFERENCES scans(id),
    provider_name        TEXT NOT NULL,
    key_value            BLOB NOT NULL,
    key_masked           TEXT NOT NULL,
    confidence           TEXT NOT NULL,
    source_path          TEXT,
    source_type          TEXT,
    line_number          INTEGER,
    verified             INTEGER NOT NULL DEFAULT 0,
    verify_status        TEXT NOT NULL DEFAULT '',
    verify_http_code     INTEGER NOT NULL DEFAULT 0,
    verify_metadata_json TEXT,
    created_at           DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS settings (
    key        TEXT PRIMARY KEY,
    value      TEXT NOT NULL,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- Indexes for common queries
CREATE INDEX IF NOT EXISTS idx_findings_scan_id ON findings(scan_id);
CREATE INDEX IF NOT EXISTS idx_findings_provider ON findings(provider_name);
CREATE INDEX IF NOT EXISTS idx_findings_created ON findings(created_at DESC);

-- Phase 8: user-authored dork definitions complementing the embedded
-- YAML-based dork registry (pkg/dorks/definitions). Embedded dorks are
-- read-only; custom_dorks stores rows created via `keyhunter dorks add`.
CREATE TABLE IF NOT EXISTS custom_dorks (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    dork_id     TEXT NOT NULL UNIQUE,
    name        TEXT NOT NULL,
    source      TEXT NOT NULL,
    category    TEXT NOT NULL,
    query       TEXT NOT NULL,
    description TEXT,
    tags        TEXT,  -- JSON array
    created_at  DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_custom_dorks_source ON custom_dorks(source);
CREATE INDEX IF NOT EXISTS idx_custom_dorks_category ON custom_dorks(category);
