from odoo import api, SUPERUSER_ID

def uninstall_hook(cr, registry):
    """
    Synchronized uninstallation: When Dine360 Restaurant Suite is uninstalled,
    automatically trigger uninstallation for all its core sub-modules.
    """
    env = api.Environment(cr, SUPERUSER_ID, {})
    modules_to_uninstall = [
        'dine360_dashboard',
        'dine360_restaurant',
        'dine360_theme_chennora',
        'dine360_kds',
        'dine360_reservation'
    ]
    
    # Search for these modules if they are installed
    modules = env['ir.module.module'].search([
        ('name', 'in', modules_to_uninstall),
        ('state', '=', 'installed')
    ])
    
    if modules:
        # Mark modules for uninstallation
        # button_immediate_uninstall will trigger the full process including registry reload
        modules.button_immediate_uninstall()
