import { useMemo } from "react"
import type { ExpertDefinition, WorkspaceSummary } from "@qjclaw/shared-types"
import {
  getExpertGuideContent,
  getProjectDisplayName,
  resolveExpertKey,
  resolveExpertVisualKey
} from "../experts/expertDisplay"
import type { ExpertProject } from "./ExpertTree"
import {
  buildHomeShortcutEntries,
  buildProjectExpertEntries,
  buildSidebarExpertEntries,
  buildStandaloneExpertEntries
} from "./expertEntries"

type ViewMode = "chat" | "experts" | "plugins" | "settings" | "knowledge"

interface UseSidebarModelOptions {
  workspace: WorkspaceSummary | null
  expertDefinitions: ExpertDefinition[]
  viewMode: ViewMode
  defaultChatName: string
}

export function useSidebarModel({
  workspace,
  expertDefinitions,
  viewMode,
  defaultChatName
}: UseSidebarModelOptions) {
  const projects = workspace?.projects ?? []
  const visibleProjects = useMemo(() => projects.filter((project) => !project.isBuiltinHome), [projects])
  const standaloneExpertDefinitions = useMemo(
    () => expertDefinitions.filter((definition) => definition.entryMode === "standalone"),
    [expertDefinitions]
  )
  const homeShortcutDefinitions = useMemo(
    () => expertDefinitions.filter((definition) => definition.entryMode === "home-chat-shortcut"),
    [expertDefinitions]
  )
  const standaloneExpertEntries = useMemo(
    () => buildStandaloneExpertEntries(standaloneExpertDefinitions, visibleProjects, defaultChatName),
    [defaultChatName, standaloneExpertDefinitions, visibleProjects]
  )
  const projectExpertEntries = useMemo(
    () => buildProjectExpertEntries(visibleProjects, standaloneExpertDefinitions, defaultChatName),
    [defaultChatName, standaloneExpertDefinitions, visibleProjects]
  )
  const homeShortcutEntries = useMemo(
    () => buildHomeShortcutEntries(homeShortcutDefinitions),
    [homeShortcutDefinitions]
  )
  const sidebarExpertEntries = useMemo(
    () => buildSidebarExpertEntries(standaloneExpertEntries, projectExpertEntries, homeShortcutEntries),
    [homeShortcutEntries, projectExpertEntries, standaloneExpertEntries]
  )
  const expertPageProjects = useMemo(
    () => [...standaloneExpertEntries, ...projectExpertEntries]
      .map((entry) => entry.project)
      .filter((project): project is ExpertProject => Boolean(project)),
    [projectExpertEntries, standaloneExpertEntries]
  )
  const activeProject = useMemo(() => {
    if (viewMode === "experts") {
      return expertPageProjects.find((project) => project.id === workspace?.currentProjectId) ?? expertPageProjects[0]
    }
    return visibleProjects.find((project) => project.id === workspace?.currentProjectId) ?? visibleProjects[0]
  }, [expertPageProjects, viewMode, visibleProjects, workspace?.currentProjectId])
  const activeExpertKey = useMemo(() => resolveExpertKey(activeProject), [activeProject])
  const activeExpertVisualKey = useMemo(() => resolveExpertVisualKey(activeProject), [activeProject])
  const activeExpertName = useMemo(() => getProjectDisplayName(activeProject, defaultChatName), [activeProject, defaultChatName])
  const activeExpertGuide = useMemo(() => getExpertGuideContent(activeProject), [activeProject])

  return {
    projects,
    visibleProjects,
    standaloneExpertDefinitions,
    homeShortcutDefinitions,
    standaloneExpertEntries,
    projectExpertEntries,
    homeShortcutEntries,
    sidebarExpertEntries,
    expertPageProjects,
    activeProject,
    activeExpertKey,
    activeExpertVisualKey,
    activeExpertName,
    activeExpertGuide
  }
}
