
-- Add a unique constraint on owners for Buildium sync upserts
-- Using association_id + first_name + last_name + property_address to handle multiple owners at same address
CREATE UNIQUE INDEX IF NOT EXISTS owners_buildium_sync_key 
ON public.owners (association_id, first_name, last_name, property_address) 
WHERE property_address IS NOT NULL;

-- Add buildium_owner_id column for future direct ID mapping
ALTER TABLE public.owners ADD COLUMN IF NOT EXISTS buildium_owner_id text;
CREATE UNIQUE INDEX IF NOT EXISTS owners_buildium_id_key ON public.owners (buildium_owner_id) WHERE buildium_owner_id IS NOT NULL;
