import { useEffect, useMemo, useState } from 'react';

import { Button } from '@renderer/components/ui/button';
import { Dialog, DialogContent, DialogFooter, DialogHeader } from '@renderer/components/ui/dialog';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@renderer/components/ui/tabs';
import { useMemberStats } from '@renderer/hooks/useMemberStats';
import { useStore } from '@renderer/store';
import { selectMemberMessagesForTeamMember } from '@renderer/store/slices/teamSlice';
import {
  buildMemberLaunchDiagnosticsPayload,
  getMemberLaunchDiagnosticsErrorMessage,
  hasMemberLaunchDiagnosticsDetails,
  hasMemberLaunchDiagnosticsError,
} from '@renderer/utils/memberLaunchDiagnostics';
import {
  getRuntimeMemorySourceLabel,
  resolveMemberRuntimeSummary,
} from '@renderer/utils/memberRuntimeSummary';
import { isLeadMember } from '@shared/utils/leadDetection';
import { BarChart3, FolderOpen, Loader2 } from 'lucide-react';

import { buildMemberActivityEntries } from './memberActivityEntries';
import { MemberDetailHeader } from './MemberDetailHeader';
import { MemberDetailStats } from './MemberDetailStats';
import { type MemberActivityFilter, type MemberDetailTab } from './memberDetailTypes';
import { MemberLaunchDiagnosticsButton } from './MemberLaunchDiagnosticsButton';
import { MemberMessagesTab } from './MemberMessagesTab';
import { MemberStatsTab } from './MemberStatsTab';
import { MemberTasksTab } from './MemberTasksTab';
import { MemberWorkspaceTab } from './MemberWorkspaceTab';

import type { TeamLaunchParams } from '@renderer/store/slices/teamSlice';
import type {
  LeadActivityState,
  MemberSpawnStatusEntry,
  ResolvedTeamMember,
  TeamAgentRuntimeEntry,
  TeamTaskWithKanban,
} from '@shared/types';

interface MemberDetailDialogProps {
  open: boolean;
  member: ResolvedTeamMember | null;
  teamName: string;
  members: ResolvedTeamMember[];
  tasks: TeamTaskWithKanban[];
  initialTab?: MemberDetailTab;
  initialActivityFilter?: MemberActivityFilter;
  isTeamAlive?: boolean;
  isTeamProvisioning?: boolean;
  isLaunchSettling?: boolean;
  leadActivity?: LeadActivityState;
  spawnEntry?: MemberSpawnStatusEntry;
  runtimeEntry?: TeamAgentRuntimeEntry;
  runtimeRunId?: string | null;
  launchParams?: TeamLaunchParams;
  onClose: () => void;
  onSendMessage: () => void;
  onAssignTask: () => void;
  onTaskClick: (task: TeamTaskWithKanban) => void;
  onRemoveMember?: () => void;
  onRestartMember?: (memberName: string) => Promise<void> | void;
  onUpdateRole?: (memberName: string, role: string | undefined) => Promise<void> | void;
  updatingRole?: boolean;
  onViewMemberChanges?: (memberName: string, filePath?: string) => void;
}

