-- DB guard for repeat_on_days: jsonb array of weekday integers 0 (Sun) … 6 (Sat), or null.
-- Application layer still sanitizes via sanitizeRepeatOnDaysForStorage in scheduleJobsService.
ALTER TABLE "schedule_jobs"
  ADD CONSTRAINT "schedule_jobs_repeat_on_days_check"
  CHECK (
    "repeat_on_days" IS NULL
    OR (
      jsonb_typeof("repeat_on_days") = 'array'
      AND (
        jsonb_array_length("repeat_on_days") = 0
        OR NOT EXISTS (
          SELECT 1
          FROM jsonb_array_elements("repeat_on_days") AS elem(value)
          WHERE jsonb_typeof(elem.value) <> 'number'
            OR (elem.value #>> '{}')::numeric <> trunc((elem.value #>> '{}')::numeric)
            OR (elem.value #>> '{}')::int < 0
            OR (elem.value #>> '{}')::int > 6
        )
      )
    )
  );
