import { IconSchool } from '@material-hu/icons/tabler';
import List from '@material-hu/mui/List';

import HuSidebarNavItem from '@material-hu/components/design-system/Sidebar/components/NavItem';
import { type NavItemProps } from '@material-hu/components/design-system/Sidebar/types';

import {
  LACOMER_COURSES_CURRICULUM_ONLY_ES_TITLE,
  LACOMER_COURSES_ONLY_ES_TITLE,
} from 'src/constants/learning';
import { useAuth } from 'src/contexts/JWTContext';
import useCommunityFeature from 'src/hooks/useCommunityFeature';
import useCoursesTitle from 'src/pages/dashboard/learning/courses/hooks/useCoursesTitle';
import { coursesRoutes } from 'src/pages/dashboard/learning/courses/routes';
import useLearningTitle from 'src/pages/dashboard/learning/hooks/useLearningTitle';
import { learningLacomerRoutes } from 'src/pages/dashboard/learning/lacomer/routes';
import usePathsTitle from 'src/pages/dashboard/learning/paths/hooks/usePathsTitle';
import { pathsRoutes } from 'src/pages/dashboard/learning/paths/routes';
import { useSessionsTitle } from 'src/pages/dashboard/learning/sessions/hooks/useSessionsTitle';
import { sessionsRoutes } from 'src/pages/dashboard/learning/sessions/routes';
import { CommunityFeature } from 'src/types/communityFeatures';
import { insertIf } from 'src/utils/arrays';
import { UserPermissions } from 'src/utils/permissions';

import itemsConfig from '../constants';
import useGetSidebarIcon from '../hooks/useSidebarIcon';
import { type SidebarItemProps } from '../types';
import { getItemIsActive, getSubItemIsActive } from '../utils';

const { key } = itemsConfig.learnings;

const NavLearningsItem = ({
  isCollapsed,
  onClick,
  isActive: isActiveProp,
}: SidebarItemProps) => {
  const { permissions } = useAuth();

  const canViewLacomerWebviews = useCommunityFeature(
    CommunityFeature.VIEW_LACOMER_WEBVIEWS,
  );

  const hasAccessToCourses = permissions.includes(UserPermissions.VIEW_COURSES);
  const hasAccessToPaths = permissions.includes(UserPermissions.VIEW_PATHS);
  const hasAccessToSessions = permissions.includes(
    UserPermissions.VIEW_LEARNING_SESSIONS,
  );

  const learningTitle = useLearningTitle();
  const coursesTitle = useCoursesTitle();
  const pathsTitle = usePathsTitle();
  const { sessionsTitle } = useSessionsTitle();

  const subItems: NavItemProps[] = [
    ...insertIf(canViewLacomerWebviews, {
      key: 'lacomer-courses',
      path: learningLacomerRoutes.courses(),
      title: LACOMER_COURSES_ONLY_ES_TITLE,
      depth: 1,
    }),
    ...insertIf(canViewLacomerWebviews, {
      key: 'lacomer-courses-curriculum',
      path: learningLacomerRoutes.coursesCurriculum(),
      title: LACOMER_COURSES_CURRICULUM_ONLY_ES_TITLE,
      depth: 1,
    }),
    ...insertIf(hasAccessToCourses, {
      key: 'courses',
      path: coursesRoutes.base(),
      title: coursesTitle,
      depth: 1,
    }),
    ...insertIf(hasAccessToPaths, {
      key: 'paths',
      path: pathsRoutes.base(),
      title: pathsTitle,
      depth: 1,
    }),
    ...insertIf(hasAccessToSessions, {
      key: 'sessions',
      path: sessionsRoutes.base(),
      title: sessionsTitle,
      depth: 1,
      info: { isNew: true },
    }),
  ];

  const { Icon } = useGetSidebarIcon({
    defaultIcon: IconSchool,
    key: 'Learnings',
  });

  const isActive =
    isActiveProp ?? subItems.some(item => getItemIsActive(item.path));

  const renderSubItems = () => {
    if (isCollapsed) return null;

    return (
      <List
        disablePadding
        sx={{ '& > *:not(:last-child)': { mb: 0 } }}
      >
        {subItems.map((item, index) => (
          <HuSidebarNavItem
            {...item}
            key={item.key}
            isLastChild={index === subItems.length - 1}
            isCollapsed={isCollapsed}
            active={isActiveProp ?? getSubItemIsActive(item.path)}
            onClick={onClick}
          />
        ))}
      </List>
    );
  };

  return (
    <HuSidebarNavItem
      key={key}
      depth={0}
      title={learningTitle}
      icon={<Icon />}
      path={coursesRoutes.base()}
      isCollapsed={isCollapsed}
      onClick={onClick}
      active={isActive}
    >
      {renderSubItems()}
    </HuSidebarNavItem>
  );
};

export default NavLearningsItem;
