-- Remove obsolete subdomain field and add product/dashboard details to Tenant form

-- Delete subdomain element if it exists
DELETE FROM form_elements WHERE form_id = 39 AND field_name = 'subdomain';

-- Add new Tenant fields
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 39, 'product_name', 'product_name', 'product_name', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 4
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 39 AND field_name = 'product_name');

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

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

-- Update Tenant table view to remove subdomain column
UPDATE table_views
SET columns = '["name","custom_domain"]'
WHERE form_id = 39 AND name = 'Default';
