import { authStore, snapshotStore } from "@shared";
import {
  Power,
  LucideIcon,
  Building2,
  Map,
  Users,
  Earth,
  Landmark,
  GitBranch,
  Table,
  Split,
  PersonStanding,
  Cpu,
  RefreshCcw,
} from "lucide-react";
import { toast } from "react-toastify";

import carrierIcon from "./carrier.svg";

export const DRAWER_WIDTH = 300;

interface NavigationItem {
  id: string;
  label: string;
  icon?: LucideIcon | React.ReactNode;
  path?: string;
  requiredRoles?: string[];
  onClick?: () => void;
  nestedItems?: NavigationItem[];
  isActive?: boolean;
}

export const ROUTE_REQUIRED_RESOURCES: Record<string, string[]> = {
  "/": [],

  "/sight": ["sights"],
  "/sight/create": ["sights"],
  "/sight/:id/edit": ["sights"],

  "/devices": ["devices", "vehicles", "routes", "carriers", "snapshot_rw"],

  "/map": ["map"],

  "/media": ["sights"],
  "/media/:id": ["sights"],
  "/media/:id/edit": ["sights"],

  "/country": ["countries"],
  "/country/create": ["countries"],
  "/country/add": ["countries"],
  "/country/:id/edit": ["countries"],

  "/city": ["cities", "countries"],
  "/city/create": ["cities", "countries"],
  "/city/:id/edit": ["cities", "countries"],

  "/route": ["routes", "carriers"],
  "/route/create": ["routes", "carriers"],
  "/route/:id/edit": ["routes", "carriers"],

  "/user": ["users"],
  "/user/create": ["users"],
  "/user/:id/edit": ["users"],

  "/snapshot": ["snapshot_rw"],
  "/snapshot/create": ["snapshot_create", "devices_rw"],

  "/carrier": ["carriers"],
  "/carrier/create": ["carriers"],
  "/carrier/:id/edit": ["carriers"],

  "/station": ["stations"],
  "/station/create": ["stations"],
  "/station/:id": ["stations"],
  "/station/:id/edit": ["stations"],

  "/vehicle/create": ["devices"],
  "/vehicle/:id/edit": ["devices"],

  "/article": ["sights"],
  "/article/:id": ["sights"],
};

export const NAVIGATION_ITEMS: {
  primary: NavigationItem[];
  secondary: NavigationItem[];
} = {
  primary: [
    {
      id: "snapshots",
      label: "Экспорт",
      icon: GitBranch,
      path: "/snapshot",
      requiredRoles: ["snapshot_rw", "snapshot_create"],
    },
    {
      id: "map",
      label: "Карта",
      icon: Map,
      path: "/map",
    },
    {
      id: "devices",
      label: "Устройства",
      icon: Cpu,
      path: "/devices",
      requiredRoles: ["devices_ro", "devices_rw"],
    },
    {
      id: "users",
      label: "Пользователи",
      icon: Users,
      path: "/user",
      requiredRoles: ["users_ro", "users_rw"],
    },
    {
      id: "all",
      label: "Справочник",
      icon: Table,
      nestedItems: [
        {
          id: "attractions",
          label: "Достопримечательности",
          icon: Landmark,
          path: "/sight",
          requiredRoles: ["sights_ro", "sights_rw"],
        },
        {
          id: "stations",
          label: "Остановки",
          icon: PersonStanding,
          path: "/station",
          requiredRoles: ["stations_ro", "stations_rw"],
        },
        {
          id: "routes",
          label: "Маршруты",
          icon: Split,
          path: "/route",
          requiredRoles: ["routes_ro", "routes_rw"],
        },

        {
          id: "countries",
          label: "Страны",
          icon: Earth,
          path: "/country",
          requiredRoles: ["countries_ro", "countries_rw"],
        },
        {
          id: "cities",
          label: "Города",
          icon: Building2,
          path: "/city",
          requiredRoles: ["cities_ro", "cities_rw"],
        },
        {
          id: "carriers",
          label: "Перевозчики",
          // @ts-ignore
          icon: () => <img src={carrierIcon} alt="Перевозчики" />,
          path: "/carrier",
          requiredRoles: ["carriers_ro", "carriers_rw"],
        },
      ],
    },
  ],
  secondary: [
    {
      id: "clear-cache",
      label: "Очистить кэш",
      icon: RefreshCcw,
      onClick: () => {
        snapshotStore.clearStoreCache();
        toast.success("Кэш очищен");
      },
    },
    {
      id: "logout",
      label: "Выйти",
      icon: Power,
      onClick: () => {
        authStore.logout();
        window.location.href = "/login";
      },
    },
  ],
};

function collectRoles(list: NavigationItem[]): string[] {
  const roles = new Set<string>(["admin"]);
  const walk = (items: NavigationItem[]) => {
    for (const item of items) {
      item.requiredRoles?.forEach((r) => roles.add(r));
      item.nestedItems && walk(item.nestedItems);
    }
  };
  walk(list);
  return Array.from(roles);
}

export const ALL_ROLES = collectRoles(NAVIGATION_ITEMS.primary);

export const VEHICLE_TYPES = [
  { label: "Автобус", value: 3 },
  { label: "Троллейбус", value: 2 },
  { label: "Трамвай", value: 1 },
  { label: "Электробус", value: 4 },
  { label: "Электричка", value: 5 },
  { label: "Вагон метро", value: 6 },
  { label: "Вагон ЖД", value: 7 },
];

export const VEHICLE_MODELS = [
  { label: "71-431P «Довлатов»", value: "71-431P «Довлатов»" },
  { label: "71-638M-02 «Альтаир»", value: "71-638M-02 «Альтаир»" },
] as const;
