File size: 27,760 Bytes
60e766b
 
d438808
ae601ea
60e766b
374bf33
60e766b
 
 
 
 
d438808
 
60e766b
 
 
 
 
 
 
 
 
 
d438808
60e766b
d438808
60e766b
d438808
 
60e766b
 
 
 
 
 
d438808
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60e766b
 
 
 
 
 
e8dc740
 
 
 
60e766b
 
 
 
 
d438808
60e766b
d438808
 
 
ae601ea
 
d438808
 
60e766b
 
d438808
60e766b
 
 
 
 
 
d438808
60e766b
d438808
60e766b
 
 
d438808
 
 
 
 
 
 
60e766b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae601ea
 
60e766b
 
 
 
 
 
d438808
 
 
 
 
 
 
 
 
ae601ea
 
 
 
 
 
 
 
d438808
60e766b
 
 
 
 
 
d438808
 
 
 
 
 
60e766b
 
 
 
d438808
 
 
 
60e766b
 
 
 
 
 
 
 
 
 
 
 
374bf33
60e766b
 
 
 
 
 
d438808
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60e766b
374bf33
60e766b
 
 
 
 
 
 
 
 
 
d438808
60e766b
 
 
 
 
 
 
 
 
374bf33
60e766b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374bf33
60e766b
 
 
 
 
 
 
374bf33
60e766b
 
 
 
 
 
 
374bf33
60e766b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374bf33
60e766b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374bf33
60e766b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d438808
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae601ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d438808
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60e766b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae601ea
 
 
 
60e766b
 
 
 
 
 
d438808
ae601ea
60e766b
 
 
 
 
 
d438808
 
 
60e766b
 
ae601ea
 
 
 
 
60e766b
d438808
ae601ea
 
d438808
 
 
 
 
60e766b
 
e8dc740
 
 
 
 
 
 
 
 
 
 
 
60e766b
 
 
 
d438808
60e766b
 
 
 
 
 
 
d438808
 
60e766b
 
 
 
 
d438808
60e766b
 
 
 
 
d438808
 
60e766b
 
 
 
 
d438808
60e766b
 
 
 
 
d438808
 
60e766b
 
 
 
 
d438808
60e766b
 
 
d438808
 
 
 
3590954
d438808
3590954
d438808
 
3590954
 
 
 
d438808
 
3590954
 
 
 
 
 
 
 
 
 
 
 
 
 
d438808
3590954
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d438808
3590954
 
 
 
 
 
 
d438808
 
60e766b
3590954
60e766b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d438808
60e766b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d438808
 
60e766b
 
 
 
 
d438808
60e766b
 
 
 
d438808
 
60e766b
 
 
 
 
d438808
60e766b
 
 
 
 
 
d438808
 
 
 
 
60e766b
 
d438808
60e766b
 
 
 
 
 
d438808
60e766b
 
 
 
d438808
60e766b
d438808
60e766b
 
 
 
 
 
 
 
 
 
 
 
 
 
d438808
60e766b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d438808
 
 
 
60e766b
 
 
 
 
d438808
 
 
 
60e766b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
"use client";

import Link from "next/link";
import { useRouter, useSearchParams } from "next/navigation";
import { useMemo, useState } from "react";
import { fetchWithAnon } from "@/lib/client-anon";

type LinkItem = {
  id: string;
  url: string;
  label: string | null;
  createdByAnonId: string | null;
  createdByUserId: string | null;
};

type TaskItem = {
  id: string;
  title: string;
  description: string | null;
  acceptance_criteria: string[];
  effort: string | null;
  status: "OPEN" | "IN_PROGRESS" | "DONE";
  claimedByAnonId: string | null;
  claimedByUserId: string | null;
  claimantNickname: string | null;
  claimantDisplayName: string | null;
  links: LinkItem[];
  working_count: number;
  working: boolean;
};

type CommentItem = {
  id: string;
  body: string;
  createdAt: string;
  author_label?: string;
};

type SolutionItem = {
  id: string;
  ideaId: string;
  taskId: string | null;
  taskTitle: string | null;
  url: string;
  label: string | null;
  description: string | null;
  type: "APP_URL" | "GITHUB_REPO" | "OTHER";
  createdAt: string;
  approvedAt: string | null;
  createdByDisplayName: string;
  voteScore: number;
  myVote: number;
  commentsCount: number;
};

