-- PAYE deduction settings for Fiji
-- Applies separately to residents and non-residents
-- Values not provided default to 0 or NULL as appropriate

INSERT INTO deduction_settings (
    name, start_salary, end_salary, base_amount, percent,
    company_contribution, start_date, end_date, applies_to,
    scope, country_id, tenant_id, branch_id
) VALUES
    ('PAYE', 0, 30000.0, 0, 0, 0, NULL, NULL, 'resident', 'country', 1, NULL, NULL),
    ('PAYE', 30001, 50000.0, 0, 18, 0, NULL, NULL, 'resident', 'country', 1, NULL, NULL),
    ('PAYE', 50001, 270000.0, 3600, 20, 0, NULL, NULL, 'resident', 'country', 1, NULL, NULL),
    ('PAYE', 270001, 300000.0, 47600, 33, 0, NULL, NULL, 'resident', 'country', 1, NULL, NULL),
    ('PAYE', 300001, 350000.0, 57500, 34, 0, NULL, NULL, 'resident', 'country', 1, NULL, NULL),
    ('PAYE', 350001, 400000.0, 74500, 35, 0, NULL, NULL, 'resident', 'country', 1, NULL, NULL),
    ('PAYE', 400001, 450000.0, 92000, 36, 0, NULL, NULL, 'resident', 'country', 1, NULL, NULL),
    ('PAYE', 450001, 500000.0, 110000, 37, 0, NULL, NULL, 'resident', 'country', 1, NULL, NULL),
    ('PAYE', 500001, 1000000.0, 128500, 38, 0, NULL, NULL, 'resident', 'country', 1, NULL, NULL),
    ('PAYE', 1000001, NULL, 318500, 39, 0, NULL, NULL, 'resident', 'country', 1, NULL, NULL),
    ('PAYE', 0, 30000.0, 0, 20, 0, NULL, NULL, 'non_resident', 'country', 1, NULL, NULL),
    ('PAYE', 30001, 50000.0, 6000, 20, 0, NULL, NULL, 'non_resident', 'country', 1, NULL, NULL),
    ('PAYE', 50001, 270000.0, 10000, 20, 0, NULL, NULL, 'non_resident', 'country', 1, NULL, NULL),
    ('PAYE', 270001, 300000.0, 54000, 33, 0, NULL, NULL, 'non_resident', 'country', 1, NULL, NULL),
    ('PAYE', 300001, 350000.0, 63900, 34, 0, NULL, NULL, 'non_resident', 'country', 1, NULL, NULL),
    ('PAYE', 350001, 400000.0, 80900, 35, 0, NULL, NULL, 'non_resident', 'country', 1, NULL, NULL),
    ('PAYE', 400001, 450000.0, 98400, 36, 0, NULL, NULL, 'non_resident', 'country', 1, NULL, NULL),
    ('PAYE', 450001, 500000.0, 116400, 37, 0, NULL, NULL, 'non_resident', 'country', 1, NULL, NULL),
    ('PAYE', 500001, 1000000.0, 134900, 38, 0, NULL, NULL, 'non_resident', 'country', 1, NULL, NULL),
    ('PAYE', 1000001, NULL, 324900, 39, 0, NULL, NULL, 'non_resident', 'country', 1, NULL, NULL),
    ('FNPF', 0, NULL, 0, 8, 10, NULL, NULL, 'resident', 'country', 1, NULL, NULL);

-- Default application settings
INSERT INTO settings (
    name, value, category, tenant_id, branch_id
) VALUES
    ('default_currency', 'FJD', 'finance', NULL, NULL),
    ('timezone', 'Pacific/Fiji', 'system', 1, 1),
    ('policy_publish_webhook', '', 'system', NULL, NULL),
    ('policy_update_webhook', '', 'system', NULL, NULL);

-- Default hazard types
INSERT INTO ohs_hazard_types (name, tenant_id, branch_id) VALUES
    ('Physical', NULL, NULL),
    ('Chemical', NULL, NULL),
    ('Biological', NULL, NULL),
    ('Psychosocial', NULL, NULL),
    ('Ergonomic', NULL, NULL);
-- Default incident types
INSERT INTO ohs_incident_types (name, form_id, tenant_id, branch_id) VALUES
    ('Injury', 4, NULL, NULL),
    ('Property Damage', 4, NULL, NULL),
    ('Environmental', 4, NULL, NULL),
    ('Near Miss', 4, NULL, NULL),
    ('First Aid', 4, NULL, NULL),
    ('Medical Treatment', 4, NULL, NULL),
    ('LTI', 4, NULL, NULL),
    ('Fatality', 4, NULL, NULL);

