CREATE TABLE IF NOT EXISTS storage_config (
    id INTEGER PRIMARY KEY CHECK (id = 1),
    mover_source          TEXT NOT NULL DEFAULT '',
    mover_dest            TEXT NOT NULL DEFAULT '',
    mover_clean_macos     INTEGER NOT NULL DEFAULT 1,
    mover_remove_source   INTEGER NOT NULL DEFAULT 1,
    mover_inplace         INTEGER NOT NULL DEFAULT 1,
    mover_rsync_options   TEXT NOT NULL DEFAULT '',
    snapraid_content      TEXT NOT NULL DEFAULT '',
    snapraid_data_dirs    TEXT NOT NULL DEFAULT '',
    snapraid_parity_dir   TEXT NOT NULL DEFAULT '',
    snapraid_scrub_plan   INTEGER NOT NULL DEFAULT 8,
    updated_at            TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS storage_jobs (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    kind        TEXT NOT NULL,
    status      TEXT NOT NULL DEFAULT 'queued',
    args_json   TEXT NOT NULL DEFAULT '{}',
    pid         INTEGER NOT NULL DEFAULT 0,
    started_at  TEXT,
    finished_at TEXT,
    exit_code   INTEGER NOT NULL DEFAULT -1,
    output      TEXT NOT NULL DEFAULT '',
    error       TEXT NOT NULL DEFAULT '',
    created_at  TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_storage_jobs_created ON storage_jobs(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_storage_jobs_kind_created ON storage_jobs(kind, created_at DESC);
