import type { DepartmentKey, UserRole } from "@/lib/types";

export type AppNavKey =
  | "dashboard"
  | "financial-flow"
  | "experienciometro"
  | "departments"
  | "initiatives"
  | "meetings"
  | "people"
  | "data-entry"
  | "settings";

type AccessContext = {
  role?: UserRole | "lead";
  department?: DepartmentKey | null;
};

const SHARED_NAV: AppNavKey[] = ["meetings", "settings", "data-entry", "departments"];

const DEPARTMENT_NAV: Record<DepartmentKey, AppNavKey[]> = {
  marketing: ["initiatives", "people"],
  administracion: ["people"],
  capital_humano: ["people"],
  operaciones: [],
  proyectos: [],
};

const DEPARTMENT_HOME_ROUTES: Record<DepartmentKey, string> = {
  marketing: "/departments/marketing",
  administracion: "/departments/human-capital",
  capital_humano: "/departments/human-capital",
  operaciones: "/departments/operations",
  proyectos: "/departments/projects",
};

function isOwner(role: UserRole | undefined): boolean {
  return role === "owner";
}

function normalizeRole(role: AccessContext["role"]): UserRole | undefined {
  if (role === "lead") {
    return "leader";
  }

  if (role === "owner" || role === "leader" || role === "employee") {
    return role;
  }

  return undefined;
}

export function getDepartmentHomeRoute(department?: DepartmentKey | null): string {
  if (!department) {
    return DEPARTMENT_HOME_ROUTES.marketing;
  }

  return DEPARTMENT_HOME_ROUTES[department];
}

export function canAccessNavItem(navKey: AppNavKey, context: AccessContext): boolean {
  const role = normalizeRole(context.role);
  const { department } = context;

  if (!role) {
    return false;
  }

  if (isOwner(role)) {
    return true;
  }

  if (navKey === "experienciometro") {
    return role === "leader";
  }

  if (navKey === "dashboard") {
    return role === "leader";
  }

  if (navKey === "financial-flow") {
    return false;
  }

  if (SHARED_NAV.includes(navKey)) {
    return navKey !== "departments" || Boolean(department);
  }

  if (!department) {
    return false;
  }

  return DEPARTMENT_NAV[department].includes(navKey);
}

function canAccessDepartmentPath(pathname: string, department?: DepartmentKey | null): boolean {
  const requested = pathname.split("/")[2] ?? "";
  if (!department) {
    return false;
  }

  const allowedPath = getDepartmentHomeRoute(department).replace("/departments/", "");
  return requested === allowedPath;
}

export function canAccessPath(context: AccessContext, pathname: string): boolean {
  const role = normalizeRole(context.role);
  const { department } = context;

  if (!role) {
    return false;
  }

  if (isOwner(role)) {
    return true;
  }

  if (pathname.startsWith("/dashboard")) {
    return role === "owner" || role === "leader";
  }

  if (pathname === "/departments") {
    return Boolean(department);
  }

  if (pathname.startsWith("/financial-flow")) {
    return canAccessNavItem("financial-flow", context);
  }

  if (pathname.startsWith("/experienciometro")) {
    return canAccessNavItem("experienciometro", context);
  }

  if (pathname.startsWith("/departments/")) {
    return canAccessDepartmentPath(pathname, department);
  }

  if (pathname.startsWith("/initiatives")) {
    if (department === "marketing" || department === "proyectos") {
      return false;
    }

    return canAccessNavItem("initiatives", context);
  }

  if (pathname.startsWith("/meetings")) {
    if (department === "marketing" || department === "proyectos") {
      return false;
    }

    return canAccessNavItem("meetings", context);
  }

  if (pathname.startsWith("/people")) {
    return canAccessNavItem("people", context);
  }

  if (pathname.startsWith("/data-entry")) {
    return canAccessNavItem("data-entry", context);
  }

  if (pathname.startsWith("/settings")) {
    return canAccessNavItem("settings", context);
  }

  if (pathname.startsWith("/api/invitations")) {
    return role === "owner" || role === "leader";
  }

  if (pathname.startsWith("/api/experienciometro")) {
    return role === "owner" || role === "leader";
  }

  return true;
}
