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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
"use client";

import { useState } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { 
  MessageSquare, 
  Heart, 
  Reply, 
  MoreHorizontal,
  Send,
  User,
  ChevronDown,
  ChevronUp,
  Flag,
  Trash2,
  Edit
} from "lucide-react";
import { Button } from "@/components/ui/button";
import { Textarea } from "@/components/ui/textarea";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { Badge } from "@/components/ui/badge";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { formatDistanceToNow } from "date-fns";

interface CommentUser {
  id: string;
  name: string;
  username: string;
  image?: string;
  rank?: string;
}

interface Comment {
  id: string;
  content: string;
  user: CommentUser;
  likes: number;
  liked?: boolean;
  createdAt: Date;
  replies?: Comment[];
}

interface CommentsProps {
  promptId: string;
  comments?: Comment[];
  currentUser?: CommentUser | null;
}

// Sample comments for demo
const SAMPLE_COMMENTS: Comment[] = [
  {
    id: "1",
    content: "This prompt is amazing! I've been using it for all my blog posts and the quality is incredible. Definitely recommend tweaking the tone variable for different audiences.",
    user: {
      id: "u1",
      name: "Sarah Chen",
      username: "sarahchen",
      image: "https://api.dicebear.com/7.x/avataaars/svg?seed=sarah",
      rank: "gold"
    },
    likes: 24,
    liked: false,
    createdAt: new Date(Date.now() - 2 * 60 * 60 * 1000),
    replies: [
      {
        id: "1-1",
        content: "Thanks for the tip! What tone setting works best for technical documentation?",
        user: {
          id: "u2",
          name: "Mike Johnson",
          username: "mikej",
          image: "https://api.dicebear.com/7.x/avataaars/svg?seed=mike",
          rank: "silver"
        },
        likes: 8,
        createdAt: new Date(Date.now() - 1 * 60 * 60 * 1000),
      },
      {
        id: "1-2",
        content: "For technical docs I use 'professional and precise'. Works great!",
        user: {
          id: "u1",
          name: "Sarah Chen",
          username: "sarahchen",
          image: "https://api.dicebear.com/7.x/avataaars/svg?seed=sarah",
          rank: "gold"
        },
        likes: 12,
        createdAt: new Date(Date.now() - 30 * 60 * 1000),
      }
    ]
  },
  {
    id: "2",
    content: "I remixed this with a few tweaks for SEO optimization. Check out my version if you're looking for better search rankings!",
    user: {
      id: "u3",
      name: "Alex Rivera",
      username: "alexr",
      image: "https://api.dicebear.com/7.x/avataaars/svg?seed=alex",
      rank: "verified"
    },
    likes: 45,
    liked: true,
    createdAt: new Date(Date.now() - 24 * 60 * 60 * 1000),
  },
  {
    id: "3",
    content: "Does this work well with Claude or is it optimized specifically for GPT-4?",
    user: {
      id: "u4",
      name: "Jordan Lee",
      username: "jordanl",
      image: "https://api.dicebear.com/7.x/avataaars/svg?seed=jordan",
    },
    likes: 6,
    createdAt: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000),
  },
];

function RankBadge({ rank }: { rank?: string }) {
  if (!rank) return null;
  
  const colors: Record<string, string> = {
    bronze: "bg-orange-900/50 text-orange-300 border-orange-700",
    silver: "bg-gray-700/50 text-gray-300 border-gray-600",
    gold: "bg-yellow-900/50 text-yellow-300 border-yellow-700",
    verified: "bg-blue-900/50 text-blue-300 border-blue-700",
  };
  
  return (
    <Badge variant="outline" className={`text-xs ${colors[rank] || ""}`}>
      {rank === "verified" ? "✓ Verified" : rank.charAt(0).toUpperCase() + rank.slice(1)}
    </Badge>
  );
}

