import {
  CalendarClock,
  CircleDot,
  Cog,
  Database,
  LayoutGrid,
  Gauge,
  Users,
  Workflow,
  Building2,
  type LucideIcon,
} from "lucide-react";
import { canAccessNavItem, getDepartmentHomeRoute, type AppNavKey } from "@/lib/access-control";
import type { DepartmentKey, UserRole } from "@/lib/types";

export type AppNavItem = {
  key: AppNavKey;
  href: string;
  label: string;
  icon: LucideIcon;
};

const NAV_DEFINITIONS: AppNavItem[] = [
  { key: "dashboard", href: "/dashboard", label: "Resumen", icon: LayoutGrid },
  { key: "financial-flow", href: "/financial-flow", label: "Flujo financiero", icon: Workflow },
  { key: "experienciometro", href: "/experienciometro", label: "Experienciómetro", icon: Gauge },
  { key: "departments", href: "/departments", label: "Departamento", icon: Building2 },
  { key: "initiatives", href: "/initiatives", label: "Iniciativas", icon: CircleDot },
  { key: "meetings", href: "/meetings", label: "Reuniones", icon: CalendarClock },
  { key: "people", href: "/people", label: "Personas", icon: Users },
  { key: "data-entry", href: "/data-entry", label: "Captura", icon: Database },
  { key: "settings", href: "/settings", label: "Configuración", icon: Cog },
];

export function getAppNavItems(role?: UserRole, department?: DepartmentKey | null): AppNavItem[] {
  return NAV_DEFINITIONS.filter((item) => {
    return canAccessNavItem(item.key, {
      role,
      department,
    });
  }).map((item) => {
    if (item.key === "departments") {
      if (role === "owner") {
        return item;
      }

      return {
        ...item,
        href: getDepartmentHomeRoute(department),
      };
    }

    if (department === "marketing" && item.key === "initiatives") {
      return {
        ...item,
        href: `${getDepartmentHomeRoute(department)}/initiatives`,
      };
    }

    if (department === "marketing" && item.key === "meetings") {
      return {
        ...item,
        href: `${getDepartmentHomeRoute(department)}/meetings`,
      };
    }

    if (department === "proyectos" && item.key === "meetings") {
      return {
        ...item,
        href: `${getDepartmentHomeRoute(department)}/meetings`,
      };
    }

    if (department === "proyectos" && item.key === "data-entry") {
      return {
        ...item,
        href: `${getDepartmentHomeRoute(department)}/capture`,
      };
    }

    return item;
  });
}
