import type {
  BigFishAnchorItemResponse,
  BigFishSessionSnapshotResponse,
  ExecuteBigFishActionRequest,
  SendBigFishMessageRequest,
} from '../../../packages/shared/src/contracts/bigFish';
import { createCreationAgentClientMessageId } from '../../services/creation-agent';
import {
  type CreationAgentAnchorView,
  type CreationAgentSessionView,
  type CreationAgentTheme,
  CreationAgentWorkspace,
} from '../creation-agent';

type BigFishAgentWorkspaceProps = {
  session: BigFishSessionSnapshotResponse | null;
  streamingReplyText?: string;
  isBusy?: boolean;
  error?: string | null;
  onBack: () => void;
  onSubmitMessage: (payload: SendBigFishMessageRequest) => void;
  onExecuteAction: (payload: ExecuteBigFishActionRequest) => void;
};

const BIG_FISH_AGENT_THEME: CreationAgentTheme = {
  accentTextClass: 'text-cyan-100/85',
  accentBgClass: 'bg-cyan-200',
  accentButtonClass: 'bg-cyan-200 shadow-cyan-950/20',
  userBubbleClass: 'bg-cyan-600 text-white',
  heroClass:
    'border border-cyan-100/16 bg-[radial-gradient(circle_at_top_left,rgba(45,212,191,0.22),transparent_32%),linear-gradient(135deg,rgba(8,47,73,0.96),rgba(13,24,38,0.96))]',
  anchorGridClass: 'grid gap-2 sm:grid-cols-4',
};

function mapBigFishAnchor(
  anchor: BigFishAnchorItemResponse,
): CreationAgentAnchorView {
  return {
    key: anchor.key,
    label: anchor.label,
    value: anchor.value,
    status: anchor.status,
  };
}

function mapBigFishSession(
  session: BigFishSessionSnapshotResponse,
): CreationAgentSessionView {
  return {
    sessionId: session.sessionId,
    title: '大鱼吃小鱼共创',
    assistantSummary:
      session.lastAssistantReply ||
      '先用一句灵感开始，Agent 会收束成可编译的玩法锚点。',
    currentTurn: session.currentTurn,
    progressPercent: session.progressPercent,
    anchors: [
      session.anchorPack.gameplayPromise,
      session.anchorPack.ecologyVisualTheme,
      session.anchorPack.growthLadder,
      session.anchorPack.riskTempo,
    ].map(mapBigFishAnchor),
    messages: session.messages,
    recommendedReplies: [],
  };
}

export function BigFishAgentWorkspace({
  session,
  streamingReplyText = '',
  isBusy = false,
  error = null,
  onBack,
  onSubmitMessage,
  onExecuteAction,
}: BigFishAgentWorkspaceProps) {
  return (
    <CreationAgentWorkspace
      session={session ? mapBigFishSession(session) : null}
      theme={BIG_FISH_AGENT_THEME}
      loadingText="正在准备大鱼吃小鱼共创工作区..."
      composerPlaceholder="说说这局的生态、成长或爽点..."
      primaryActionLabel="生成结果页"
      streamingReplyText={streamingReplyText}
      isStreamingReply={Boolean(streamingReplyText)}
      isBusy={isBusy}
      error={error}
      onBack={onBack}
      onSubmitText={(text) => {
        onSubmitMessage({
          clientMessageId: createCreationAgentClientMessageId('big-fish'),
          text,
        });
      }}
      onPrimaryAction={() => {
        onExecuteAction({ action: 'big_fish_compile_draft' });
      }}
    />
  );
}

export default BigFishAgentWorkspace;
