-- Fix quantity calculations to be the sum of refill + spulna counts
UPDATE filaments 
SET kolicina = 
  COALESCE(
    CASE 
      WHEN refill ~ '^\d+$' THEN CAST(refill AS INTEGER)
      ELSE 0
    END, 0
  ) + 
  COALESCE(
    CASE 
      WHEN spulna ~ '^(\d+)\s*spuln' THEN 
        CAST(SUBSTRING(spulna FROM '^(\d+)\s*spuln') AS INTEGER)
      ELSE 0
    END, 0
  );

-- Specifically fix refill-only colors to ensure quantity matches refill count
UPDATE filaments 
SET kolicina = 
  CASE 
    WHEN refill ~ '^\d+$' THEN CAST(refill AS INTEGER)
    ELSE 0
  END
WHERE boja IN (
  'Beige',
  'Light Gray',
  'Yellow',
  'Orange',
  'Gold',
  'Bright Green',
  'Pink',
  'Magenta',
  'Maroon Red',
  'Purple',
  'Turquoise',
  'Cobalt Blue',
  'Brown',
  'Bronze',
  'Silver',
  'Blue Grey',
  'Dark Gray'
);