-- Add unique constraint on analysis_response_elements by analysis_response_id + analysis_element_original_id
CREATE UNIQUE INDEX analysis_response_elements_unique_by_response_and_element 
ON "medreport"."analysis_response_elements" 
USING btree (analysis_response_id, analysis_element_original_id);

ALTER TABLE "medreport"."analysis_response_elements" 
ADD CONSTRAINT "analysis_response_elements_unique_by_response_and_element" 
UNIQUE USING INDEX "analysis_response_elements_unique_by_response_and_element";

-- Add updated_at column to medipost_actions table
ALTER TABLE "medreport"."medipost_actions" ADD COLUMN "updated_at" TIMESTAMP WITH TIME ZONE DEFAULT now();

-- Add unique constraint on medipost_actions by medipost_private_message_id
-- Using partial index to allow multiple NULL values but enforce uniqueness for non-NULL values
CREATE UNIQUE INDEX medipost_actions_unique_by_private_message_id 
ON "medreport"."medipost_actions" 
USING btree (medipost_private_message_id) 
WHERE medipost_private_message_id IS NOT NULL;
