-- ============================================================
-- Версия 1 (user_version = 1): начальная схема
-- ============================================================

CREATE TABLE IF NOT EXISTS devices
(
    id            TEXT PRIMARY KEY,
    api_id        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 applications
(
    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,
    FOREIGN KEY (device_id) REFERENCES devices (id) ON DELETE CASCADE,
    UNIQUE (device_id, code)
);

-- app_code: код банка ("ozon", "black") — не путать с человекочитаемым name
CREATE TABLE IF NOT EXISTS accounts
(
    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',
    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,
    FOREIGN KEY (account_id) REFERENCES accounts (id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS events
(
    id            INTEGER PRIMARY KEY AUTOINCREMENT,
    account_id    INTEGER,
    external_id   INTEGER,
    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,
    FOREIGN KEY (account_id) REFERENCES accounts (id) ON DELETE CASCADE,
    FOREIGN KEY (device_id)  REFERENCES devices (id)  ON DELETE CASCADE,
    UNIQUE (external_id)
);

-- ============================================================
-- Версия 2 (user_version = 2): currency + rename app_name→app_code
-- ============================================================

-- Для существующих БД (применяется автоматически при старте):
-- ALTER TABLE accounts ADD COLUMN currency TEXT DEFAULT '';
-- ALTER TABLE accounts RENAME COLUMN app_name TO app_code;
