-- ============================================================
-- Схема БД ARCDeskProject
-- ============================================================

CREATE TABLE IF NOT EXISTS devices
(
    id            TEXT PRIMARY KEY,
    api_id        TEXT,
    adb_serial    TEXT,
    status        TEXT,
    name          TEXT,
    android       TEXT,
    screen_width  INTEGER,
    screen_height INTEGER,
    battery       INTEGER,
    update_time   DATETIME,
    timestamp     DATETIME DEFAULT (strftime('%d.%m.%Y %H:%M:%S', 'now')),
    image         BLOB
);

CREATE TABLE IF NOT EXISTS bank_profiles
(
    id               INTEGER PRIMARY KEY AUTOINCREMENT,
    device_id        TEXT,
    pin_code         TEXT,
    pin_code_checked TEXT DEFAULT 'no',
    name             TEXT,
    code             TEXT,
    package          TEXT,
    status           TEXT DEFAULT 'off',
    install          TEXT DEFAULT 'no',
    comment          TEXT,
    phone            TEXT DEFAULT '',
    email            TEXT DEFAULT '',
    full_name        TEXT DEFAULT '',
    bank_profile_id  TEXT DEFAULT '',
    currency         TEXT DEFAULT '',
    amount           REAL DEFAULT 0,
    app_version      TEXT DEFAULT '',
    app_version_checked_at DATETIME,
    FOREIGN KEY (device_id) REFERENCES devices (id) ON DELETE CASCADE,
    UNIQUE (device_id, code)
);

-- app_code: код банка ("ozon", "black") — не путать с человекочитаемым name
CREATE TABLE IF NOT EXISTS materials
(
    id           INTEGER PRIMARY KEY AUTOINCREMENT,
    device_id    TEXT,
    app_code     TEXT,
    last_numbers TEXT,
    description  TEXT,
    amount       REAL,
    currency     TEXT DEFAULT '',
    update_time  DATETIME DEFAULT (strftime('%d.%m.%Y %H:%M:%S', 'now')),
    status       TEXT DEFAULT 'active',
    card_number  TEXT DEFAULT '',
    account_number TEXT DEFAULT '',
    material_id  TEXT DEFAULT '',
    FOREIGN KEY (device_id) REFERENCES devices (id) ON DELETE CASCADE,
    UNIQUE (device_id, app_code, last_numbers)
);

CREATE TABLE IF NOT EXISTS transactions
(
    id                        INTEGER PRIMARY KEY AUTOINCREMENT,
    account_id                INTEGER,
    external_id               INTEGER,
    amount                    REAL,
    fee                       REAL,
    status                    TEXT,
    type                      TEXT,
    phone                     TEXT,
    name                      TEXT,
    short_description         TEXT,
    updated_short_description TEXT,
    description               TEXT,
    updated_description       TEXT,
    bank_name                 TEXT,
    bank_time                 TEXT,
    bank_tr_external_id       TEXT,
    update_time               DATETIME,
    complete_time             DATETIME,
    timestamp                 DATETIME DEFAULT CURRENT_TIMESTAMP,
    receipt_image             BLOB,
    FOREIGN KEY (account_id) REFERENCES materials (id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS events
(
    id             INTEGER PRIMARY KEY AUTOINCREMENT,
    account_id     INTEGER,
    external_id    TEXT,
    transaction_id TEXT,
    device_id      TEXT,
    amount         REAL,
    phone          TEXT,
    bank_name      TEXT,
    comment        TEXT,
    status         TEXT,
    type           TEXT,
    update_time    DATETIME,
    complete_time  DATETIME,
    timestamp      DATETIME DEFAULT CURRENT_TIMESTAMP,
    json           TEXT DEFAULT '',
    sent_to_server INTEGER DEFAULT 0,
    screenshot     BLOB,
    FOREIGN KEY (account_id) REFERENCES materials (id) ON DELETE CASCADE,
    FOREIGN KEY (device_id)  REFERENCES devices (id)   ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS banks
(
    id        INTEGER PRIMARY KEY AUTOINCREMENT,
    alias     TEXT UNIQUE,
    name      TEXT,
    nspk_name TEXT
);

CREATE TABLE IF NOT EXISTS app_logs
(
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    source     TEXT NOT NULL DEFAULT '',
    device_id  TEXT NOT NULL DEFAULT '',
    message    TEXT NOT NULL,
    screenshot BLOB,
    timestamp  TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_app_logs_ts ON app_logs (id DESC);

CREATE TABLE IF NOT EXISTS general_logs
(
    id        INTEGER PRIMARY KEY AUTOINCREMENT,
    level     TEXT NOT NULL DEFAULT '',
    source    TEXT NOT NULL DEFAULT '',
    message   TEXT NOT NULL,
    timestamp TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_general_logs_id ON general_logs (id DESC);

CREATE TABLE IF NOT EXISTS settings
(
    key   TEXT PRIMARY KEY,
    value TEXT
);

-- Wi-Fi реквизиты подключения per-device (связь по android_id = devices.id).
-- ip+port — для adb connect; code — одноразовый pairing-код (только для первого adb pair).
CREATE TABLE IF NOT EXISTS wifi_connections
(
    android_id TEXT PRIMARY KEY,
    ip         TEXT,
    port       INTEGER,
    code       TEXT,
    updated_at DATETIME,
    FOREIGN KEY (android_id) REFERENCES devices (id) ON DELETE CASCADE
);
