-- Таблица отслеживания между пользователями
CREATE TABLE user_tracking (
    id SERIAL PRIMARY KEY,
    tracker_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    tracked_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT unique_tracking_pair UNIQUE (tracker_id, tracked_id),
    CONSTRAINT no_self_tracking CHECK (tracker_id != tracked_id)
);

CREATE INDEX idx_user_tracking_tracker ON user_tracking(tracker_id);
CREATE INDEX idx_user_tracking_tracked ON user_tracking(tracked_id);

-- Таблица токенов приглашений (живут 1 час)
CREATE TABLE tracking_invite_tokens (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    token VARCHAR(64) NOT NULL UNIQUE,
    expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_tracking_invite_tokens_token ON tracking_invite_tokens(token);
CREATE INDEX idx_tracking_invite_tokens_user ON tracking_invite_tokens(user_id);
