WITH parsed_hole_counts AS (
    SELECT
        c.id AS course_id,
        NULLIF(SUBSTRING(COALESCE(f.amenities->>'antall_hull', '') FROM '([0-9]+)'), '')::INTEGER AS parsed_hole_count
    FROM courses c
    JOIN facilities f ON f.id = c.facility_id
    WHERE c.is_main_course = TRUE
      AND COALESCE(f.amenities->>'antall_hull', '') <> ''
)
UPDATE courses c
SET physical_hole_count = p.parsed_hole_count
FROM parsed_hole_counts p
WHERE c.id = p.course_id
  AND p.parsed_hole_count IS NOT NULL
  AND (
      c.physical_hole_count IS NULL
      OR c.physical_hole_count > (p.parsed_hole_count * 2)
  );