-- Default permit types
INSERT INTO permit_types (name)
VALUES
    ('Hot Work'),
    ('Confined Space Entry'),
    ('Electrical Work'),
    ('Working at Heights'),
    ('Excavation'),
    ('General Work'),
    ('Cold Work');

-- Default form element types
INSERT INTO element_types (tenant_id, branch_id, name, html_type)
VALUES
    (NULL, NULL, 'text', 'text'),
    (NULL, NULL, 'number', 'number'),
    (NULL, NULL, 'date', 'date'),
    (NULL, NULL, 'dropdown', 'select'),
    (NULL, NULL, 'checkbox', 'checkbox');

-- Additional types required by default forms
INSERT INTO element_types (tenant_id, branch_id, name, html_type)
VALUES
    (NULL, NULL, 'textarea', 'textarea'),
    (NULL, NULL, 'file', 'file'),
    (NULL, NULL, 'datetime', 'datetime-local');
INSERT INTO element_types (tenant_id, branch_id, name, html_type)
    VALUES (NULL, NULL, 'select-addable', 'select');
INSERT INTO element_types (tenant_id, branch_id, name, html_type)
    VALUES (NULL, NULL, 'multiselect-search', 'multiselect');
INSERT INTO element_types (tenant_id, branch_id, name, html_type)
    VALUES (NULL, NULL, 'email', 'email');
INSERT INTO element_types (tenant_id, branch_id, name, html_type)
    VALUES (NULL, NULL, 'text-multiple', 'text-multiple');

-- Default OHS incident form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (4, 'OHS Incident', 'Default incident report form', 'ohs_incidents', NULL, NULL);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
VALUES
    (4, 'injury_date', 'injury_date', 'injury_date', 3, 1),
    (4, 'injury_time', 'injury_time', 'injury_time', 1, 2),
    (4, 'classification', 'classification', 'classification', 4, 3),
    (4, 'nature', 'nature', 'nature', 1, 4),
    (4, 'location', 'location', 'location', 1, 5),
    (4, 'status', 'status', 'status', 4, 6);

INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
VALUES (4, 'Default', '["status","injury_date","classification","location"]', 'injury_date', 'DESC');

-- Default table views for other OHS lists
INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
VALUES
    (5, 'Default', '["date","location","inspector_id"]', 'date', 'DESC'),
    (6, 'Default', '["title","description","doc_path","location","department_id","analysis_date","review_date"]', 'title', 'ASC');

-- Job safety analysis form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (6, 'Job Safety Analysis', 'Record JSA details', 'ohs_jsas', NULL, NULL);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
VALUES
    (6, 'title', 'title', 'title', 1, 1),
    (6, 'description', 'description', 'description', 1, 2),
    (6, 'doc_path', 'doc_path', 'doc_path', 7, 3),
    (6, 'location', 'location', 'location', 1, 4),
    (6, 'department_id', 'department_id', 'department_id', 2, 5),
    (6, 'analysis_date', 'analysis_date', 'analysis_date', 3, 6),
    (6, 'prepared_by', 'prepared_by', 'prepared_by', 2, 7),
    (6, 'team_member_ids', 'team_member_ids', 'team_member_ids', 6, 8),
    (6, 'ppe_required', 'ppe_required', 'ppe_required', 6, 9),
    (6, 'permit_requirements', 'permit_requirements', 'permit_requirements', 6, 10),
    (6, 'review_date', 'review_date', 'review_date', 3, 11);

-- Default hazard form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (7, 'OHS Hazard', 'Default hazard report form', NULL, NULL, NULL);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
VALUES
    (7, 'description', 'description', 'description', 1, 1),
    (7, 'location', 'location', 'location', 1, 2),
    (7, 'hazard_type_id', 'hazard_type_id', 'hazard_type_id', 4, 3),
    (7, 'probability', 'probability', 'probability', 4, 4),
    (7, 'severity', 'severity', 'severity', 4, 5),
    (7, 'risk_level', 'risk_level', 'risk_level', 2, 6),
    (7, 'latitude', 'latitude', 'latitude', 2, 7),
    (7, 'longitude', 'longitude', 'longitude', 2, 8);

-- SCORM package management form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (8, 'SCORM Package', 'Upload SCORM content', NULL, NULL, NULL);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
VALUES
    (8, 'course_id', 'course_id', 'course_id', 2, 1),
    (8, 'title', 'title', 'title', 1, 2),
    (8, 'scorm_zip', 'scorm_zip', 'scorm_zip', 7, 3),
    (8, 'is_scorm', 'is_scorm', 'is_scorm', 5, 4),
    (8, 'caption', 'caption', 'caption', 7, 5),
    (8, 'language_code', 'language_code', 'language_code', 1, 6);