type IdeaPayload = {
  id: string;
  title: string;
  problemStatement: string;
  rawInputText: string;
  targetUsers: string | null;
  platform: string | null;
  constraints: string | null;
  tags: string[];
  features: string[];
  open_questions: string[];
  upvotesCount: number;
  commentsCount: number;
  submitter_label: string;
  viewerAnonId: string;
  viewerUserId: string | null;
  isAuthenticated: boolean;
  canApproveSolutions: boolean;
  canDelete: boolean;
  merged_submission_count: number;
  idea_working_count: number;
  idea_working: boolean;
  tasks: TaskItem[];
  comments: CommentItem[];
  solutions: SolutionItem[];
};

type TaskUpdate = {
  id: string;
  status: "OPEN" | "IN_PROGRESS" | "DONE";
  claimedByAnonId: string | null;
  claimedByUserId: string | null;
  claimantNickname?: string | null;
  claimantDisplayName?: string | null;
  links?: LinkItem[];
};

type SolutionCommentItem = {
  id: string;
  body: string;
  createdAt: string;
  author_label?: string;
};

function formatStatus(status: TaskItem["status"]): string {
  if (status === "IN_PROGRESS") return "In Progress";
  if (status === "DONE") return "Done";
  return "Open";
}

export function IdeaDetailClient({
  initialIdea,
  isVoted,
  initialNickname,
}: {
  initialIdea: IdeaPayload;
  isVoted: boolean;
  initialNickname: string;
}) {
  const router = useRouter();
  const searchParams = useSearchParams();
  const [idea, setIdea] = useState(initialIdea);
  const [voted, setVoted] = useState(isVoted);
  const [nickname, setNickname] = useState(initialNickname);
  const [nickInput, setNickInput] = useState(initialNickname);
  const [commentInput, setCommentInput] = useState("");

  const [solutions, setSolutions] = useState<SolutionItem[]>(initialIdea.solutions || []);
  const [solutionUrl, setSolutionUrl] = useState("");
  const [solutionLabel, setSolutionLabel] = useState("");
  const [solutionDescription, setSolutionDescription] = useState("");
  const [solutionType, setSolutionType] = useState<"APP_URL" | "GITHUB_REPO" | "OTHER">("APP_URL");
  const [solutionTaskId, setSolutionTaskId] = useState<string>("");

  const [solutionComments, setSolutionComments] = useState<Record<string, SolutionCommentItem[]>>({});
  const [solutionCommentInput, setSolutionCommentInput] = useState<Record<string, string>>({});
  const [actionError, setActionError] = useState("");

  const mergeNotice = useMemo(() => {
    if (searchParams.get("merged") !== "1") return "";
    return searchParams.get("reason") === "SUBSET"
      ? "Your submission was merged: it is a subset of this existing idea."
      : "Your submission was merged: this idea already exists, so discussion stays in one thread.";
  }, [searchParams]);

  function applyTaskUpdate(taskId: string, taskUpdate: TaskUpdate) {
    setIdea((prev) => ({
      ...prev,
      tasks: prev.tasks.map((task) => {
        if (task.id !== taskId) return task;

        const nextClaimedByUser = taskUpdate.claimedByUserId;
        const nextClaimedByAnon = taskUpdate.claimedByAnonId;
        const nextClaimantDisplayName =
          taskUpdate.claimantDisplayName ??
          taskUpdate.claimantNickname ??
          (nextClaimedByAnon === prev.viewerAnonId ? nickname || "anon" : task.claimantDisplayName);

        return {
          ...task,
          status: taskUpdate.status,
          claimedByUserId: nextClaimedByUser,
          claimedByAnonId: nextClaimedByAnon,
          claimantNickname: nextClaimantDisplayName,
          claimantDisplayName: nextClaimantDisplayName,
          links: taskUpdate.links ?? task.links,
        };
      }),
    }));
  }

  async function toggleVote() {
    const next = !voted;
    setVoted(next);
    setIdea((prev) => ({ ...prev, upvotesCount: prev.upvotesCount + (next ? 1 : -1) }));

    const method = next ? "POST" : "DELETE";
    const response = await fetchWithAnon(`/api/ideas/${idea.id}/upvote`, { method });
    if (!response.ok) {
      setVoted(!next);
      setIdea((prev) => ({ ...prev, upvotesCount: prev.upvotesCount + (next ? -1 : 1) }));
    }
  }

  async function toggleIdeaWorking() {
    if (!idea.isAuthenticated) return;

    const next = !idea.idea_working;
    const method = next ? "POST" : "DELETE";
    const response = await fetchWithAnon(`/api/ideas/${idea.id}/work-vote`, { method });
    if (!response.ok) return;

    const data = await response.json();
    setIdea((prev) => ({
      ...prev,
      idea_working: data.working,
      idea_working_count: data.working_count,
    }));
  }

  async function toggleTaskWorking(taskId: string, working: boolean) {
    if (!idea.isAuthenticated) return;

    const response = await fetchWithAnon(`/api/tasks/${taskId}/work-vote`, { method: working ? "DELETE" : "POST" });
    if (!response.ok) return;

    const data = await response.json();
    setIdea((prev) => ({
      ...prev,
      tasks: prev.tasks.map((task) =>
        task.id === taskId
          ? {
              ...task,
              working: data.working,
              working_count: data.working_count,
            }
          : task
      ),
    }));
  }

  async function saveNickname() {
    const response = await fetchWithAnon("/api/me/nickname", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ nickname: nickInput }),
    });

    if (response.ok) {
      setNickname(nickInput);
      setIdea((prev) => ({
        ...prev,
        tasks: prev.tasks.map((task) =>
          task.claimedByAnonId === prev.viewerAnonId ? { ...task, claimantNickname: nickInput || "anon", claimantDisplayName: nickInput || "anon" } : task
        ),
      }));
    }
  }

  async function addComment() {
    const body = commentInput.trim();
    if (!body) return;

    const response = await fetchWithAnon(`/api/ideas/${idea.id}/comments`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ body }),
    });

    if (!response.ok) return;
    const data = await response.json();

    setIdea((prev) => ({
      ...prev,
      commentsCount: prev.commentsCount + 1,
      comments: [data.comment, ...prev.comments],
    }));
    setCommentInput("");
  }

  async function claim(taskId: string) {
    const response = await fetchWithAnon(`/api/tasks/${taskId}/claim`, { method: "POST" });
    if (!response.ok) return;

    const data = await response.json();
    applyTaskUpdate(taskId, data.task);
  }

  async function unclaim(taskId: string) {
    const response = await fetchWithAnon(`/api/tasks/${taskId}/unclaim`, { method: "POST" });
    if (!response.ok) return;

    const data = await response.json();
    applyTaskUpdate(taskId, data.task);
  }

  async function setStatus(taskId: string, status: TaskItem["status"]) {
    const response = await fetchWithAnon(`/api/tasks/${taskId}/status`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ status }),
    });
    if (!response.ok) return;

    const data = await response.json();
    applyTaskUpdate(taskId, data.task);
  }

  async function addLink(taskId: string) {
    const url = window.prompt("Paste URL (repo/PR/demo)");
    if (!url) return;
    const label = window.prompt("Label (optional)") || undefined;

    const response = await fetchWithAnon(`/api/tasks/${taskId}/links`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ url, label }),
    });

    if (!response.ok) return;
    const data = await response.json();

    setIdea((prev) => ({
      ...prev,
      tasks: prev.tasks.map((task) => (task.id === taskId ? { ...task, links: [...task.links, data.link] } : task)),
    }));
  }

  async function removeLink(taskId: string, linkId: string) {
    const response = await fetchWithAnon(`/api/task-links/${linkId}`, { method: "DELETE" });
    if (!response.ok) return;

    setIdea((prev) => ({
      ...prev,
      tasks: prev.tasks.map((task) =>
        task.id === taskId
          ? {
              ...task,
              links: task.links.filter((link) => link.id !== linkId),
            }
          : task
      ),
    }));
  }

  async function submitSolution() {
    if (!idea.isAuthenticated) return;
    if (!solutionUrl.trim()) return;

    const response = await fetchWithAnon(`/api/ideas/${idea.id}/solutions`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        taskId: solutionTaskId || undefined,
        url: solutionUrl.trim(),
        label: solutionLabel.trim() || undefined,
        description: solutionDescription.trim() || undefined,
        type: solutionType,
      }),
    });

    if (!response.ok) return;
    const data = await response.json();
    setSolutions(data.items || []);
    setSolutionUrl("");
    setSolutionLabel("");
    setSolutionDescription("");
    setSolutionTaskId("");
    setSolutionType("APP_URL");
  }

  async function setSolutionVote(solutionId: string, value: number) {
    const current = solutions.find((solution) => solution.id === solutionId)?.myVote || 0;
    const response =
      current === value
        ? await fetchWithAnon(`/api/solutions/${solutionId}/vote`, { method: "DELETE" })
        : await fetchWithAnon(`/api/solutions/${solutionId}/vote`, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ value }),
          });

    if (!response.ok) return;
    const data = await response.json();

    setSolutions((prev) =>
      prev.map((solution) =>
        solution.id === solutionId
          ? {
              ...solution,
              voteScore: data.score,
              myVote: data.myVote,
            }
          : solution
      )
    );
  }

  async function approveSolution(solutionId: string) {
    const response = await fetchWithAnon(`/api/solutions/${solutionId}/approve`, { method: "POST" });
    if (!response.ok) return;

    const data = await response.json();
    const approvedAt = data.solution?.approvedAt || new Date().toISOString();
    setSolutions((prev) => prev.map((solution) => (solution.id === solutionId ? { ...solution, approvedAt } : solution)));
  }

  async function deleteIdea() {
    if (!idea.canDelete) return;

    const confirmed = window.confirm("Delete this post? This action cannot be undone.");
    if (!confirmed) return;

    setActionError("");
    const response = await fetchWithAnon(`/api/ideas/${idea.id}`, { method: "DELETE" });
    if (!response.ok) {
      const data = await response.json().catch(() => null);
      setActionError((data && typeof data.error === "string" ? data.error : "Failed to delete post.") || "Failed to delete post.");
      return;
    }

    router.push("/");
    router.refresh();
  }

  async function loadSolutionComments(solutionId: string) {
    const response = await fetchWithAnon(`/api/solutions/${solutionId}/comments`);
    if (!response.ok) return;

    const data = await response.json();
    setSolutionComments((prev) => ({ ...prev, [solutionId]: data.items || [] }));
  }

  async function addSolutionComment(solutionId: string) {
    const body = (solutionCommentInput[solutionId] || "").trim();
    if (!body) return;

    const response = await fetchWithAnon(`/api/solutions/${solutionId}/comments`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ body }),
    });
    if (!response.ok) return;

    const data = await response.json();
    setSolutionComments((prev) => ({ ...prev, [solutionId]: data.items || [] }));
    setSolutionCommentInput((prev) => ({ ...prev, [solutionId]: "" }));
    setSolutions((prev) =>
      prev.map((solution) => (solution.id === solutionId ? { ...solution, commentsCount: (data.items || []).length } : solution))
    );
  }

  const grouped = useMemo(() => {
    return {
      open: idea.tasks.filter((task) => task.status === "OPEN"),
      inProgress: idea.tasks.filter((task) => task.status === "IN_PROGRESS"),
      done: idea.tasks.filter((task) => task.status === "DONE"),
    };
  }, [idea.tasks]);

  return (
    <div className="space-y-5 md:space-y-6">
      <section className="hero-panel reveal-up">
        <p className="kicker">Generated Idea</p>
        <h1 className="section-title mt-2 text-3xl md:text-4xl">{idea.title}</h1>
        <p className="subtle mt-4 max-w-4xl text-sm md:text-base">{idea.problemStatement}</p>

        {mergeNotice ? (
          <p className="mt-4 rounded-lg border border-amber-300 bg-amber-50 px-3 py-2 text-sm text-amber-900">{mergeNotice}</p>
        ) : null}

        <div className="mt-4 flex flex-wrap gap-1.5">
          {idea.tags.map((tag) => (
            <span key={tag} className="badge">
              {tag}
            </span>
          ))}
          <span className="pill">Submitter: {idea.submitter_label}</span>
          <span className="pill">Merged submissions: {idea.merged_submission_count}</span>
        </div>

        <div className="mt-5 flex flex-wrap items-center gap-2 md:gap-3">
          <button className={`btn ${voted ? "btn-primary" : ""}`} onClick={toggleVote}>
            {voted ? "Upvoted" : "Upvote"} ({idea.upvotesCount})
          </button>
          <button className={`btn ${idea.idea_working ? "btn-primary" : ""}`} onClick={toggleIdeaWorking} disabled={!idea.isAuthenticated}>
            Working on this ({idea.idea_working_count})
          </button>
          <span className="pill">{idea.commentsCount} comments</span>
          <span className="pill">{idea.tasks.length} tasks</span>
          {idea.canDelete ? (
            <button className="btn border-red-200 text-red-700 hover:bg-red-50" onClick={deleteIdea}>
              Delete Post
            </button>
          ) : null}
        </div>

        {actionError ? <p className="rounded-lg border border-red-200 bg-red-50 px-3 py-2 text-sm text-red-700">{actionError}</p> : null}

        {!idea.isAuthenticated ? (
          <p className="subtle mt-2 text-xs">
            <Link href="/login" className="underline">Sign in</Link> to submit solutions and add developer work votes.
          </p>
        ) : null}
      </section>

      <section className="card reveal-up" style={{ animationDelay: "70ms" }}>
        <div className="flex flex-wrap items-center gap-2">
          <h2 className="section-title text-xl">Original Submission</h2>
          {idea.platform ? <span className="pill">Platform: {idea.platform}</span> : null}
          {idea.targetUsers ? <span className="pill">Target: {idea.targetUsers}</span> : null}
        </div>
        {idea.constraints ? <p className="subtle mt-2 text-xs">Constraints: {idea.constraints}</p> : null}
        <p className="mt-3 whitespace-pre-wrap break-words rounded-xl border border-zinc-200 bg-white/70 p-3 text-sm leading-6">
          {idea.rawInputText}
        </p>
      </section>

      <section className="card reveal-up space-y-4" style={{ animationDelay: "180ms" }}>
        <div className="flex items-end justify-between gap-2">
          <div>
            <h2 className="section-title">Tasks</h2>
            <p className="subtle mt-1 text-sm">Claim tasks, attach implementation links, and vote to signal developer interest.</p>
          </div>
        </div>

        <TaskColumn
          title="Open"
          items={grouped.open}
          viewerAnonId={idea.viewerAnonId}
          viewerUserId={idea.viewerUserId}
          isAuthenticated={idea.isAuthenticated}
          onClaim={claim}
          onUnclaim={unclaim}
          onSetStatus={setStatus}
          onAddLink={addLink}
          onRemoveLink={removeLink}
          onToggleWorking={toggleTaskWorking}
        />
        <TaskColumn
          title="In Progress"
          items={grouped.inProgress}
          viewerAnonId={idea.viewerAnonId}
          viewerUserId={idea.viewerUserId}
          isAuthenticated={idea.isAuthenticated}
          onClaim={claim}
          onUnclaim={unclaim}
          onSetStatus={setStatus}
          onAddLink={addLink}
          onRemoveLink={removeLink}
          onToggleWorking={toggleTaskWorking}
        />
        <TaskColumn
          title="Done"
          items={grouped.done}
          viewerAnonId={idea.viewerAnonId}
          viewerUserId={idea.viewerUserId}
          isAuthenticated={idea.isAuthenticated}
          onClaim={claim}
          onUnclaim={unclaim}
          onSetStatus={setStatus}
          onAddLink={addLink}
          onRemoveLink={removeLink}
          onToggleWorking={toggleTaskWorking}
        />
      </section>

      <section className="card reveal-up space-y-4" style={{ animationDelay: "200ms" }}>
        <div className="flex items-end justify-between gap-2">
          <div>
            <h2 className="section-title">Solutions</h2>
            <p className="subtle mt-1 text-sm">Only developers who marked "Working on this" can submit solutions.</p>
          </div>
          <span className="pill">{solutions.length} submitted</span>
        </div>

        <div className="flex flex-wrap gap-2">
          <Link href={`/ideas/${idea.id}/solutions`} className="btn">
            View Submitted Solutions
          </Link>
        </div>

        {idea.isAuthenticated && idea.idea_working ? (
          <div className="rounded-xl border border-zinc-200 bg-white/70 p-3">
            <div className="grid gap-2 md:grid-cols-2">
              <input className="input" value={solutionUrl} onChange={(e) => setSolutionUrl(e.target.value)} placeholder="Solution URL (app or repo)" />
              <input className="input" value={solutionLabel} onChange={(e) => setSolutionLabel(e.target.value)} placeholder="Label (optional)" />
              <select className="input" value={solutionType} onChange={(e) => setSolutionType(e.target.value as "APP_URL" | "GITHUB_REPO" | "OTHER")}>
                <option value="APP_URL">App URL</option>
                <option value="GITHUB_REPO">GitHub repo</option>
                <option value="OTHER">Other</option>
              </select>
              <select className="input" value={solutionTaskId} onChange={(e) => setSolutionTaskId(e.target.value)}>
                <option value="">For whole idea</option>
                {idea.tasks.map((task) => (
                  <option key={task.id} value={task.id}>{task.title}</option>
                ))}
              </select>
              <textarea
                className="input md:col-span-2 min-h-20"
                value={solutionDescription}
                onChange={(e) => setSolutionDescription(e.target.value)}
                placeholder="How this solution addresses the demand"
              />
            </div>
            <div className="mt-2 flex items-center gap-2">
              <button className="btn btn-primary" onClick={submitSolution}>
                Submit Solution
              </button>
            </div>
          </div>
        ) : (
          <div className="rounded-xl border border-zinc-200 bg-white/70 p-3 text-sm">
            {idea.isAuthenticated ? (
              <div className="flex flex-wrap items-center gap-2">
                <span>Mark yourself as working on this idea first, then the solution form will appear.</span>
                <button className="btn" onClick={toggleIdeaWorking}>Working on this</button>
              </div>
            ) : (
              <p>
                <Link href="/login" className="underline">Sign in</Link> and click "Working on this" to submit a solution.
              </p>
            )}
          </div>
        )}
      </section>

      <section className="card reveal-up space-y-4" style={{ animationDelay: "240ms" }}>

        <div className="flex items-end justify-between gap-2">
          <h2 className="section-title">Comments</h2>
          <span className="pill">{idea.commentsCount} total</span>
        </div>

        <div className="flex flex-col gap-2 md:flex-row">
          <input
            className="input"
            value={commentInput}
            onChange={(event) => setCommentInput(event.target.value)}
            placeholder="Add a comment"
          />
          <button className="btn btn-primary md:min-w-28" onClick={addComment}>
            Post
          </button>
        </div>

        <div className="space-y-2">
          {idea.comments.length ? (
            idea.comments.map((comment) => (
              <div key={comment.id} className="rounded-xl border border-white/70 bg-white/60 p-3 text-sm">
                <p>{comment.body}</p>
                <p className="subtle mt-1 text-xs">{comment.author_label || "Anon"} • {new Date(comment.createdAt).toLocaleString()}</p>
              </div>
            ))
          ) : (
            <p className="subtle text-sm">No comments yet.</p>
          )}
        </div>
      </section>
    </div>
  );
}

