import type { Id } from "@code/backend/convex/_generated/dataModel";
import {
  MobileAssistantChatScreen,
  MobileExpandedWorkScreen,
  MobileHomeScreen,
  MobileWorkListScreen,
  MobileWorkUnitDetailScreen,
} from "@code/ui/components/mobile-product";
import { useNavigate, useParams } from "react-router";

import { useMobileProjectWorkspace } from "@/hooks/use-mobile-project-workspace";
import { useMobileWorkspace } from "@/hooks/use-mobile-workspace";

export type MobileFlowScreen =
  | "assistant-chat"
  | "home"
  | "work-list"
  | "work-unit-detail";

interface MobileFlowPageProps {
  screen: MobileFlowScreen;
}

export const MobileFlowPage = ({ screen }: MobileFlowPageProps) => {
  const navigate = useNavigate();
  const { workUnitId } = useParams();
  const projectWorkspace = useMobileProjectWorkspace({
    selectedWorkUnitId: workUnitId,
  });
  const workspace = useMobileWorkspace({
    onCreateIssue: (title, body) =>
      projectWorkspace.raiseIssue({ body, title }),
    onSend: projectWorkspace.sendMessage,
  });
  const workPath = "/work";
  const chatPath = "/chat";

  const handleOpenUnit = (selectedWorkUnitId?: string) => {
    const targetId =
      selectedWorkUnitId ?? projectWorkspace.data.selectedWorkUnit?.id;
    if (!targetId) {
      navigate("/dashboard");
      return;
    }
    if (screen === "work-list" && !workspace.expanded) {
      workspace.setExpanded(true);
      return;
    }
    navigate(`/work/${targetId}`);
  };

  const screenProps = {
    composerValue: workspace.composerValue,
    createIssueBody: workspace.issueBody,
    createIssueTitle: workspace.issueTitle,
    data: projectWorkspace.data,
    onBack: () => navigate(workPath),
    onComposerChange: workspace.handleComposerChange,
    onComposerSubmit: workspace.handleComposerSubmit,
    onCreateBodyChange: workspace.setIssueBody,
    onCreateIssue: workspace.handleCreateIssueSubmit,
    onCreateIssueFromSignal: projectWorkspace.data.latestSignal?.projectId
      ? () =>
          void projectWorkspace.raiseIssueFromSignal(
            projectWorkspace.data.latestSignal?.id ?? ""
          )
      : undefined,
    onCreateTitleChange: workspace.setIssueTitle,
    onManageProjects: () => navigate("/dashboard"),
    onOpenAssistant: () => navigate(chatPath),
    onOpenUnit: handleOpenUnit,
    onProjectSelect: (projectId: string) =>
      projectWorkspace.projectWorkspace.setSelectedProjectId(
        projectId as Id<"projects">
      ),
    onReviewUnit: (reviewUrl: string) => {
      window.open(reviewUrl, "_blank", "noopener,noreferrer");
    },
    onStartUnit: (selectedIssueId: string) => {
      void projectWorkspace.startWorkUnit(
        selectedIssueId as Id<"projectIssues">
      );
    },
    onViewWork: () => navigate(workPath),
    pendingAction: projectWorkspace.projectWorkspace.pendingAction,
    statusMessage: workspace.statusMessage ?? projectWorkspace.error?.message,
  };

  if (screen === "assistant-chat") {
    return <MobileAssistantChatScreen {...screenProps} />;
  }
  if (screen === "home") {
    return <MobileHomeScreen {...screenProps} />;
  }
  if (screen === "work-unit-detail") {
    return <MobileWorkUnitDetailScreen {...screenProps} />;
  }
  if (workspace.expanded) {
    return <MobileExpandedWorkScreen {...screenProps} />;
  }
  return <MobileWorkListScreen {...screenProps} />;
};
