
CREATE OR REPLACE FUNCTION public.auto_link_violation_owner()
 RETURNS trigger
 LANGUAGE plpgsql
 SECURITY DEFINER
 SET search_path TO 'public'
AS $function$
DECLARE
  v_owner_id UUID;
  v_unit_id UUID;
  v_normalized_address TEXT;
BEGIN
  -- Normalize the violation address once
  IF NEW.address IS NOT NULL AND NEW.address != '' THEN
    v_normalized_address := LOWER(TRIM(REPLACE(REPLACE(REPLACE(REPLACE(NEW.address, '.', ''), ',', ''), '#', ''), '-', '')));
    
    -- Auto-link unit_id if not already set
    IF NEW.unit_id IS NULL THEN
      SELECT u.id INTO v_unit_id
      FROM public.units u
      WHERE u.association_id = NEW.association_id
        AND LOWER(TRIM(REPLACE(REPLACE(REPLACE(REPLACE(u.address, '.', ''), ',', ''), '#', ''), '-', '')))
          = v_normalized_address
      ORDER BY u.created_at
      LIMIT 1;
      
      IF v_unit_id IS NOT NULL THEN
        NEW.unit_id := v_unit_id;
      END IF;
    END IF;

    -- Auto-link owner_id if not already set
    IF NEW.owner_id IS NULL THEN
      SELECT o.id INTO v_owner_id
      FROM public.owners o
      WHERE o.association_id = NEW.association_id
        AND LOWER(TRIM(REPLACE(REPLACE(REPLACE(REPLACE(o.property_address, '.', ''), ',', ''), '#', ''), '-', '')))
          = v_normalized_address
      ORDER BY (CASE WHEN o.status = 'active' THEN 0 ELSE 1 END), o.created_at
      LIMIT 1;
      
      IF v_owner_id IS NOT NULL THEN
        NEW.owner_id := v_owner_id;
      END IF;
    END IF;

    -- If unit found but owner still null, try linking owner via unit_id
    IF NEW.owner_id IS NULL AND NEW.unit_id IS NOT NULL THEN
      SELECT o.id INTO v_owner_id
      FROM public.owners o
      WHERE o.association_id = NEW.association_id
        AND o.unit_id = NEW.unit_id
      ORDER BY (CASE WHEN o.status = 'active' THEN 0 ELSE 1 END), o.created_at
      LIMIT 1;
      
      IF v_owner_id IS NOT NULL THEN
        NEW.owner_id := v_owner_id;
      END IF;
    END IF;
  END IF;
  
  RETURN NEW;
END;
$function$;
