-- 006_plugins_usage_timer.sql: Usage Timer plugin (时长记录)

CREATE TABLE IF NOT EXISTS usage_daily (
    id                  INTEGER PRIMARY KEY AUTOINCREMENT,
    device_uid          TEXT NOT NULL REFERENCES devices(device_uid) ON DELETE CASCADE,
    date                TEXT NOT NULL,
    total_active_minutes INTEGER NOT NULL DEFAULT 0,
    total_idle_minutes   INTEGER NOT NULL DEFAULT 0,
    first_active_at     TEXT,
    last_active_at      TEXT,
    UNIQUE(device_uid, date)
);

CREATE TABLE IF NOT EXISTS app_usage_daily (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    device_uid      TEXT NOT NULL REFERENCES devices(device_uid) ON DELETE CASCADE,
    date            TEXT NOT NULL,
    app_name        TEXT NOT NULL,
    usage_minutes   INTEGER NOT NULL DEFAULT 0,
    UNIQUE(device_uid, date, app_name)
);

CREATE INDEX IF NOT EXISTS idx_usage_daily_date ON usage_daily(date);
CREATE INDEX IF NOT EXISTS idx_app_usage_daily_date ON app_usage_daily(date);
