-- 003_usb.sql: USB control tables

CREATE TABLE IF NOT EXISTS usb_events (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    device_uid      TEXT NOT NULL REFERENCES devices(device_uid) ON DELETE CASCADE,
    vendor_id       TEXT,
    product_id      TEXT,
    serial_number   TEXT,
    device_name     TEXT,
    event_type      TEXT NOT NULL CHECK(event_type IN ('inserted', 'removed', 'blocked')),
    event_time      TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS usb_policies (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    name            TEXT NOT NULL,
    policy_type     TEXT NOT NULL CHECK(policy_type IN ('all_block', 'whitelist', 'blacklist')),
    target_group    TEXT,
    rules           TEXT NOT NULL,
    enabled         INTEGER NOT NULL DEFAULT 1,
    created_at      TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at      TEXT NOT NULL DEFAULT (datetime('now'))
);

-- Indexes
CREATE INDEX IF NOT EXISTS idx_usb_events_device_time ON usb_events(device_uid, event_time);
CREATE INDEX IF NOT EXISTS idx_usb_events_time ON usb_events(event_time);
CREATE INDEX IF NOT EXISTS idx_usb_policies_target ON usb_policies(target_group);
