import { useState, useEffect, useCallback } from 'react'
import { useSDK } from '@/lib/sdk'
import type { DocumentReview, ReviewStats } from '../_types'

const API_BASE = '/api/sdk/v1/compliance/document-reviews'

async function apiFetch<T>(url: string, init?: RequestInit): Promise<T> {
  const res = await fetch(url, {
    headers: { 'Content-Type': 'application/json' },
    ...init,
  })
  if (!res.ok) throw new Error(`${res.status} ${res.statusText}`)
  return res.json()
}

export function useDocumentReviews() {
  const { projectId } = useSDK()
  const [reviews, setReviews] = useState<DocumentReview[]>([])
  const [stats, setStats] = useState<ReviewStats>({})
  const [loading, setLoading] = useState(true)
  const [statusFilter, setStatusFilter] = useState<string | null>(null)

  const loadReviews = useCallback(async () => {
    try {
      setLoading(true)
      const params = new URLSearchParams()
      if (projectId) params.set('project_id', projectId)
      if (statusFilter) params.set('status', statusFilter)
      const qs = params.toString() ? `?${params}` : ''

      const [reviewsData, statsData] = await Promise.all([
        apiFetch<DocumentReview[]>(`${API_BASE}${qs}`),
        apiFetch<ReviewStats>(`${API_BASE}/stats${projectId ? `?project_id=${projectId}` : ''}`),
      ])
      setReviews(reviewsData)
      setStats(statsData)
    } catch {
      // Silent — component shows empty state
    } finally {
      setLoading(false)
    }
  }, [projectId, statusFilter])

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

  const approveReview = useCallback(async (reviewId: string) => {
    const updated = await apiFetch<DocumentReview>(`${API_BASE}/${reviewId}/approve`, { method: 'POST' })
    setReviews(prev => prev.map(r => r.id === reviewId ? updated : r))
    return updated
  }, [])

  const rejectReview = useCallback(async (reviewId: string, comment: string) => {
    const updated = await apiFetch<DocumentReview>(`${API_BASE}/${reviewId}/reject`, {
      method: 'POST',
      body: JSON.stringify({ comment }),
    })
    setReviews(prev => prev.map(r => r.id === reviewId ? updated : r))
    return updated
  }, [])

  const sendNotification = useCallback(async (reviewId: string) => {
    return apiFetch<{ sent: boolean }>(`${API_BASE}/${reviewId}/send`, { method: 'POST' })
  }, [])

  return {
    reviews, stats, loading, statusFilter, setStatusFilter,
    loadReviews, approveReview, rejectReview, sendNotification,
  }
}
