-- Catalog module switch repair for Product Tags and Grouped Products.
-- By default this preserves the current on/off values.
-- To manually force a value, change NULL to 1 or 0 before running.

SET @enable_product_tags_module := NULL;
SET @enable_grouped_products_module := NULL;

SET @sql := (
  SELECT IF(
    COUNT(*) = 0,
    'ALTER TABLE store_settings ADD COLUMN enable_product_tags_module TINYINT(1) NOT NULL DEFAULT 0',
    'SELECT 1'
  )
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'store_settings'
    AND COLUMN_NAME = 'enable_product_tags_module'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql := (
  SELECT IF(
    COUNT(*) = 0,
    'ALTER TABLE store_settings ADD COLUMN enable_grouped_products_module TINYINT(1) NOT NULL DEFAULT 0',
    'SELECT 1'
  )
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'store_settings'
    AND COLUMN_NAME = 'enable_grouped_products_module'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

INSERT INTO store_settings (id) VALUES (1)
ON DUPLICATE KEY UPDATE id = id;

UPDATE store_settings
SET
  enable_product_tags_module = COALESCE(@enable_product_tags_module, enable_product_tags_module),
  enable_grouped_products_module = COALESCE(@enable_grouped_products_module, enable_grouped_products_module)
WHERE id = 1;

INSERT INTO permissions (permission_code, module_code, action_code, permission_name, description, is_active, sort_order)
VALUES
  ('product_tags.page', 'product_tags', 'page', 'Product Tags Page', 'Open product tags management.', 1, 2310),
  ('product_tags.view', 'product_tags', 'view', 'View Product Tags', 'View product tags.', 1, 2311),
  ('product_tags.manage', 'product_tags', 'manage', 'Manage Product Tags', 'Create, edit, assign, and delete product tags.', 1, 2312),
  ('product_tags.create', 'product_tags', 'create', 'Create Product Tags', 'Create custom product tags.', 1, 2313),
  ('product_tags.edit', 'product_tags', 'edit', 'Edit Product Tags', 'Edit product tag names, visibility, color, and sort order.', 1, 2314),
  ('product_tags.delete', 'product_tags', 'delete', 'Delete Product Tags', 'Delete product tags.', 1, 2315),
  ('product_tags.assign', 'product_tags', 'assign', 'Assign Product Tags', 'Assign or remove product tags from products.', 1, 2316),
  ('grouped_products.page', 'grouped_products', 'page', 'Grouped Products Page', 'Open grouped products management.', 1, 2330),
  ('grouped_products.view', 'grouped_products', 'view', 'View Grouped Products', 'View grouped product bundles.', 1, 2331),
  ('grouped_products.manage', 'grouped_products', 'manage', 'Manage Grouped Products', 'Create, edit, and delete grouped product bundles.', 1, 2332)
ON DUPLICATE KEY UPDATE
  module_code = VALUES(module_code),
  action_code = VALUES(action_code),
  permission_name = VALUES(permission_name),
  description = VALUES(description),
  is_active = VALUES(is_active),
  sort_order = VALUES(sort_order);

INSERT INTO api_permission_map (api_prefix, module_code, permission_code, description)
VALUES
  ('/api/product-tags', 'product_tags', 'product_tags.page', 'Product tag APIs.'),
  ('/api/product-tags/public', 'product_tags', NULL, 'Public visible product tags storefront API.'),
  ('/api/grouped-products', 'grouped_products', 'grouped_products.page', 'Grouped product admin APIs.'),
  ('/api/store/grouped-products', 'grouped_products', NULL, 'Public grouped product storefront APIs.')
ON DUPLICATE KEY UPDATE
  module_code = VALUES(module_code),
  permission_code = VALUES(permission_code),
  description = VALUES(description);

SELECT
  enable_product_tags_module,
  enable_grouped_products_module
FROM store_settings
WHERE id = 1;


