-- 008_plugins_popup_blocker.sql: Popup Blocker plugin (弹窗拦截)

CREATE TABLE IF NOT EXISTS popup_filter_rules (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    rule_type       TEXT NOT NULL CHECK(rule_type IN ('block', 'allow')),
    window_title    TEXT,
    window_class    TEXT,
    process_name    TEXT,
    target_type     TEXT NOT NULL DEFAULT 'global' CHECK(target_type IN ('global', 'group', 'device')),
    target_id       TEXT,
    enabled         INTEGER NOT NULL DEFAULT 1,
    created_at      TEXT NOT NULL DEFAULT (datetime('now'))
);

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

CREATE INDEX IF NOT EXISTS idx_popup_rules_enabled ON popup_filter_rules(rule_type, enabled);
