
print("--- STARTING HOMEPAGE REPAIR ---")
try:
    website = env['website'].get_current_website()
    print(f"Target Website: {website.name}")

    # 1. Find and Archive COW (Customized) Homepage Views
    # These are usually created when you 'Edit' the homepage in the UI.
    # They shadow the underlying theme/generic view.
    cow_views = env['ir.ui.view'].search([
        ('key', '=', 'website.homepage'),
        ('website_id', '=', website.id),
        ('type', '=', 'qweb')
    ])
    
    if cow_views:
        print(f"Found {len(cow_views)} Blocking Custom View(s). Archiving them...")
        for cv in cow_views:
            print(f" -> Archiving View: {cv.name} (ID: {cv.id})")
            cv.write({'active': False, 'key': cv.key + '.archived.' + str(cv.id)}) # Change key to stop it from matching
    else:
        print("No Blocking Custom Views (COW) found.")

    # 2. Ensure Theme View is Active
    theme_view = env.ref('dine360_theme_chennora.custom_homepage', raise_if_not_found=False)
    if theme_view:
        print(f"Theme View found: {theme_view.name}")
        if not theme_view.active:
            theme_view.write({'active': True})
            print(" -> Activated Theme View")
        else:
            print(" -> Theme View is already active")
            
        # Optional: Force priority check
        if theme_view.priority < 50:
            theme_view.write({'priority': 50})
            print(" -> Enforced Priority 50")
            
    else:
        print("ERROR: Theme View 'dine360_theme_chennora.custom_homepage' NOT FOUND. Please upgrade the module.")

    env.cr.commit()
    print("--- REPAIR COMPLETE. PLEASE REFRESH HOMEPAGE ---")

except Exception as e:
    print(f"ERROR: {e}")

