-- Add sender_name to incoming letters
ALTER TABLE letters_incoming 
ADD COLUMN IF NOT EXISTS sender_name VARCHAR(255);

-- Add receiver_name to outgoing letters
ALTER TABLE letters_outgoing 
ADD COLUMN IF NOT EXISTS receiver_name VARCHAR(255);

-- Add indexes for the new fields to support searching
CREATE INDEX IF NOT EXISTS idx_letters_incoming_sender_name ON letters_incoming(sender_name) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_letters_outgoing_receiver_name ON letters_outgoing(receiver_name) WHERE deleted_at IS NULL;

-- Add GIN indexes for full-text search
CREATE INDEX IF NOT EXISTS idx_letters_incoming_sender_name_text ON letters_incoming USING gin(to_tsvector('simple', COALESCE(sender_name, '')));
CREATE INDEX IF NOT EXISTS idx_letters_outgoing_receiver_name_text ON letters_outgoing USING gin(to_tsvector('simple', COALESCE(receiver_name, '')));