-- Migration: standardize system-level table names with sys_ prefix
-- Strategy:
-- 1) Rename physical tables to sys_*
-- 2) Create compatibility views with legacy names

SET @rename_users_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'users' AND table_type = 'BASE TABLE'
    ) AND NOT EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'sys_users'
    ),
    'RENAME TABLE users TO sys_users',
    'SELECT 1'
  )
);
PREPARE stmt FROM @rename_users_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @rename_roles_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'roles' AND table_type = 'BASE TABLE'
    ) AND NOT EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'sys_roles'
    ),
    'RENAME TABLE roles TO sys_roles',
    'SELECT 1'
  )
);
PREPARE stmt FROM @rename_roles_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @rename_menus_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'menus' AND table_type = 'BASE TABLE'
    ) AND NOT EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'sys_menus'
    ),
    'RENAME TABLE menus TO sys_menus',
    'SELECT 1'
  )
);
PREPARE stmt FROM @rename_menus_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @rename_rmp_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'role_menu_permissions' AND table_type = 'BASE TABLE'
    ) AND NOT EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'sys_role_menu_permissions'
    ),
    'RENAME TABLE role_menu_permissions TO sys_role_menu_permissions',
    'SELECT 1'
  )
);
PREPARE stmt FROM @rename_rmp_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @rename_dict_data_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'dict_data' AND table_type = 'BASE TABLE'
    ) AND NOT EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'sys_dict_data'
    ),
    'RENAME TABLE dict_data TO sys_dict_data',
    'SELECT 1'
  )
);
PREPARE stmt FROM @rename_dict_data_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @rename_sys_param_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'system_parameters' AND table_type = 'BASE TABLE'
    ) AND NOT EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'sys_system_parameters'
    ),
    'RENAME TABLE system_parameters TO sys_system_parameters',
    'SELECT 1'
  )
);
PREPARE stmt FROM @rename_sys_param_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Drop existing legacy-name views if present, then recreate compatibility views.
DROP VIEW IF EXISTS users;
DROP VIEW IF EXISTS roles;
DROP VIEW IF EXISTS menus;
DROP VIEW IF EXISTS role_menu_permissions;
DROP VIEW IF EXISTS dict_data;
DROP VIEW IF EXISTS system_parameters;

CREATE VIEW users AS SELECT * FROM sys_users;
CREATE VIEW roles AS SELECT * FROM sys_roles;
CREATE VIEW menus AS SELECT * FROM sys_menus;
CREATE VIEW role_menu_permissions AS SELECT * FROM sys_role_menu_permissions;
CREATE VIEW dict_data AS SELECT * FROM sys_dict_data;
CREATE VIEW system_parameters AS SELECT * FROM sys_system_parameters;
