-- 0001_init.sql

CREATE TABLE IF NOT EXISTS users (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    username    TEXT UNIQUE NOT NULL,
    password_hash TEXT NOT NULL,
    role        TEXT NOT NULL DEFAULT 'user',
    created_at  DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS ssh_keys (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    label       TEXT NOT NULL,
    private_key_path TEXT NOT NULL,
    public_key  TEXT NOT NULL,
    created_at  DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS machines (
    id                          INTEGER PRIMARY KEY AUTOINCREMENT,
    name                        TEXT NOT NULL,
    host                        TEXT NOT NULL,
    port                        INTEGER NOT NULL DEFAULT 22,
    ssh_user                    TEXT NOT NULL DEFAULT 'root',
    ssh_key_id                  INTEGER REFERENCES ssh_keys(id),
    mac_address                 TEXT,
    wol_enabled                 INTEGER NOT NULL DEFAULT 0,
    broadcast_addr              TEXT,
    wake_timeout_seconds        INTEGER NOT NULL DEFAULT 120,
    wake_check_interval_seconds INTEGER NOT NULL DEFAULT 5,
    fingerprint_confirmed       INTEGER NOT NULL DEFAULT 0,
    status                      TEXT NOT NULL DEFAULT 'unknown',
    last_seen_at                DATETIME,
    created_at                  DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS sync_pairs (
    id                  INTEGER PRIMARY KEY AUTOINCREMENT,
    name                TEXT NOT NULL,
    source_machine_id   INTEGER REFERENCES machines(id),
    source_path         TEXT NOT NULL,
    dest_machine_id     INTEGER REFERENCES machines(id),
    dest_path           TEXT NOT NULL,
    direction           TEXT NOT NULL DEFAULT 'push',
    rsync_flags         TEXT NOT NULL DEFAULT '-aP',
    exclude_patterns    TEXT NOT NULL DEFAULT '',
    enabled             INTEGER NOT NULL DEFAULT 1,
    created_at          DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS schedules (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    sync_pair_id    INTEGER NOT NULL REFERENCES sync_pairs(id) ON DELETE CASCADE,
    cron_expr       TEXT NOT NULL,
    next_run_at     DATETIME,
    enabled         INTEGER NOT NULL DEFAULT 1,
    created_at      DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS jobs (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    sync_pair_id    INTEGER NOT NULL REFERENCES sync_pairs(id),
    trigger_type    TEXT NOT NULL DEFAULT 'manual',
    status          TEXT NOT NULL DEFAULT 'queued',
    started_at      DATETIME,
    finished_at     DATETIME,
    log_file        TEXT,
    created_at      DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS job_logs (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    job_id      INTEGER NOT NULL REFERENCES jobs(id) ON DELETE CASCADE,
    stream      TEXT NOT NULL,
    content     TEXT NOT NULL,
    timestamp   DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS sessions (
    id          TEXT PRIMARY KEY,
    user_id     INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    token_hash  TEXT NOT NULL,
    expires_at  DATETIME NOT NULL,
    revoked     INTEGER NOT NULL DEFAULT 0,
    created_at  DATETIME DEFAULT CURRENT_TIMESTAMP
);
