-- Insert Client and Case forms with table views and modal definitions

-- Client form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
SELECT 35, 'Client', 'Client details', 'clients', NULL, NULL
WHERE NOT EXISTS (SELECT 1 FROM forms WHERE id = 35);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 35, 'name', 'name', 'name', 1, 1
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 35 AND field_name = 'name');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 35, 'phone', 'phone', 'phone', 1, 2
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 35 AND field_name = 'phone');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 35, 'email', 'email', 'email', 1, 3
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 35 AND field_name = 'email');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 35, 'description', 'description', 'description', 6, 4
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 35 AND field_name = 'description');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 35, 'comments', 'comments', 'comments', 6, 5
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 35 AND field_name = 'comments');

-- Case form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
SELECT 36, 'Case', 'Case record', 'cases', NULL, NULL
WHERE NOT EXISTS (SELECT 1 FROM forms WHERE id = 36);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 36, 'client_id', 'client_id', 'client_id', 2, 1
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 36 AND field_name = 'client_id');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 36, 'title', 'title', 'title', 1, 2
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 36 AND field_name = 'title');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 36, 'status', 'status', 'status', 4, 3
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 36 AND field_name = 'status');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 36, 'opened_at', 'opened_at', 'opened_at', 3, 4
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 36 AND field_name = 'opened_at');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 36, 'closed_at', 'closed_at', 'closed_at', 3, 5
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 36 AND field_name = 'closed_at');

-- Status options
INSERT INTO element_options (element_id, display_text, value)
SELECT fe.id, v.opt, v.opt
FROM (VALUES ('open'), ('closed'), ('pending')) AS v(opt)
CROSS JOIN LATERAL (
    SELECT id FROM form_elements WHERE form_id = 36 AND field_name = 'status'
) fe
WHERE NOT EXISTS (
    SELECT 1 FROM element_options eo
    WHERE eo.element_id = fe.id AND eo.value = v.opt
);

-- Table views
INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
SELECT 35, 'Default', '["name","email","phone"]', 'name', 'ASC'
WHERE NOT EXISTS (SELECT 1 FROM table_views WHERE form_id = 35 AND name = 'Default');
INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
SELECT 36, 'Default', '["client_id","title","status","opened_at","closed_at"]', 'opened_at', 'DESC'
WHERE NOT EXISTS (SELECT 1 FROM table_views WHERE form_id = 36 AND name = 'Default');

-- Clients modal
INSERT INTO ui_modals (name, config, tenant_id, branch_id)
SELECT 'Clients', '{"title":"Clients","header":[{"type":"button","label":"Add Client","formName":"Client"},{"type":"button","label":"Detail View","onClick":"openSelectedDetail"}],"body":[{"type":"table","options":{"tableName":"clients","showGlobalSearch":false,"addButton":false,"rowClick":"selectRecord"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}' , NULL, NULL
WHERE NOT EXISTS (SELECT 1 FROM ui_modals WHERE name = 'Clients');

-- Cases modal
INSERT INTO ui_modals (name, config, tenant_id, branch_id)
SELECT 'Cases', '{"title":"Cases","header":[{"type":"button","label":"Add Case","formName":"Case"},{"type":"button","label":"Detail View","onClick":"openSelectedDetail"}],"body":[{"type":"table","options":{"tableName":"cases","showGlobalSearch":false,"addButton":false,"rowClick":"selectRecord"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}' , NULL, NULL
WHERE NOT EXISTS (SELECT 1 FROM ui_modals WHERE name = 'Cases');

-- Case Manager modal
INSERT INTO ui_modals (name, config, tenant_id, branch_id)
SELECT 'Case Manager', '{"title":"Case Manager","header":[{"type":"button","label":"Add Client","formName":"Client"},{"type":"button","label":"Add Case","formName":"Case"},{"type":"button","label":"Detail View","onClick":"openSelectedDetail"}],"body":[{"type":"html","html":"<select id=\"cmClientSelect\"></select>"},{"type":"table","id":"cmCases","options":{"tableName":"cases","showGlobalSearch":false,"addButton":false,"rowClick":"selectRecord"}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}' , NULL, NULL
WHERE NOT EXISTS (SELECT 1 FROM ui_modals WHERE name = 'Case Manager');