function CommentItem({ 
  comment, 
  isReply = false,
  onReply,
  onLike,
  currentUser
}: { 
  comment: Comment; 
  isReply?: boolean;
  onReply: (commentId: string) => void;
  onLike: (commentId: string) => void;
  currentUser?: CommentUser | null;
}) {
  const [showReplies, setShowReplies] = useState(true);
  const [isLiked, setIsLiked] = useState(comment.liked || false);
  const [likesCount, setLikesCount] = useState(comment.likes);

  const handleLike = () => {
    if (isLiked) {
      setLikesCount(prev => prev - 1);
    } else {
      setLikesCount(prev => prev + 1);
    }
    setIsLiked(!isLiked);
    onLike(comment.id);
  };

  return (
    <motion.div
      initial={{ opacity: 0, y: 10 }}
      animate={{ opacity: 1, y: 0 }}
      className={`${isReply ? "ml-12 border-l-2 border-muted pl-4" : ""}`}
    >
      <div className="flex gap-3 py-4">
        <Avatar className="h-10 w-10">
          <AvatarImage src={comment.user.image} />
          <AvatarFallback>
            <User className="h-5 w-5" />
          </AvatarFallback>
        </Avatar>
        
        <div className="flex-1 min-w-0">
          <div className="flex items-center gap-2 flex-wrap">
            <span className="font-semibold">{comment.user.name}</span>
            <span className="text-sm text-muted-foreground">@{comment.user.username}</span>
            <RankBadge rank={comment.user.rank} />
            <span className="text-sm text-muted-foreground">
              · {formatDistanceToNow(comment.createdAt, { addSuffix: true })}
            </span>
          </div>
          
          <p className="mt-1 text-sm leading-relaxed">{comment.content}</p>
          
          <div className="flex items-center gap-4 mt-2">
            <Button 
              variant="ghost" 
              size="sm" 
              className={`h-8 px-2 ${isLiked ? "text-pink-500" : "text-muted-foreground"}`}
              onClick={handleLike}
            >
              <Heart className={`h-4 w-4 mr-1 ${isLiked ? "fill-current" : ""}`} />
              {likesCount}
            </Button>
            
            {!isReply && (
              <Button 
                variant="ghost" 
                size="sm" 
                className="h-8 px-2 text-muted-foreground"
                onClick={() => onReply(comment.id)}
              >
                <Reply className="h-4 w-4 mr-1" />
                Reply
              </Button>
            )}
            
            <DropdownMenu>
              <DropdownMenuTrigger asChild>
                <Button variant="ghost" size="sm" className="h-8 px-2 text-muted-foreground">
                  <MoreHorizontal className="h-4 w-4" />
                </Button>
              </DropdownMenuTrigger>
              <DropdownMenuContent align="start">
                {currentUser?.id === comment.user.id && (
                  <>
                    <DropdownMenuItem>
                      <Edit className="h-4 w-4 mr-2" />
                      Edit
                    </DropdownMenuItem>
                    <DropdownMenuItem className="text-destructive">
                      <Trash2 className="h-4 w-4 mr-2" />
                      Delete
                    </DropdownMenuItem>
                    <DropdownMenuSeparator />
                  </>
                )}
                <DropdownMenuItem>
                  <Flag className="h-4 w-4 mr-2" />
                  Report
                </DropdownMenuItem>
              </DropdownMenuContent>
            </DropdownMenu>
          </div>
        </div>
      </div>

      {/* Replies */}
      {comment.replies && comment.replies.length > 0 && (
        <div className="ml-4">
          <Button
            variant="ghost"
            size="sm"
            className="text-primary h-8 mb-2"
            onClick={() => setShowReplies(!showReplies)}
          >
            {showReplies ? (
              <>
                <ChevronUp className="h-4 w-4 mr-1" />
                Hide {comment.replies.length} {comment.replies.length === 1 ? "reply" : "replies"}
              </>
            ) : (
              <>
                <ChevronDown className="h-4 w-4 mr-1" />
                Show {comment.replies.length} {comment.replies.length === 1 ? "reply" : "replies"}
              </>
            )}
          </Button>
          
          <AnimatePresence>
            {showReplies && (
              <motion.div
                initial={{ opacity: 0, height: 0 }}
                animate={{ opacity: 1, height: "auto" }}
                exit={{ opacity: 0, height: 0 }}
              >
                {comment.replies.map((reply) => (
                  <CommentItem
                    key={reply.id}
                    comment={reply}
                    isReply
                    onReply={onReply}
                    onLike={onLike}
                    currentUser={currentUser}
                  />
                ))}
              </motion.div>
            )}
          </AnimatePresence>
        </div>
      )}
    </motion.div>
  );
}

