-- Migration: 003_remove_material
-- Removes the material segment from part numbers
-- Old format: {project}-{category}-{material}-{sequence} (e.g., CS100-F01-316-0001)
-- New format: {project}-{category}-{sequence} (e.g., CS100-F01-0001)

BEGIN;

-- Transform existing part numbers: remove 3rd segment (material)
-- Pattern: XXXXX-CCC-MMM-NNNN -> XXXXX-CCC-NNNN
UPDATE items
SET part_number =
    split_part(part_number, '-', 1) || '-' ||
    split_part(part_number, '-', 2) || '-' ||
    split_part(part_number, '-', 4),
    updated_at = now()
WHERE part_number ~ '^[A-Z0-9]{5}-[A-Z][0-9]{2}-[A-Z0-9]{3}-[0-9]{4}$';

-- Update properties JSONB in revisions to remove material key
UPDATE revisions
SET properties = properties - 'material'
WHERE properties ? 'material';

COMMIT;
