'use client'

import { useState, useEffect, useCallback } from 'react'
import type { Email, Account } from './types'
import { API_BASE } from './types'

export function useMail() {
  const [emails, setEmails] = useState<Email[]>([])
  const [accounts, setAccounts] = useState<Account[]>([])
  const [selectedEmail, setSelectedEmail] = useState<Email | null>(null)
  const [selectedAccount, setSelectedAccount] = useState<string>('all')
  const [selectedFolder, setSelectedFolder] = useState('inbox')
  const [loading, setLoading] = useState(true)
  const [searchQuery, setSearchQuery] = useState('')

  const fetchEmails = useCallback(async () => {
    try {
      setLoading(true)
      const params = new URLSearchParams()
      if (selectedAccount !== 'all') {
        params.append('account_id', selectedAccount)
      }
      if (selectedFolder === 'unread') {
        params.append('unread_only', 'true')
      }
      if (searchQuery) {
        params.append('search', searchQuery)
      }

      const res = await fetch(`${API_BASE}/api/v1/mail/inbox?${params}`)
      if (res.ok) {
        const data = await res.json()
        setEmails(data.emails || [])
      }
    } catch (err) {
      console.error('Failed to fetch emails:', err)
    } finally {
      setLoading(false)
    }
  }, [selectedAccount, selectedFolder, searchQuery])

  const fetchAccounts = useCallback(async () => {
    try {
      const res = await fetch(`${API_BASE}/api/v1/mail/accounts`)
      if (res.ok) {
        const data = await res.json()
        setAccounts(data.accounts || [])
      }
    } catch (err) {
      console.error('Failed to fetch accounts:', err)
    }
  }, [])

  useEffect(() => {
    fetchAccounts()
  }, [fetchAccounts])

  useEffect(() => {
    fetchEmails()
  }, [fetchEmails])

  const analyzeEmail = async (emailId: string) => {
    try {
      const res = await fetch(`${API_BASE}/api/v1/mail/analyze/${emailId}`, {
        method: 'POST',
      })
      if (res.ok) {
        const data = await res.json()
        setSelectedEmail((prev) => prev ? { ...prev, ...data, aiAnalyzed: true } : null)
        setEmails((prev) =>
          prev.map((e) => (e.id === emailId ? { ...e, ...data, aiAnalyzed: true } : e))
        )
      }
    } catch (err) {
      console.error('Failed to analyze email:', err)
    }
  }

  const markAsRead = async (emailId: string) => {
    try {
      await fetch(`${API_BASE}/api/v1/mail/inbox/${emailId}/read`, {
        method: 'POST',
      })
      setEmails((prev) =>
        prev.map((e) => (e.id === emailId ? { ...e, isRead: true } : e))
      )
    } catch (err) {
      console.error('Failed to mark as read:', err)
    }
  }

  const handleEmailClick = (email: Email) => {
    setSelectedEmail(email)
    if (!email.isRead) {
      markAsRead(email.id)
    }
  }

  const totalUnread = accounts.reduce((sum, acc) => sum + acc.unreadCount, 0)

  return {
    emails, accounts,
    selectedEmail, setSelectedEmail,
    selectedAccount, setSelectedAccount,
    selectedFolder, setSelectedFolder,
    loading, searchQuery, setSearchQuery,
    analyzeEmail, handleEmailClick, totalUnread,
  }
}