export function Comments({ promptId, comments = SAMPLE_COMMENTS, currentUser }: CommentsProps) {
  const [newComment, setNewComment] = useState("");
  const [replyingTo, setReplyingTo] = useState<string | null>(null);
  const [allComments, setAllComments] = useState(comments);

  const handleSubmit = () => {
    if (!newComment.trim()) return;
    
    // In real app, this would be an API call
    const comment: Comment = {
      id: Date.now().toString(),
      content: newComment,
      user: currentUser || {
        id: "guest",
        name: "Guest User",
        username: "guest"
      },
      likes: 0,
      createdAt: new Date(),
    };
    
    if (replyingTo) {
      setAllComments(prev => 
        prev.map(c => {
          if (c.id === replyingTo) {
            return {
              ...c,
              replies: [...(c.replies || []), comment]
            };
          }
          return c;
        })
      );
      setReplyingTo(null);
    } else {
      setAllComments(prev => [comment, ...prev]);
    }
    
    setNewComment("");
  };

  const handleReply = (commentId: string) => {
    setReplyingTo(commentId);
    // Focus the textarea
    document.getElementById("comment-input")?.focus();
  };

  const handleLike = (commentId: string) => {
    // In real app, this would be an API call
    console.log("Liked comment:", commentId);
  };

  return (
    <div className="space-y-6">
      <div className="flex items-center gap-2">
        <MessageSquare className="h-5 w-5" />
        <h3 className="text-lg font-semibold">
          Comments ({allComments.length})
        </h3>
      </div>

      {/* Comment Input */}
      <div className="space-y-3">
        {replyingTo && (
          <div className="flex items-center gap-2 text-sm text-muted-foreground bg-muted/50 px-3 py-2 rounded-md">
            <Reply className="h-4 w-4" />
            <span>Replying to comment</span>
            <Button 
              variant="ghost" 
              size="sm" 
              className="h-6 px-2 ml-auto"
              onClick={() => setReplyingTo(null)}
            >
              Cancel
            </Button>
          </div>
        )}
        
        <div className="flex gap-3">
          <Avatar className="h-10 w-10">
            <AvatarImage src={currentUser?.image} />
            <AvatarFallback>
              <User className="h-5 w-5" />
            </AvatarFallback>
          </Avatar>
          
          <div className="flex-1 space-y-2">
            <Textarea
              id="comment-input"
              placeholder={replyingTo ? "Write a reply..." : "Add a comment..."}
              value={newComment}
              onChange={(e) => setNewComment(e.target.value)}
              className="min-h-20 resize-none"
            />
            
            <div className="flex justify-end">
              <Button 
                onClick={handleSubmit}
                disabled={!newComment.trim()}
                className="bg-linear-to-r from-purple-600 to-pink-600 hover:from-purple-700 hover:to-pink-700"
              >
                <Send className="h-4 w-4 mr-2" />
                {replyingTo ? "Reply" : "Comment"}
              </Button>
            </div>
          </div>
        </div>
      </div>

      {/* Comments List */}
      <div className="divide-y">
        {allComments.map((comment) => (
          <CommentItem
            key={comment.id}
            comment={comment}
            onReply={handleReply}
            onLike={handleLike}
            currentUser={currentUser}
          />
        ))}
      </div>

      {allComments.length === 0 && (
        <div className="text-center py-12 text-muted-foreground">
          <MessageSquare className="h-12 w-12 mx-auto mb-4 opacity-50" />
          <p>No comments yet. Be the first to comment!</p>
        </div>
      )}
    </div>
  );
}