CREATE OR REPLACE FUNCTION public.create_form_inbox_entry_from_owner_registration_request()
RETURNS trigger
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path TO 'public'
AS $$
DECLARE
  v_owner_name text;
  v_owner_email text;
  v_title text;
  v_summary text;
BEGIN
  IF NEW.owner_id IS NOT NULL THEN
    SELECT
      NULLIF(trim(concat_ws(' ', o.first_name, o.last_name)), ''),
      o.email
    INTO v_owner_name, v_owner_email
    FROM public.owners o
    WHERE o.id = NEW.owner_id;
  END IF;

  v_owner_name := COALESCE(v_owner_name, NULLIF(NEW.last_name, ''));
  v_owner_email := COALESCE(v_owner_email, NEW.email);
  v_title := 'Registration Request - ' || COALESCE(v_owner_name, NEW.email, 'Homeowner');
  v_summary := concat_ws(' | ',
    'Email: ' || NEW.email,
    CASE WHEN NEW.unit_identifier IS NOT NULL THEN 'Unit: ' || NEW.unit_identifier END,
    CASE WHEN NEW.account_number IS NOT NULL THEN 'Account: ' || NEW.account_number END,
    CASE WHEN NEW.status IS NOT NULL THEN 'Status: ' || NEW.status END
  );

  INSERT INTO public.form_inbox (
    source_type,
    source_id,
    association_id,
    title,
    submitter_name,
    submitter_email,
    summary
  )
  VALUES (
    'registration_request',
    NEW.id,
    NEW.association_id,
    v_title,
    v_owner_name,
    v_owner_email,
    LEFT(v_summary, 200)
  );

  RETURN NEW;
END;
$$;

CREATE OR REPLACE FUNCTION public.notify_staff_on_owner_registration_request()
RETURNS trigger
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path TO 'public'
AS $$
DECLARE
  staff_user record;
  v_display_name text;
  v_message text;
BEGIN
  v_display_name := COALESCE(NULLIF(NEW.last_name, ''), NEW.email, 'A homeowner');
  v_message := v_display_name || ' submitted a registration request';

  IF NEW.unit_identifier IS NOT NULL THEN
    v_message := v_message || ' for unit ' || NEW.unit_identifier;
  END IF;

  FOR staff_user IN
    SELECT DISTINCT user_id
    FROM public.user_roles
    WHERE role IN ('admin'::public.app_role, 'manager'::public.app_role)
      AND user_id IS NOT NULL
  LOOP
    INSERT INTO public.in_app_notifications
      (user_id, type, title, message, related_item_id, related_item_type, link)
    VALUES (
      staff_user.user_id,
      'owner_registration_submitted',
      'New Registration Request',
      v_message,
      NEW.id,
      'owner_registration_request',
      '/dashboard/form-inbox'
    );
  END LOOP;

  RETURN NEW;
END;
$$;

DROP TRIGGER IF EXISTS trg_owner_registration_request_form_inbox ON public.owner_registration_requests;
CREATE TRIGGER trg_owner_registration_request_form_inbox
AFTER INSERT ON public.owner_registration_requests
FOR EACH ROW
EXECUTE FUNCTION public.create_form_inbox_entry_from_owner_registration_request();

DROP TRIGGER IF EXISTS trg_owner_registration_request_notify_staff ON public.owner_registration_requests;
CREATE TRIGGER trg_owner_registration_request_notify_staff
AFTER INSERT ON public.owner_registration_requests
FOR EACH ROW
EXECUTE FUNCTION public.notify_staff_on_owner_registration_request();