"""Seed the 2026-07 admin sidebar menus (AKN) that have no other seeder.

Run after migrating:  python master_seeder/seed_akn_updates.py

Adds these admin-only left-side menu items (their pages exist in the frontend
but were previously not seeded anywhere) as CHILDREN of the 'Master Data' group:

    Login History          /admin/login-history
    AI / LLM Providers     /admin/llm-providers
    AI Ranking Parameters  /admin/rank-parameters

They are admin_only=True with blank permission_code (admins see everything),
sitting under the existing 'Master Data' parent alongside its other children.

Also removes the deprecated 'Draft Candidates' child menu.

Idempotent — keyed on `route`, safe to re-run.
"""

import django
import os
import sys

# Admin menus nested under Master Data: (route, name, icon)
ADMIN_MENUS = [
    ("/admin/login-history", "Login History", "fa-solid fa-clock-rotate-left"),
    ("/admin/llm-providers", "AI / LLM Providers", "fa-solid fa-robot"),
    ("/admin/rank-parameters", "AI Ranking Parameters", "fa-solid fa-sliders"),
]


def seed_akn_updates():
    from apps.menus.models import Menu

    # Ensure the 'Master Data' parent exists (created by add_master_data_menus.py;
    # get_or_create here so this seeder is safe to run standalone too).
    master_data, _ = Menu.objects.get_or_create(
        name="Master Data", parent__isnull=True,
        defaults={
            "route": "", "icon": "fa-solid fa-database", "sort_order": 8,
            "permission_code": "", "is_active": True, "admin_only": False,
        },
    )

    # Place these after whatever children already exist under Master Data.
    last = master_data.children.order_by("-sort_order").first()
    next_order = (last.sort_order + 1) if last else 1

    for route, name, icon in ADMIN_MENUS:
        item, created = Menu.objects.get_or_create(
            route=route,
            defaults={
                "name": name,
                "parent": master_data,
                "icon": icon,
                "sort_order": next_order,
                "permission_code": "",
                "is_active": True,
                "admin_only": True,
            },
        )
        if not created:
            item.name = name
            item.parent = master_data          # move under Master Data if it was top-level
            item.icon = icon
            item.sort_order = next_order
            item.permission_code = ""
            item.is_active = True
            item.admin_only = True
            item.save()
        print(f"{'Created' if created else 'Updated'} '{name}' ({route}) under 'Master Data' [admin_only, sort={next_order}].")
        next_order += 1

    # Remove the deprecated 'Draft Candidates' menu (any parent).
    removed, _ = Menu.objects.filter(name="Draft Candidates").delete()
    if removed:
        print(f"Removed deprecated 'Draft Candidates' menu ({removed} row(s)).")
    else:
        print("No 'Draft Candidates' menu to remove.")


if __name__ == "__main__":
    sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.dev")
    django.setup()
    seed_akn_updates()
