-- Phase 1: Database cleanup - Drop redundant tables and rename for admin clarity
-- Date: 2026-04-20

-- ============================================
-- DROP: Remove redundant external_roles table
-- Reason: roles.audience = 'EXTERNAL' already identifies external roles
-- This table just adds a 1:1 mapping with no extra fields
-- ============================================
DROP TABLE IF EXISTS external_roles;

-- ============================================
-- RENAME: Tables for admin clarity
-- ============================================

-- internal_roles → internal_role_details
ALTER TABLE internal_roles RENAME TO internal_role_details;

-- role_permissions → role_admin_permissions
ALTER TABLE role_permissions RENAME TO role_admin_permissions;

-- permissions → permission_definitions
ALTER TABLE permissions RENAME TO permission_definitions;

-- dashboard_configs → role_sidebar_configs
ALTER TABLE dashboard_configs RENAME TO role_sidebar_configs;

-- runtime_configs → role_runtime_configs
ALTER TABLE runtime_configs RENAME TO role_runtime_configs;

-- user_roles → user_role_assignments
ALTER TABLE user_roles RENAME TO user_role_assignments;

-- dashboard_widgets → role_dashboard_widgets
ALTER TABLE dashboard_widgets RENAME TO role_dashboard_widgets;

-- ============================================
-- UPDATE: Sequences for renamed tables
-- ============================================
ALTER SEQUENCE internal_roles_id_seq RENAME TO internal_role_details_id_seq;
ALTER SEQUENCE role_permissions_id_seq RENAME TO role_admin_permissions_id_seq;
ALTER SEQUENCE permissions_id_seq RENAME TO permission_definitions_id_seq;
ALTER SEQUENCE dashboard_configs_id_seq RENAME TO role_sidebar_configs_id_seq;
ALTER SEQUENCE runtime_configs_id_seq RENAME TO role_runtime_configs_id_seq;
ALTER SEQUENCE user_roles_id_seq RENAME TO user_role_assignments_id_seq;
ALTER SEQUENCE dashboard_widgets_id_seq RENAME TO role_dashboard_widgets_id_seq;