function statusBadgeClass(status: TaskItem["status"]): string {
  if (status === "DONE") return "bg-emerald-100 text-emerald-700 border-emerald-200";
  if (status === "IN_PROGRESS") return "bg-amber-100 text-amber-700 border-amber-200";
  return "bg-sky-100 text-sky-700 border-sky-200";
}

function TaskColumn({
  title,
  items,
  viewerAnonId,
  viewerUserId,
  isAuthenticated,
  onClaim,
  onUnclaim,
  onSetStatus,
  onAddLink,
  onRemoveLink,
  onToggleWorking,
}: {
  title: string;
  items: TaskItem[];
  viewerAnonId: string;
  viewerUserId: string | null;
  isAuthenticated: boolean;
  onClaim: (taskId: string) => Promise<void>;
  onUnclaim: (taskId: string) => Promise<void>;
  onSetStatus: (taskId: string, status: TaskItem["status"]) => Promise<void>;
  onAddLink: (taskId: string) => Promise<void>;
  onRemoveLink: (taskId: string, linkId: string) => Promise<void>;
  onToggleWorking: (taskId: string, working: boolean) => Promise<void>;
}) {
  return (
    <div className="space-y-2">
      <h3 className="text-sm font-semibold uppercase tracking-[0.16em] text-zinc-600">{title}</h3>
      {items.length === 0 ? <p className="subtle text-sm">No tasks.</p> : null}
      {items.map((task) => {
        const isClaimedByMe = viewerUserId
          ? task.claimedByUserId === viewerUserId
          : !task.claimedByUserId && task.claimedByAnonId === viewerAnonId;

        const isClaimedBySomeoneElse = Boolean(task.claimedByUserId || task.claimedByAnonId) && !isClaimedByMe;

        return (
          <details key={task.id} className="rounded-xl border border-white/70 bg-white/65 p-3">
            <summary className="cursor-pointer text-sm font-semibold">
              <span>{task.title}</span>
              <span className={`ml-2 inline-flex rounded-full border px-2 py-0.5 text-xs ${statusBadgeClass(task.status)}`}>
                {formatStatus(task.status)}
              </span>
              {task.effort ? <span className="ml-2 text-xs text-zinc-500">Effort {task.effort}</span> : null}
              <span className="ml-2 text-xs text-zinc-500">Working votes {task.working_count}</span>
            </summary>

            <div className="mt-3 space-y-2 text-sm">
              {task.description ? <p>{task.description}</p> : null}
              {task.claimedByAnonId || task.claimedByUserId ? (
                <p className="text-xs text-zinc-600">
                  Claimed by <span className="font-medium">{task.claimantDisplayName || "anon"}</span>
                </p>
              ) : (
                <p className="text-xs text-zinc-600">Unclaimed</p>
              )}

              {task.acceptance_criteria.length > 0 ? (
                <ul className="list-disc space-y-1 pl-5">
                  {task.acceptance_criteria.map((criteria) => (
                    <li key={criteria}>{criteria}</li>
                  ))}
                </ul>
              ) : null}

              <div className="flex flex-wrap gap-2">
                {!task.claimedByAnonId && !task.claimedByUserId ? (
                  <button className="btn" onClick={() => onClaim(task.id)}>
                    Claim
                  </button>
                ) : null}

                {isClaimedByMe ? (
                  <>
                    <button className="btn" onClick={() => onUnclaim(task.id)}>
                      Unclaim
                    </button>
                    <button className="btn" onClick={() => onSetStatus(task.id, "IN_PROGRESS")}>
                      In Progress
                    </button>
                    <button className="btn" onClick={() => onSetStatus(task.id, "DONE")}>
                      Done
                    </button>
                    <button className="btn" onClick={() => onSetStatus(task.id, "OPEN")}>
                      Open
                    </button>
                    <button className="btn" onClick={() => onAddLink(task.id)}>
                      Add Link
                    </button>
                  </>
                ) : null}

                <button className={`btn ${task.working ? "btn-primary" : ""}`} onClick={() => onToggleWorking(task.id, task.working)} disabled={!isAuthenticated}>
                  I am working
                </button>

                {isClaimedBySomeoneElse ? <span className="subtle text-xs">Only claimant can update this task.</span> : null}
              </div>

              <div className="space-y-1.5">
                {task.links.map((link) => {
                  const canRemoveAsUser = Boolean(viewerUserId) && (link.createdByUserId === viewerUserId || task.claimedByUserId === viewerUserId);
                  const canRemoveAsAnon = !viewerUserId && (link.createdByAnonId === viewerAnonId || task.claimedByAnonId === viewerAnonId);
                  const canRemove = canRemoveAsUser || canRemoveAsAnon;

                  return (
                    <div key={link.id} className="flex items-center justify-between gap-2 rounded-lg border border-zinc-200 bg-white/90 px-2 py-1">
                      <a href={link.url} target="_blank" rel="noreferrer" className="truncate text-cyan-700 underline">
                        {link.label || link.url}
                      </a>
                      {canRemove ? (
                        <button className="text-xs text-zinc-500" onClick={() => onRemoveLink(task.id, link.id)}>
                          remove
                        </button>
                      ) : null}
                    </div>
                  );
                })}
              </div>
            </div>
          </details>
        );
      })}
    </div>
  );
}