-- Add sprinkler-specific fields to user_equipment
ALTER TABLE user_equipment
  ADD COLUMN IF NOT EXISTS sprinkler_mount VARCHAR(20) CHECK (sprinkler_mount IN ('in_ground','above_ground')),
  ADD COLUMN IF NOT EXISTS sprinkler_head_type VARCHAR(30) CHECK (sprinkler_head_type IN ('rotor_impact','oscillating_fan','spray_fixed','drip')),
  ADD COLUMN IF NOT EXISTS sprinkler_gpm DECIMAL(8,2),
  ADD COLUMN IF NOT EXISTS sprinkler_throw_feet DECIMAL(8,2),
  ADD COLUMN IF NOT EXISTS sprinkler_degrees INTEGER,
  ADD COLUMN IF NOT EXISTS sprinkler_length_feet DECIMAL(8,2),
  ADD COLUMN IF NOT EXISTS sprinkler_width_feet DECIMAL(8,2),
  ADD COLUMN IF NOT EXISTS sprinkler_coverage_sqft DECIMAL(10,2);

-- Ensure a Sprinkler category and type exist
DO $$
DECLARE cid INT; tid INT; 
BEGIN
  SELECT id INTO cid FROM equipment_categories WHERE name ILIKE 'Sprinkler' LIMIT 1;
  IF cid IS NULL THEN
    INSERT INTO equipment_categories(name, description) VALUES ('Sprinkler','Watering sprinklers') RETURNING id INTO cid;
  END IF;
  SELECT id INTO tid FROM equipment_types WHERE name ILIKE 'Sprinkler' LIMIT 1;
  IF tid IS NULL THEN
    INSERT INTO equipment_types(name, category_id) VALUES ('Sprinkler', cid);
  END IF;
END $$;

SELECT 'Sprinkler equipment fields added' as migration_status;

