-- ============================================================================
--  One-time module rename:  fusion_authorizer_portal  ->  fusion_portal
-- ============================================================================
--  WHY THIS IS NEEDED
--  ------------------
--  Odoo identifies an installed module by its TECHNICAL NAME (the addon folder
--  name) and stores that name in many places: the module record itself, every
--  external ID (ir_model_data), each view's `key`, and the dependency rows of
--  every other module that depends on it.
--
--  Renaming only the folder + the in-code references makes Odoo treat
--  `fusion_portal` as a BRAND-NEW, uninstalled module while the old
--  `fusion_authorizer_portal` records stay behind. Installing fresh would then
--  DUPLICATE groups / mail templates / menus and ORPHAN all existing data
--  (assessments, fusion.pdf.template records, portal users' group links, ...).
--
--  This script renames the DB rows in place so the existing install simply
--  "becomes" fusion_portal, after which a normal  -u fusion_portal  reloads all
--  XML data cleanly under the new external IDs.
--
--  WHEN TO RUN
--  -----------
--  Run ONCE against every database that already has the module installed
--  (e.g. westin, entech), BEFORE the first  -u fusion_portal.
--  Fresh databases that never had fusion_authorizer_portal do NOT need this.
--  It is idempotent — re-running after the rename matches nothing and is a no-op.
--
--  HOW TO RUN
--  ----------
--    1. Stop Odoo (or do it during a maintenance window).
--    2. psql into the target DB and execute this file:
--         \i rename_module.sql
--       (or paste the BEGIN..COMMIT block).
--    3. Confirm the four "remaining" counts at the end are all 0.
--    4. Start Odoo with:   -u fusion_portal
--    5. (optional) hard-refresh browsers / clear bundle cache — the asset paths
--       changed from /fusion_authorizer_portal/... to /fusion_portal/...
-- ============================================================================

BEGIN;

-- 1) The module record itself.
UPDATE ir_module_module
   SET name = 'fusion_portal'
 WHERE name = 'fusion_authorizer_portal';

-- 2) Every external ID owned by the module (groups, views, templates, menus,
--    server actions, record rules, ACLs, cron, ...).
UPDATE ir_model_data
   SET module = 'fusion_portal'
 WHERE module = 'fusion_authorizer_portal';

-- 3) View keys are stored ON the view row (not derived from ir_model_data),
--    and the post_init hook / migrations search by this key prefix.
UPDATE ir_ui_view
   SET key = replace(key, 'fusion_authorizer_portal.', 'fusion_portal.')
 WHERE key LIKE 'fusion_authorizer_portal.%';

-- 4) Dependency rows of OTHER modules that depend on this one
--    (fusion_schedule, fusion_repairs, fusion_quotations, and any future ones).
UPDATE ir_module_module_dependency
   SET name = 'fusion_portal'
 WHERE name = 'fusion_authorizer_portal';

-- ---------------------------------------------------------------------------
-- Verification — every count below must be 0 after the updates above.
-- ---------------------------------------------------------------------------
SELECT 'ir_module_module'            AS table, count(*) AS remaining FROM ir_module_module            WHERE name   = 'fusion_authorizer_portal'
UNION ALL
SELECT 'ir_model_data',                       count(*)              FROM ir_model_data               WHERE module = 'fusion_authorizer_portal'
UNION ALL
SELECT 'ir_ui_view.key',                      count(*)              FROM ir_ui_view                  WHERE key  LIKE 'fusion_authorizer_portal.%'
UNION ALL
SELECT 'ir_module_module_dependency',         count(*)              FROM ir_module_module_dependency WHERE name   = 'fusion_authorizer_portal';

-- If all four show 0, COMMIT. Otherwise ROLLBACK and investigate.
COMMIT;

-- Optional: force a clean recompile of web asset bundles (paths changed).
--   DELETE FROM ir_attachment WHERE url LIKE '/web/assets/%';
-- (safe; Odoo regenerates bundles on next page load / -u.)