-- Training path form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (9, 'Training Path', 'Create a learning path', NULL, NULL, NULL);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
VALUES
    (9, 'name', 'name', 'name', 1, 1),
    (9, 'description', 'description', 'description', 1, 2),
    (9, 'department_id', 'department_id', 'department_id', 2, 3),
    (9, 'role_id', 'role_id', 'role_id', 2, 4);

-- Safety induction form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (10, 'Safety Induction', 'Record employee safety inductions', NULL, NULL, NULL);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, is_required, order_index)
VALUES
    (10, 'employee_id', 'employee_id', 'employee_id', 2, TRUE, 1),
    (10, 'induction_date', 'induction_date', 'induction_date', 3, TRUE, 2),
    (10, 'document_id', 'document_id', 'document_id', 7, FALSE, 3);

-- Employee license form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (11, 'Employee License', 'Track employee licenses', NULL, NULL, NULL);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, is_required, order_index)
VALUES
    (11, 'employee_id', 'employee_id', 'employee_id', 2, TRUE, 1),
    (11, 'license_type', 'license_type', 'license_type', 1, TRUE, 2),
    (11, 'license_number', 'license_number', 'license_number', 1, TRUE, 3),
    (11, 'expiry_date', 'expiry_date', 'expiry_date', 3, FALSE, 4),
    (11, 'document_id', 'document_id', 'document_id', 7, FALSE, 5);

-- Training requirement form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (12, 'Training Requirement', 'Assign mandatory courses per project', NULL, NULL, NULL);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, is_required, order_index)
VALUES
    (12, 'project_id', 'project_id', 'project_id', 2, TRUE, 1),
    (12, 'role_id', 'role_id', 'role_id', 2, FALSE, 2),
    (12, 'location_id', 'location_id', 'location_id', 2, FALSE, 3),
    (12, 'course_id', 'course_id', 'course_id', 2, TRUE, 4);

INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
VALUES (12, 'Default', '["project_id","course_id"]', 'project_id', 'ASC');

-- Time clock form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (13, 'Time Clock', 'Record time clock entries', 'time_clocks', NULL, NULL);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
VALUES
    (13, 'user_id', 'user_id', 'user_id', 2, 1),
    (13, 'clock_type', 'clock_type', 'clock_type', 4, 2),
    (13, 'lat', 'lat', 'lat', 2, 3),
    (13, 'lng', 'lng', 'lng', 2, 4),
    (13, 'location_id', 'location_id', 'location_id', 2, 5),
    (13, 'stationApproved', 'stationApproved', 'stationApproved', 5, 6),
    (13, 'security_type', 'security_type', 'security_type', 1, 7),
    (13, 'clocked_at', 'clocked_at', 'clocked_at', 8, 8);

INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
VALUES (13, 'Default', '["user_id","clock_type","location_id","clocked_at"]', 'clocked_at', 'DESC');

-- Timesheet form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (14, 'Timesheet', 'Record daily timesheet totals', 'timesheets', NULL, NULL);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
VALUES
    (14, 'user_id', 'user_id', 'user_id', 2, 1),
    (14, 'date', 'date', 'date', 3, 2),
    (14, 'standard_hours', 'standard_hours', 'standard_hours', 2, 3),
    (14, 'overtime_hours', 'overtime_hours', 'overtime_hours', 2, 4),
    (14, 'break_hours', 'break_hours', 'break_hours', 2, 5),
    (14, 'approved', 'approved', 'approved', 5, 6);

INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
VALUES (14, 'Default', '["user_id","date","standard_hours","overtime_hours","break_hours","approved"]', 'date', 'DESC');

-- Permit application form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (15, 'Permit Application', 'Default permit application form', 'permit_applications', NULL, NULL);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
VALUES
    (15, 'permit_type_id', 'permit_type_id', 'permit_type_id', 2, 1),
    (15, 'applicant_id', 'applicant_id', 'applicant_id', 2, 2),
    (15, 'description', 'description', 'description', 6, 3),
    (15, 'work_location', 'work_location', 'work_location', 1, 4),
    (15, 'equipment', 'equipment', 'equipment', 1, 5),
    (15, 'hazard_assessment', 'hazard_assessment', 'hazard_assessment', 6, 6),
    (15, 'control_measures', 'control_measures', 'control_measures', 6, 7),
    (15, 'ppe_required', 'ppe_required', 'ppe_required', 6, 8),
    (15, 'loto_instructions', 'loto_instructions', 'loto_instructions', 6, 9),
    (15, 'emergency_response', 'emergency_response', 'emergency_response', 6, 10),
    (15, 'issuer_id', 'issuer_id', 'issuer_id', 2, 11),
    (15, 'supervisor_id', 'supervisor_id', 'supervisor_id', 2, 12);

INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
VALUES (15, 'Default', '["permit_type_id","applicant_id","status","created_at"]', 'created_at', 'DESC');

-- Default groups
INSERT INTO groups (name, description)
VALUES
  ('Drivers', 'Drivers allowed to operate fleet vehicles'),
  ('Mechanics', 'Vehicle maintenance team'),
  ('Fleet Managers', 'Fleet management supervisors');

-- Vehicle registration form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (29, 'Vehicle Registration', 'Capture fleet vehicle details', 'vehicles', NULL, NULL);

INSERT INTO form_elements (
    form_id, label, field_name, data_column, type_id,
    options_table, options_label_column, options_value_column, order_index)
VALUES
    (29, 'vehicle_number', 'vehicle_number', 'vehicle_number', 1, NULL, NULL, NULL, 1),
    (29, 'vin', 'vin', 'vin', 1, NULL, NULL, NULL, 2),
    (29, 'engine_number', 'engine_number', 'engine_number', 1, NULL, NULL, NULL, 3),
    (29, 'mileage', 'mileage', 'mileage', 2, NULL, NULL, NULL, 4),
    (29, 'make', 'make', 'make', 9, 'vehicle_makes', 'name', 'id', 5),
    (29, 'model', 'model', 'model', 9, 'vehicle_models', 'name', 'id', 6),
    (29, 'type', 'type', 'type', 9, 'vehicle_types', 'name', 'id', 7),
    (29, 'year', 'year', 'year', 2, NULL, NULL, NULL, 8),
    (29, 'fitness_expires', 'fitness_expires', 'fitness_expires', 3, NULL, NULL, NULL, 9);

INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
VALUES (29, 'Default', '["vehicle_number","make","model","year"]', 'vehicle_number', 'ASC');

-- Trip log form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (30, 'Trip Log', 'Record vehicle trips with mileage', NULL, NULL, NULL);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
VALUES
    (30, 'vehicle_id', 'vehicle_id', 'vehicle_id', 2, 1),
    (30, 'driver_id', 'driver_id', 'driver_id', 2, 2),
    (30, 'trip_date', 'trip_date', 'trip_date', 3, 3),
    (30, 'start_time', 'start_time', 'start_time', 8, 4),
    (30, 'end_time', 'end_time', 'end_time', 8, 5),
    (30, 'start_location', 'start_location', 'start_location', 1, 6),
    (30, 'end_location', 'end_location', 'end_location', 1, 7),
    (30, 'start_mileage', 'start_mileage', 'start_mileage', 2, 8),
    (30, 'end_mileage', 'end_mileage', 'end_mileage', 2, 9),
    (30, 'distance_km', 'distance_km', 'distance_km', 2, 10),
    (30, 'status', 'status', 'status', 1, 11);

INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
VALUES (30, 'Default', '["vehicle_id","driver_id","trip_date","start_time","end_time","start_mileage","end_mileage","distance_km","status"]', 'start_time', 'DESC');

-- Service type form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (31, 'Service Type', 'Define maintenance intervals', 'service_types', NULL, NULL);

INSERT INTO form_elements (
    form_id, label, field_name, data_column, type_id,
    options_table, options_label_column, options_value_column, order_index)
VALUES
    (31, 'name', 'name', 'name', 1, NULL, NULL, NULL, 1),
    (31, 'description', 'description', 'description', 6, NULL, NULL, NULL, 2),
    (31, 'make', 'make', 'make', 9, 'vehicle_makes', 'name', 'id', 3),
    (31, 'model', 'model', 'model', 9, 'vehicle_models', 'name', 'id', 4),
    (31, 'vehicle_type', 'type', 'type', 9, 'vehicle_types', 'name', 'id', 5),
    (31, 'km_interval', 'km_interval', 'km_interval', 2, NULL, NULL, NULL, 6),
    (31, 'month_interval', 'month_interval', 'month_interval', 2, NULL, NULL, NULL, 7);

INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
VALUES (31, 'Default', '["name","make","model","km_interval","month_interval"]', 'name', 'ASC');

-- Service Types modal
INSERT INTO ui_modals (name, config, tenant_id, branch_id) VALUES (
  'Service Types',
  '{"title":"Service Types","header":[{"type":"button","label":"Add Service Type","formName":"Service Type"}],"body":[{"type":"table","options":{"tableName":"service_types","showGlobalSearch":false,"addButton":false}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL
);