export const MemberDetailDialog = ({
  open,
  member,
  teamName,
  members,
  tasks,
  initialTab = 'tasks',
  initialActivityFilter = 'all',
  isTeamAlive,
  isTeamProvisioning,
  isLaunchSettling,
  leadActivity,
  spawnEntry,
  runtimeEntry,
  runtimeRunId,
  launchParams,
  onClose,
  onSendMessage,
  onAssignTask,
  onTaskClick,
  onRemoveMember,
  onRestartMember,
  onUpdateRole,
  updatingRole,
  onViewMemberChanges,
}: MemberDetailDialogProps): React.JSX.Element | null => {
  const memberTasks = useMemo(
    () => (member ? tasks.filter((t) => t.owner === member.name) : []),
    [tasks, member]
  );
  const memberMessages = useStore((state) =>
    selectMemberMessagesForTeamMember(state, teamName, member?.name ?? null)
  );
  const memberActivityCount = useMemo(() => {
    if (!member) {
      return 0;
    }
    return buildMemberActivityEntries({
      teamName,
      memberName: member.name,
      members,
      tasks,
      messages: memberMessages,
    }).length;
  }, [member, memberMessages, members, tasks, teamName]);

  const inProgressTasks = useMemo(
    () => memberTasks.filter((t) => t.status === 'in_progress').length,
    [memberTasks]
  );

  const completedTasks = useMemo(
    () => memberTasks.filter((t) => t.status === 'completed').length,
    [memberTasks]
  );

  const [activeTab, setActiveTab] = useState<MemberDetailTab>(initialTab);
  const [restarting, setRestarting] = useState(false);
  const [restartError, setRestartError] = useState<string | null>(null);

  const runtimeSummary = useMemo(
    () =>
      member
        ? resolveMemberRuntimeSummary(member, launchParams, spawnEntry, runtimeEntry)
        : undefined,
    [launchParams, member, runtimeEntry, spawnEntry]
  );
  const memorySourceLabel = getRuntimeMemorySourceLabel(runtimeEntry);
  const restartInFlight =
    spawnEntry?.launchState === 'starting' ||
    spawnEntry?.launchState === 'runtime_pending_bootstrap' ||
    spawnEntry?.launchState === 'runtime_pending_permission';
  const launchDiagnosticsPayload = useMemo(
    () =>
      member
        ? buildMemberLaunchDiagnosticsPayload({
            teamName,
            runId: runtimeRunId,
            memberName: member.name,
            spawnEntry,
            runtimeEntry,
          })
        : null,
    [member, runtimeEntry, runtimeRunId, spawnEntry, teamName]
  );
  const showCopyDiagnostics =
    launchDiagnosticsPayload != null &&
    hasMemberLaunchDiagnosticsError(launchDiagnosticsPayload) &&
    hasMemberLaunchDiagnosticsDetails(launchDiagnosticsPayload);
  const launchErrorMessage = launchDiagnosticsPayload
    ? getMemberLaunchDiagnosticsErrorMessage(launchDiagnosticsPayload)
    : undefined;

  useEffect(() => {
    if (!open || !member) {
      return;
    }
    setActiveTab(initialTab);
    setRestartError(null);
    setRestarting(false);
  }, [initialTab, member, open]);

  const {
    stats: memberStats,
    loading: statsLoading,
    error: statsError,
  } = useMemberStats(teamName, member?.name ?? null);

  const totalTokens = memberStats ? memberStats.inputTokens + memberStats.outputTokens : null;

  if (!member) return null;

  return (
    <Dialog open={open} onOpenChange={(nextOpen) => !nextOpen && onClose()}>
      <DialogContent className="min-w-0 sm:max-w-4xl">
        <div className="flex items-start gap-4">
          <DialogHeader className="shrink-0">
            <MemberDetailHeader
              member={member}
              runtimeSummary={runtimeSummary}
              isTeamAlive={isTeamAlive}
              isTeamProvisioning={isTeamProvisioning}
              leadActivity={isLeadMember(member) ? leadActivity : undefined}
              spawnStatus={spawnEntry?.status}
              spawnLaunchState={spawnEntry?.launchState}
              spawnLivenessSource={spawnEntry?.livenessSource}
              spawnRuntimeAlive={spawnEntry?.runtimeAlive}
              runtimeEntry={runtimeEntry}
              isLaunchSettling={isLaunchSettling}
              onUpdateRole={
                onUpdateRole ? (newRole) => onUpdateRole(member.name, newRole) : undefined
              }
              updatingRole={updatingRole}
            />
          </DialogHeader>

          <MemberDetailStats
            totalTasks={memberTasks.length}
            inProgressTasks={inProgressTasks}
            totalTokens={totalTokens}
            statsLoading={statsLoading}
            statsComputedAt={memberStats?.computedAt}
            onTabChange={setActiveTab}
          />
        </div>

        <Tabs
          value={activeTab}
          onValueChange={(v) => setActiveTab(v as MemberDetailTab)}
          className="min-w-0 overflow-hidden"
        >
          <TabsList className="w-full">
            <TabsTrigger value="tasks" className="flex-1 gap-1.5">
              Tasks
              {memberTasks.length > 0 && (
                <span className="rounded-full bg-[var(--color-surface)] px-1.5 text-[10px]">
                  {memberTasks.length}
                </span>
              )}
            </TabsTrigger>
            <TabsTrigger value="workspace" className="flex-1 gap-1.5">
              <FolderOpen size={12} />
              Workspace
            </TabsTrigger>
            <TabsTrigger value="activity" className="flex-1 gap-1.5">
              Activity
              {memberActivityCount > 0 && (
                <span className="rounded-full bg-[var(--color-surface)] px-1.5 text-[10px]">
                  {memberActivityCount}
                </span>
              )}
            </TabsTrigger>
            <TabsTrigger value="stats" className="flex-1 gap-1.5">
              <BarChart3 size={12} />
              Stats
            </TabsTrigger>
          </TabsList>
          <TabsContent value="tasks">
            <MemberTasksTab tasks={memberTasks} onTaskClick={onTaskClick} />
          </TabsContent>
          <TabsContent value="workspace">
            <MemberWorkspaceTab
              teamName={teamName}
              memberName={member.name}
              onFileClick={(filePath) => onViewMemberChanges?.(member.name, filePath)}
              onViewAllChanges={() => onViewMemberChanges?.(member.name)}
            />
          </TabsContent>
          <TabsContent value="activity">
            <MemberMessagesTab
              teamName={teamName}
              memberName={member.name}
              members={members}
              tasks={tasks}
              initialFilter={initialActivityFilter}
              onTaskClick={onTaskClick}
            />
          </TabsContent>
          <TabsContent value="stats">
            <MemberStatsTab
              teamName={teamName}
              memberName={member.name}
              prefetchedStats={memberStats}
              prefetchedLoading={statsLoading}
              prefetchedError={statsError}
              onFileClick={(filePath) => onViewMemberChanges?.(member.name, filePath)}
              onShowAllFiles={() => onViewMemberChanges?.(member.name)}
            />
          </TabsContent>
        </Tabs>

        <DialogFooter>
          {restartError ? (
            <div className="text-xs text-red-400">{restartError}</div>
          ) : launchErrorMessage ? (
            <div className="flex min-w-0 items-center gap-2 text-xs text-red-400">
              <span className="min-w-0 truncate" title={launchErrorMessage}>
                {launchErrorMessage}
              </span>
              {launchDiagnosticsPayload && showCopyDiagnostics ? (
                <MemberLaunchDiagnosticsButton
                  payload={launchDiagnosticsPayload}
                  label="复制诊断信息"
                  className="h-auto shrink-0 gap-1.5 px-2 py-1 text-red-300 hover:bg-red-500/10 hover:text-red-200"
                />
              ) : null}
            </div>
          ) : runtimeEntry?.pid ? (
            <div className="text-xs text-[var(--color-text-muted)]">
              PID {runtimeEntry.pid}
              {memorySourceLabel ? ` · ${memorySourceLabel}` : ''}
            </div>
          ) : null}
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
};
