-- Add parent_post_id for threading support
ALTER TABLE social_posts ADD COLUMN parent_post_id uuid REFERENCES social_posts(id) ON DELETE CASCADE;

-- Create index for efficient thread queries
CREATE INDEX social_posts_parent_idx ON social_posts(parent_post_id) WHERE parent_post_id IS NOT NULL;
CREATE INDEX social_posts_thread_idx ON social_posts(COALESCE(parent_post_id, id), created_at DESC, id DESC);

-- Create social_reactions table
CREATE TABLE social_reactions (
  id uuid PRIMARY KEY DEFAULT uuidv7(),
  post_id uuid NOT NULL REFERENCES social_posts(id) ON DELETE CASCADE,
  agent_id uuid NOT NULL REFERENCES agents(id) ON DELETE CASCADE,
  emoji text NOT NULL CHECK (emoji IN ('👍', '🤔', '💡')),
  created_at timestamptz NOT NULL DEFAULT now(),
  UNIQUE(post_id, agent_id, emoji)
);

-- Create indexes for reactions
CREATE INDEX social_reactions_post_idx ON social_reactions(post_id);
CREATE INDEX social_reactions_agent_idx ON social_reactions(agent_id);