-- Vehicles modal
INSERT INTO ui_modals (name, config, tenant_id, branch_id) VALUES (
  'Vehicles',
  '{"title":"Vehicles","header":[{"type":"button","label":"Add Vehicle","formName":"Vehicle Registration"}],"body":[{"type":"table","options":{"tableName":"vehicles","showGlobalSearch":false,"addButton":false}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL
);

-- User groups form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (32, 'User Group', 'Manage user groups and permissions', 'groups', NULL, NULL);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
VALUES
    (32, 'name', 'name', 'name', 1, 1),
    (32, 'description', 'description', 'description', 6, 2),
    (32, 'parent_id', 'parent_id', 'parent_id', 2, 3);

INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
VALUES (32, 'Default', '["name","description"]', 'name', 'ASC');

-- User Groups modal
INSERT INTO ui_modals (name, config, tenant_id, branch_id) VALUES (
  'User Groups',
  '{"title":"User Groups","header":[{"type":"button","label":"Add Group","formName":"User Group"}],"body":[{"type":"table","options":{"tableName":"groups","showGlobalSearch":true,"addButton":false}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL
);

-- Group members form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (33, 'Group Member', 'Assign users to groups', 'user_group_memberships', NULL, NULL);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
VALUES
    (33, 'user_id', 'user_id', 'user_id', 2, 1),
    (33, 'group_id', 'group_id', 'group_id', 2, 2),
    (33, 'role', 'role', 'role', 1, 3);

INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
VALUES (33, 'Default', '["user_id","group_id","role"]', 'user_id', 'ASC');

-- Group Members modal
INSERT INTO ui_modals (name, config, tenant_id, branch_id) VALUES (
  'Group Members',
  '{"title":"Group Members","header":[{"type":"button","label":"Add Member","formName":"Group Member"}],"body":[{"type":"table","options":{"tableName":"user_group_memberships","showGlobalSearch":true,"addButton":false}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL
);

-- Users modal
INSERT INTO ui_modals (name, config, tenant_id, branch_id) VALUES (
  'Users',
  '{"title":"Users","header":[{"type":"button","label":"Add User","formName":"User"}],"body":[{"type":"table","options":{"tableName":"users","showGlobalSearch":true,"addButton":false}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL
);

-- Dashboard card permissions modal
INSERT INTO ui_modals (name, config, tenant_id, branch_id) VALUES (
  'dashboard_card_permissions',
  '{"title":"Dashboard Card Permissions","body":[{"type":"html","html":"<select id=\"dcpTenantSelect\"><option value=\"\">All</option></select><div class=\"tabs mb-2\" id=\"dcp-tabs\"><a class=\"tab tab-bordered tab-active\" data-tab=\"groups\">Groups</a><a class=\"tab tab-bordered\" data-tab=\"users\">Users</a></div>"},{"type":"table","id":"dcpGroups","options":{"tableName":"groups","addButton":false,"showGlobalSearch":false,"actionsHeader":"Allow"}},{"type":"table","id":"dcpUsers","options":{"tableName":"users","addButton":false,"showGlobalSearch":false,"actionsHeader":"Allow"}}],"footer":[{"type":"button","label":"Save","onClick":"saveDashboardCardPerms"},{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL
);

-- Dashboard card permissions manager modal
INSERT INTO ui_modals (name, config, tenant_id, branch_id) VALUES (
  'dashboard_card_permissions_manager',
  '{"title":"Card Permissions Manager","body":[{"type":"html","html":"<select id=\"dcpTenantSelect\"></select><select id=\"dcpmGroupSelect\"></select><select id=\"dcpmUserSelect\"></select>"},{"type":"html","id":"dcpmCards","html":""},{"type":"html","html":"<div id=\"dcpm-detail\" class=\"hidden\"></div>"}],"footer":[{"type":"button","label":"Save","onClick":"saveCardPermissions"},{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL
);

-- Trip Logs modal
INSERT INTO ui_modals (name, config, tenant_id, branch_id) VALUES (
  'Trip Logs',
  '{"title":"Trip Logs","header":[{"type":"button","label":"Map View","onClick":"openTripMapView"}],"body":[{"type":"table","options":{"tableName":"vehicle_trips","addButton":false,"rowClick":"selectRecord","showGlobalSearch":true}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL
);

-- Vehicle Issue Report form
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
VALUES (34, 'Vehicle Issue Report', 'Report vehicle issues', 'vehicle_reports', NULL, NULL);

INSERT INTO form_elements (
    form_id, label, field_name, data_column, type_id,
    options_table, options_label_column, options_value_column, order_index)
VALUES
    (34, 'vehicle_id', 'vehicle_id', 'vehicle_id', 9,
        'vehicles', 'vehicle_number', 'id', 1),
    (34, 'report_type', 'report_type', 'report_type', 4,
        NULL, NULL, NULL, 2),
    (34, 'description', 'description', 'description', 6,
        NULL, NULL, NULL, 3);

INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
VALUES (34, 'Default', '["vehicle_id","report_type","status"]', 'created_at', 'DESC');

-- Report type options
INSERT INTO element_options (element_id, display_text, value) VALUES
    ((SELECT id FROM form_elements WHERE form_id = 34 AND field_name = 'report_type'), 'minor', 'minor'),
    ((SELECT id FROM form_elements WHERE form_id = 34 AND field_name = 'report_type'), 'major', 'major'),
    ((SELECT id FROM form_elements WHERE form_id = 34 AND field_name = 'report_type'), 'breakdown', 'breakdown');

-- Vehicle Reports modal
INSERT INTO ui_modals (name, config, tenant_id, branch_id) VALUES (
  'Vehicle Reports',
  '{"title":"Vehicle Reports","header":[{"type":"button","label":"Add Report","formName":"Vehicle Issue Report"},{"type":"button","label":"Detail View","onClick":"openSelectedDetail"}],"body":[{"type":"table","id":"vehicleReports","options":{"tableName":"vehicle_reports","showGlobalSearch":false,"addButton":false,"rowClick":"selectRecord","filters":{"status":"open"},"apiRowActions":[{"buttonText":"Accept","apiUrl":"../api/vehicle_reports/{id}/assign"}]}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL
);

-- Fleet Calendar modal
INSERT INTO ui_modals (name, config, tenant_id, branch_id) VALUES (
  'Fleet Calendar',
  '{"title":"Fleet Calendar","body":[{"type":"calendar","options":{"source":"fleet","showAddButton":false}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
  NULL,
  NULL
);

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', (SELECT id FROM element_types WHERE name='email' LIMIT 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,
    options_table, options_label_column, options_value_column, order_index)
SELECT 36, 'client', 'client_id', 'client_id',
    (SELECT id FROM element_types WHERE name = 'dropdown' LIMIT 1),
    'clients', 'name', 'id', 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');
INSERT INTO form_elements (
    form_id, label, field_name, data_column, type_id,
    options_table, options_label_column, options_value_column, order_index)
SELECT 36, 'assigned_representative', 'lead_rep_id', 'lead_rep_id',
    (SELECT id FROM element_types WHERE name = 'dropdown' LIMIT 1),
    'lawyers', 'full_name', 'id', 6
WHERE NOT EXISTS (
    SELECT 1 FROM form_elements WHERE form_id = 36 AND field_name = 'lead_rep_id'
);
INSERT INTO form_elements (
    form_id, label, field_name, data_column, type_id, order_index
)
SELECT 36, 'description', 'description', 'description',
    (SELECT id FROM element_types WHERE name = 'textarea' LIMIT 1), 7
WHERE NOT EXISTS (
    SELECT 1 FROM form_elements WHERE form_id = 36 AND field_name = 'description'
);
INSERT INTO form_elements (
    form_id, label, field_name, data_column, type_id, order_index
)
SELECT 36, 'initial_instructions', 'initial_instructions', 'initial_instructions',
    (SELECT id FROM element_types WHERE name = 'textarea' LIMIT 1), 8
WHERE NOT EXISTS (
    SELECT 1 FROM form_elements WHERE form_id = 36 AND field_name = 'initial_instructions'
);
INSERT INTO form_elements (
    form_id, label, field_name, data_column, type_id, order_index
)
SELECT 36, 'documents', 'document_id', 'document_id',
    (SELECT id FROM element_types WHERE name = 'file' LIMIT 1), 9
WHERE NOT EXISTS (
    SELECT 1 FROM form_elements WHERE form_id = 36 AND field_name = 'document_id'
);

-- 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');

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

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 37, 'full_name', 'full_name', 'full_name', 1, 1
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 37 AND field_name = 'full_name');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 37, 'email', 'email', 'email', (SELECT id FROM element_types WHERE name='email' LIMIT 1), 2
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 37 AND field_name = 'email');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 37, 'phone', 'phone', 'phone', 1, 3
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 37 AND field_name = 'phone');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 37, 'date_joined', 'date_joined', 'date_joined', 3, 4
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 37 AND field_name = 'date_joined');

INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
SELECT 37, 'Default', '["full_name","email","phone","date_joined"]', 'full_name', 'ASC'
WHERE NOT EXISTS (SELECT 1 FROM table_views WHERE form_id = 37 AND name = 'Default');

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

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 39, 'name', 'name', 'name', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 1
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 39 AND field_name = 'name');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 39, 'subdomain', 'subdomain', 'subdomain', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 2
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 39 AND field_name = 'subdomain');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 39, 'custom_domain', 'custom_domain', 'custom_domain', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 3
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 39 AND field_name = 'custom_domain');
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 39, 'settings', 'settings', 'settings', (SELECT id FROM element_types WHERE name='textarea' LIMIT 1), 4
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 39 AND field_name = 'settings');

INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
SELECT 39, 'Default', '["name","subdomain","custom_domain"]', 'name', 'ASC'
WHERE NOT EXISTS (SELECT 1 FROM table_views WHERE form_id = 39 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');

-- Lawyers modal
INSERT INTO ui_modals (name, config, tenant_id, branch_id)
SELECT 'Lawyers', '{"title":"Lawyers","header":[{"type":"button","label":"Add Lawyer","formName":"Lawyer"},{"type":"button","label":"Detail View","onClick":"openSelectedDetail"}],"body":[{"type":"table","options":{"tableName":"lawyers","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 = 'Lawyers');

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

-- Admins form and modal

-- Create Admin form if it doesn't exist
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
SELECT 40, 'Admin', 'Admin user', 'users', NULL, NULL
WHERE NOT EXISTS (SELECT 1 FROM forms WHERE id = 40);

-- Form fields
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 40, 'full_name', 'full_name', 'full_name', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 1
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 40 AND field_name='full_name');

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 40, 'email', 'emails', 'emails', (SELECT id FROM element_types WHERE name='text-multiple' LIMIT 1), 2
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 40 AND field_name='email');

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 40, 'phones', 'phones', 'phones', (SELECT id FROM element_types WHERE name='text-multiple' LIMIT 1), 3
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 40 AND field_name='phones');

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

INSERT INTO form_elements (
    form_id, label, field_name, data_column, type_id,
    options_table, options_label_column, options_value_column, order_index)
SELECT 40, 'tenant_id', 'tenant_id', 'tenant_id',
    (SELECT id FROM element_types WHERE name='dropdown' LIMIT 1),
    'tenants', 'name', 'id', 5
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 40 AND field_name='tenant_id');

INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
SELECT 40, 'Default', '["full_name","email","tenant_id"]', 'full_name', 'ASC'
WHERE NOT EXISTS (SELECT 1 FROM table_views WHERE form_id = 40 AND name='Default');

-- Admins modal definition
INSERT INTO ui_modals (name, config, tenant_id, branch_id)
SELECT 'Admins', '{"title":"Admins","header":[{"type":"button","label":"Add Admin","formName":"Admin"}],"body":[{"type":"table","options":{"tableName":"users","filters":{"is_admin":"1"},"showGlobalSearch":false,"addButton":false}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}' , NULL, NULL
WHERE NOT EXISTS (SELECT 1 FROM ui_modals WHERE name = 'Admins');

-- User form

-- Create User form if it doesn't exist
INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
SELECT 41, 'User', 'User account details', 'users', NULL, NULL
WHERE NOT EXISTS (SELECT 1 FROM forms WHERE id = 41);

-- Form fields
INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 41, 'full_name', 'full_name', 'full_name', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 1
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 41 AND field_name = 'full_name');

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 41, 'email', 'emails', 'emails', (SELECT id FROM element_types WHERE name='text-multiple' LIMIT 1), 2
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 41 AND field_name = 'emails');

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

INSERT INTO form_elements (
    form_id, label, field_name, data_column, type_id,
    options_table, options_label_column, options_value_column, order_index)
SELECT 41, 'tenant_id', 'tenant_id', 'tenant_id',
    (SELECT id FROM element_types WHERE name='dropdown' LIMIT 1),
    'tenants', 'name', 'id', 4
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 41 AND field_name = 'tenant_id');

INSERT INTO form_elements (
    form_id, label, field_name, data_column, type_id,
    options_table, options_label_column, options_value_column, order_index)
SELECT 41, 'groups', 'groups', 'groups',
    (SELECT id FROM element_types WHERE name='multiselect-search' LIMIT 1),
    'groups', 'name', 'id', 5
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 41 AND field_name = 'groups');

-- Default table view
INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
SELECT 41, 'Default', '["full_name","emails","tenant_id"]', 'full_name', 'ASC'
WHERE NOT EXISTS (SELECT 1 FROM table_views WHERE form_id = 41 AND name = 'Default');

