// #region SidebarNavigationTest:Module [TYPE Function]
// @SEMANTICS: tests, sidebar, navigation, rbac, permissions
// @PURPOSE: Verifies RBAC-based sidebar sections and category visibility.
// @LAYER UI (Tests)
// @RELATION DEPENDS_ON -> [EXT:frontend:SidebarNavigation]

import { describe, it, expect } from "vitest";
import { buildSidebarCategories, buildSidebarSections } from "../sidebarNavigation";

interface I18nNavRecord {
  [key: string]: string | undefined;
}

interface I18nState {
  nav: I18nNavRecord;
}

interface UserRole {
  name?: string;
  permissions?: Array<string | { resource?: string; action?: string }>;
}

interface User {
  roles: UserRole[];
}

const i18nState: I18nState = {
  nav: {
    section_resources: "Resources",
    section_operations: "Operations",
    section_system: "System",
    dashboards: "Dashboards",
    overview: "Overview",
    health_center: "Health Center",
    datasets: "Datasets",
    all_datasets: "All Datasets",
    dataset_review_workspace: "Review Workspace",
    migration: "Migration",
    migration_overview: "Overview",
    migration_mappings: "Mappings",
    git: "Git",
    git_repo_status: "Repository Status",
    translation: "Translation",
    translation_jobs: "Jobs",
    translation_dictionaries: "Dictionaries",
    translation_history: "History",
    reports: "Reports",
    all_reports: "All Reports",
    tools: "Tools",
    tools_mapper: "Dataset Mapper",
    tools_debug: "System Debug",
    tools_storage: "File Storage",
    tools_backups: "Backup Manager",
    storage: "Storage",
    backups: "Backups",
    repositories: "Repositories",
    settings: "Settings",
    admin: "Admin",
    admin_users: "User Management",
    admin_roles: "Role Management",
    admin_settings: "ADFS Config",
    admin_llm: "LLM Providers",
    profile: "Profile",
  },
};

function makeUser(roles: UserRole[]): User {
  return { roles };
}

describe("sidebarNavigation", () => {
  it("shows sections structure with correct section IDs", () => {
    // Admin user sees all sections
    const user = makeUser([
      {
        name: "Admin",
        permissions: [],
      },
    ]);
    const sections = buildSidebarSections(i18nState, user);
    const sectionIds = sections.map((s) => s.id);

    expect(sectionIds).toEqual(["resources", "operations", "system"]);
  });

  it("shows only categories available to a non-admin user", () => {
    const user = makeUser([
      {
        name: "Operator",
        permissions: [
          { resource: "plugin:migration", action: "READ" },
          { resource: "tasks", action: "READ" },
        ],
      },
    ]);

    const categories = buildSidebarCategories(i18nState, user);
    const categoryIds = categories.map((category) => category.id);

    // migration requires plugin:migration → visible
    // git/tools have no requiredPermission → always visible
    // translation requires translate.job → hidden
    // storage requires plugin:storage → hidden
    // admin requires admin:* → hidden
    expect(categoryIds).toEqual([
      "dashboards",
      "datasets",
      "migration",
      "git",
      "reports",
      "tools",
    ]);
  });

  it("hides admin category when user has no admin permissions", () => {
    const user = makeUser([
      {
        name: "Viewer",
        permissions: [{ resource: "plugin:migration", action: "READ" }],
      },
    ]);

    const categories = buildSidebarCategories(i18nState, user);
    const adminCategory = categories.find((category) => category.id === "admin");

    expect(adminCategory).toBeUndefined();
  });

  it("shows full admin category for admin role", () => {
    const user = makeUser([
      {
        name: "Admin",
        permissions: [],
      },
    ]);

    const categories = buildSidebarCategories(i18nState, user);
    const adminCategory = categories.find((category) => category.id === "admin");

    expect(adminCategory).toBeDefined();
    expect(adminCategory!.subItems.map((item: { path: string }) => item.path)).toEqual([
      "/admin/users",
      "/admin/roles",
      "/admin/settings",
      "/admin/settings/llm",
    ]);
  });

  it("shows git and tools for users without plugin permissions", () => {
    const user = makeUser([
      {
        name: "Basic",
        permissions: [],
      },
    ]);

    const categories = buildSidebarCategories(i18nState, user);
    const categoryIds = categories.map((category) => category.id);

    // Profile is now in the sidebar footer, not a nav category
    // Git and Tools have no requiredPermission → always visible
    expect(categoryIds).toContain("git");
    expect(categoryIds).toContain("tools");
  });
});
// #endregion SidebarNavigationTest:Module
