File size: 3,598 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { NextRequest, NextResponse } from "next/server";
import {
  getDebateTopicById,
  getRandomDebateTopic,
  type DebateCategory,
  DEBATE_CATEGORIES,
} from "@/lib/debate/topics";
import {
  getOpponentById,
  getRandomOpponent,
  type OpponentDifficulty,
} from "@/lib/debate/opponents";
import { withApiError } from "@/lib/api-error";

const VALID_DIFFICULTIES: OpponentDifficulty[] = ["easy", "medium", "hard"];
const VALID_ROUNDS = [1, 3, 5];

/**
 * POST /api/debate/start
 *
 * Body:
 *   topicId?:     string        — pick a specific topic
 *   customTopic?: string        — free-text topic (overrides topicId)
 *   category?:    DebateCategory — filter for random topic
 *   opponentId?:  string        — pick a specific opponent
 *   difficulty?:  "easy"|"medium"|"hard" — for random opponent
 *   rounds?:      1|3|5         — number of debate rounds (default 3)
 *   userSide?:    "pro"|"con"   — which side the user argues (default "pro")
 *
 * Returns a debate session configuration object that the client
 * holds in state across the multi-round flow.
 */
export const POST = withApiError(async (request: NextRequest) => {
  let body: Record<string, unknown>;
  try {
    body = await request.json();
  } catch {
    return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
  }

  // --- Resolve topic ---
  let topic: { id: string; topic: string; category: string; description: string };

  if (typeof body.customTopic === "string" && body.customTopic.trim().length >= 5) {
    topic = {
      id: "custom",
      topic: body.customTopic.trim(),
      category: "Custom",
      description: "User-provided debate topic",
    };
  } else if (typeof body.topicId === "string") {
    const found = getDebateTopicById(body.topicId);
    if (!found) {
      return NextResponse.json({ error: "Topic not found" }, { status: 404 });
    }
    topic = found;
  } else {
    const category =
      typeof body.category === "string" &&
      DEBATE_CATEGORIES.includes(body.category as DebateCategory)
        ? (body.category as DebateCategory)
        : undefined;
    const random = getRandomDebateTopic(category);
    topic = random;
  }

  // --- Resolve opponent ---
  let opponent: { id: string; name: string; difficulty: string; description: string; icon: string; era?: string; systemPrompt: string };

  if (typeof body.opponentId === "string") {
    const found = getOpponentById(body.opponentId);
    if (!found) {
      return NextResponse.json({ error: "Opponent not found" }, { status: 404 });
    }
    opponent = found;
  } else {
    const difficulty: OpponentDifficulty =
      typeof body.difficulty === "string" &&
      VALID_DIFFICULTIES.includes(body.difficulty as OpponentDifficulty)
        ? (body.difficulty as OpponentDifficulty)
        : "medium";
    opponent = getRandomOpponent(difficulty);
  }

  // --- Rounds ---
  const rounds =
    typeof body.rounds === "number" && VALID_ROUNDS.includes(body.rounds)
      ? body.rounds
      : 3;

  // --- User side ---
  const userSide =
    body.userSide === "con" ? "con" : "pro";

  return NextResponse.json({
    topic: {
      id: topic.id,
      topic: topic.topic,
      category: topic.category,
      description: topic.description,
    },
    opponent: {
      id: opponent.id,
      name: opponent.name,
      difficulty: opponent.difficulty,
      description: opponent.description,
      icon: opponent.icon,
      era: opponent.era ?? null,
    },
    rounds,
    userSide,
    // systemPrompt is NOT returned to the client — it's used server-side only
  });
});