-- Default dashboard card actions
INSERT INTO ui_actions (page, action_key, button_label, button_id, workflow_event) VALUES
('Dashboard','card_permissions','Permissions','Accounts','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ActionBuilder','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Admins','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','AnalyticsDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ApplicationFieldDefs','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Applications','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ApprovalQueue','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','AttendanceDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Calendar','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','CareerPaths','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','CareerReadiness','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Careers','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','CaseManagement','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Companies','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','CompetencyLibrary','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','CompetencyManagement','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','CompetencyMatrix','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ComplianceCalendar','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ComplianceDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ComplianceReports','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ContractApprovals','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ContractRepository','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','CountryDeductions','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','CountryHolidays','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','CreateUser','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','CustomModule','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','CustomizeDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','DeductionSettings','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Deductions','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','DigitalWallet','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Disciplinary','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','DmsAdmin','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','EmergencyPreparedness','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','EmployeeLicenses','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','EmployeeTypes','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Employees','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ExecutiveDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ExitManagement','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ExpiringDocuments','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Feedback','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','FormBuilder','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','GoalAlignment','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','GoalDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','GoalProgress','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','GoalStrategyAlignment','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','GoalTemplates','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','GoodsReceipts','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','HRPerformanceDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','HazardManagement','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','HazardTypes','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','IncidentHeatmap','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','IncidentSummaryDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','IncidentTypes','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','InterviewFeedback','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Interviews','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ItemAssignments','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','JSATrainingStatus','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','JobOpeningTemplates','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','KanbanBoard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','LeaveRequests','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','LedgerEntries','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Locations','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Logout','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ManagePolicies','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','MyGoals','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','MyPolicies','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','MyRequest','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','MyReviews','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','NearMissDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','NewHire','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','OHSAssignmentRules','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','OHSIncidents','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','OHSInspections','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','OHSInvestigationDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','OHSJSA','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','OhsReports','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','OpenPosition','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','OvertimeSettings','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','PageConfigBuilder','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','PageCustomizer','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','PayCycles','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Payments','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','PayrollProcess','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','PayrollSubmissions','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','PayrollVerification','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Payslips','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','PerformanceDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','PerformanceManagement','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Permits','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','PolicyApprovals','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Properties','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','PropertyRates','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','PurchaseOrders','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','PurchaseRequest','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','RFQs','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Reconciliation','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Reporting','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ReviewCalibration','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ReviewCycles','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','RiskMatrixDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','RootCauseDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','SafetyDocuments','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','SafetyInductions','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ScheduleInterviews','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','SentimentDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','SystemSettings','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','TeamGoalsDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','TeamLearningTracker','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Tenants','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','TimeClockLocations','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','TimeClocks','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Training','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','TrainingCalendar','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','TrainingCompletion','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','TrainingDashboard','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','TrainingGamification','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','TrainingProfile','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','TripLogs','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','UpcomingReviews','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','Users','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','VendorInvoices','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','ViewBuilder','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','WorkflowTasks','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','procurement','dashboard.card.permissions'),
('Dashboard','card_permissions','Permissions','CardPermissionsManager','dashboard.card.permissions');

-- Default workflow to launch the Dashboard Card Permissions modal
INSERT INTO workflows (name, trigger_event, definition, is_active)
VALUES (
  'Dashboard Card Permissions',
  'dashboard.card.permissions',
  '{"conditions":[]}',
  TRUE
);

INSERT INTO workflow_steps (workflow_id, step_order, step_type, config)
VALUES (
  (SELECT id FROM workflows WHERE name='Dashboard Card Permissions' LIMIT 1),
  1,
  'open_modal',
  '{"modal":"dashboard_card_permissions"}'
);
-- Default dashboard card permissions
INSERT INTO dashboard_card_permissions (card_slug, group_id)
VALUES
  ('Vehicle_Reports', (SELECT id FROM groups WHERE name='Mechanics' LIMIT 1)),
  ('Vehicle_Reports', (SELECT id FROM groups WHERE name='Fleet Managers' LIMIT 1)),
  ('Fleet_Calendar', (SELECT id FROM groups WHERE name='Fleet Managers' LIMIT 1));
-- Default vehicle makes and models
INSERT INTO vehicle_makes (name, tenant_id, branch_id)
VALUES ('Toyota', NULL, NULL);

INSERT INTO vehicle_models (make_id, name, tenant_id, branch_id)
VALUES ((SELECT id FROM vehicle_makes WHERE name='Toyota' LIMIT 1), 'Corolla', NULL, NULL);

-- Default vehicle service workflow to schedule maintenance after registration
INSERT INTO workflows (name, trigger_event, definition, is_active)
VALUES ('Vehicle Service Setup', 'vehicle.registered', '{"conditions": []}', TRUE);

INSERT INTO workflow_steps (workflow_id, step_order, step_type, config)
VALUES (
  (SELECT id FROM workflows WHERE name='Vehicle Service Setup' LIMIT 1),
  1,
  'schedule_services',
  '{}'
);
