-- Update Vehicle Issue Report form field types
UPDATE form_elements
SET type_id = (SELECT id FROM element_types WHERE name = 'select-addable' LIMIT 1),
    options_table = 'vehicles',
    options_label_column = 'vehicle_number',
    options_value_column = 'id'
WHERE form_id = 34 AND field_name = 'vehicle_id';

UPDATE form_elements
SET type_id = (SELECT id FROM element_types WHERE name = 'dropdown' LIMIT 1)
WHERE form_id = 34 AND field_name = 'report_type';

-- Insert report_type options if missing
INSERT INTO element_options (element_id, display_text, value)
SELECT fe.id, opt, opt
FROM (
    VALUES ('minor'), ('major'), ('breakdown')
) AS v(opt)
CROSS JOIN LATERAL (SELECT id FROM form_elements WHERE form_id = 34 AND field_name = 'report_type') fe
WHERE NOT EXISTS (
    SELECT 1 FROM element_options eo
    WHERE eo.element_id = fe.id AND eo.value = v.opt
);
