CREATE TABLE users (
    id           TEXT PRIMARY KEY,
    username     TEXT NOT NULL,
    display_name TEXT,
    email        TEXT,
    created_at   TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    last_seen_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE play_events (
    id        BIGSERIAL PRIMARY KEY,
    user_id   TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    track_id  BIGINT NOT NULL REFERENCES tracks(id) ON DELETE CASCADE,
    played_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_play_events_user_id ON play_events(user_id);
CREATE INDEX idx_play_events_track_id ON play_events(track_id);
CREATE INDEX idx_play_events_user_track ON play_events(user_id, track_id);
CREATE INDEX idx_play_events_played_at ON play_events(played_at DESC);
