-- V6: Add indexes for high-frequency query columns.
-- All indexes use IF NOT EXISTS for idempotent migrations.

-- post table: most reads filter/sort by discussion_id, parent_id, create_time
CREATE INDEX IF NOT EXISTS idx_post_discussion_id  ON post(discussion_id);
CREATE INDEX IF NOT EXISTS idx_post_parent_id      ON post(parent_id);
CREATE INDEX IF NOT EXISTS idx_post_create_time    ON post(create_time);
CREATE INDEX IF NOT EXISTS idx_post_create_id      ON post(create_id);

-- discussion table: sorted by id/create_time/start_time/like_count; joined on start_user_id/last_user_id
CREATE INDEX IF NOT EXISTS idx_discussion_create_time    ON discussion(create_time);
CREATE INDEX IF NOT EXISTS idx_discussion_start_time     ON discussion(start_time);
CREATE INDEX IF NOT EXISTS idx_discussion_like_count     ON discussion(like_count);
CREATE INDEX IF NOT EXISTS idx_discussion_start_user_id  ON discussion(start_user_id);
CREATE INDEX IF NOT EXISTS idx_discussion_last_user_id   ON discussion(last_user_id);

-- discussiontag table: used in tag-filter queries and cascade deletes
CREATE INDEX IF NOT EXISTS idx_discussiontag_discussion_id ON discussiontag(discussion_id);
CREATE INDEX IF NOT EXISTS idx_discussiontag_tag_id        ON discussiontag(tag_id);

-- user table: login and username-lookup path
CREATE INDEX IF NOT EXISTS idx_user_username ON user(username);

-- likecollect table: user activity queries
CREATE INDEX IF NOT EXISTS idx_likecollect_user_id        ON likecollect(user_id);
CREATE INDEX IF NOT EXISTS idx_likecollect_discussion_id  ON likecollect(discussion_id);
