-- Add address, city and country fields to clients
ALTER TABLE clients
    ADD COLUMN IF NOT EXISTS address varchar(255),
    ADD COLUMN IF NOT EXISTS city varchar(100),
    ADD COLUMN IF NOT EXISTS country varchar(100);

-- Add form elements for new client fields
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 35, 'address', 'address', 'address', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 6
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 35 AND field_name = 'address');

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 35, 'city', 'city', 'city', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 7
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 35 AND field_name = 'city');

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 35, 'country', 'country', 'country', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 8
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 35 AND field_name = 'country');

-- Update default table view
UPDATE table_views
SET columns = '["name","email","phone","address","city","country"]'
WHERE form_id = 35 AND name = 'Default';
