"use client"

import type React from "react"

import { createContext, useContext, useEffect, useState } from "react"

interface User {
  id: string
  email: string
  firstName: string
  lastName: string
  avatar?: string
  isOnline: boolean
  lastSeen?: Date
}

interface AuthContextType {
  user: User | null
  isLoading: boolean
  signIn: (email: string, password: string) => Promise<void>
  signUp: (userData: {
    firstName: string
    lastName: string
    email: string
    password: string
  }) => Promise<void>
  signOut: () => Promise<void>
  resetPassword: (email: string) => Promise<void>
}

const AuthContext = createContext<AuthContextType | undefined>(undefined)

export function AuthProvider({ children }: { children: React.ReactNode }) {
  const [user, setUser] = useState<User | null>(null)
  const [isLoading, setIsLoading] = useState(true)

  useEffect(() => {
    // TODO: Check for existing session
    // For now, simulate loading
    const timer = setTimeout(() => {
      setIsLoading(false)
    }, 1000)

    return () => clearTimeout(timer)
  }, [])

  const signIn = async (email: string, password: string) => {
    // TODO: Implement actual sign in logic
    console.log("Sign in:", { email, password })

    // Mock user data
    const mockUser: User = {
      id: "1",
      email,
      firstName: "John",
      lastName: "Doe",
      isOnline: true,
    }

    setUser(mockUser)
  }

  const signUp = async (userData: {
    firstName: string
    lastName: string
    email: string
    password: string
  }) => {
    // TODO: Implement actual sign up logic
    console.log("Sign up:", userData)
  }

  const signOut = async () => {
    // TODO: Implement actual sign out logic
    setUser(null)
  }

  const resetPassword = async (email: string) => {
    // TODO: Implement actual password reset logic
    console.log("Reset password for:", email)
  }

  return (
    <AuthContext.Provider
      value={{
        user,
        isLoading,
        signIn,
        signUp,
        signOut,
        resetPassword,
      }}
    >
      {children}
    </AuthContext.Provider>
  )
}

export function useAuth() {
  const context = useContext(AuthContext)
  if (context === undefined) {
    throw new Error("useAuth must be used within an AuthProvider")
  }
  return context
}
