"use client"

import { useState, useEffect, useCallback } from "react"

interface Message {
  id: string
  senderId: string
  receiverId: string
  content: string
  timestamp: Date
  type: "text" | "image" | "file" | "audio"
  status: "sent" | "delivered" | "read"
}

interface ChatState {
  messages: Message[]
  isTyping: boolean
  isConnected: boolean
}

export function useChat(userId: string) {
  const [chatState, setChatState] = useState<ChatState>({
    messages: [],
    isTyping: false,
    isConnected: false,
  })

  // Simulate WebSocket connection
  useEffect(() => {
    console.log(`[v0] Connecting to chat for user ${userId}`)
    setChatState((prev) => ({ ...prev, isConnected: true }))

    // Simulate receiving messages
    const messageInterval = setInterval(() => {
      if (Math.random() > 0.98) {
        // 2% chance every second
        const newMessage: Message = {
          id: Date.now().toString(),
          senderId: userId,
          receiverId: "current-user",
          content: "This is a simulated real-time message!",
          timestamp: new Date(),
          type: "text",
          status: "delivered",
        }

        setChatState((prev) => ({
          ...prev,
          messages: [...prev.messages, newMessage],
        }))
      }
    }, 1000)

    // Simulate typing indicator
    const typingInterval = setInterval(() => {
      if (Math.random() > 0.95) {
        // 5% chance every 2 seconds
        setChatState((prev) => ({ ...prev, isTyping: true }))
        setTimeout(() => {
          setChatState((prev) => ({ ...prev, isTyping: false }))
        }, 2000)
      }
    }, 2000)

    return () => {
      clearInterval(messageInterval)
      clearInterval(typingInterval)
      setChatState((prev) => ({ ...prev, isConnected: false }))
    }
  }, [userId])

  const sendMessage = useCallback(
    (content: string, type: "text" | "image" | "file" | "audio" = "text") => {
      const message: Message = {
        id: Date.now().toString(),
        senderId: "current-user",
        receiverId: userId,
        content,
        timestamp: new Date(),
        type,
        status: "sent",
      }

      setChatState((prev) => ({
        ...prev,
        messages: [...prev.messages, message],
      }))

      // Simulate message delivery and read status
      setTimeout(() => {
        setChatState((prev) => ({
          ...prev,
          messages: prev.messages.map((msg) => (msg.id === message.id ? { ...msg, status: "delivered" } : msg)),
        }))
      }, 1000)

      setTimeout(() => {
        setChatState((prev) => ({
          ...prev,
          messages: prev.messages.map((msg) => (msg.id === message.id ? { ...msg, status: "read" } : msg)),
        }))
      }, 3000)

      return message.id
    },
    [userId],
  )

  const markAsRead = useCallback((messageId: string) => {
    setChatState((prev) => ({
      ...prev,
      messages: prev.messages.map((msg) => (msg.id === messageId ? { ...msg, status: "read" } : msg)),
    }))
  }, [])

  return {
    messages: chatState.messages,
    isTyping: chatState.isTyping,
    isConnected: chatState.isConnected,
    sendMessage,
    markAsRead,
  }
}
