CREATE TABLE desktop_clients (
    id UUID PRIMARY KEY,
    tenant_id BIGINT NOT NULL REFERENCES tenants(id),
    workspace_id BIGINT NOT NULL REFERENCES workspaces(id),
    user_id BIGINT NOT NULL REFERENCES users(id),
    token_hash BYTEA NOT NULL UNIQUE,
    device_name TEXT,
    os TEXT,
    cpu_arch TEXT,
    client_version TEXT,
    channel TEXT,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    last_seen_at TIMESTAMPTZ,
    last_rotated_at TIMESTAMPTZ,
    revoked_at TIMESTAMPTZ
);

CREATE INDEX idx_desktop_clients_workspace
    ON desktop_clients (workspace_id)
    WHERE revoked_at IS NULL;

CREATE INDEX idx_desktop_clients_user
    ON desktop_clients (user_id)
    WHERE revoked_at IS NULL;
