"use client"

import { useState, useEffect } from "react"
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"

interface User {
  national_id: string
  name: string
}

interface TeamUserCategoryModalProps {
  isOpen: boolean
  onClose: () => void
  category: string
  categoryName: string
  adminId: string
  teamName: string
  departmentName: string
  fieldName: string
}

export function TeamUserCategoryModal({
  isOpen,
  onClose,
  category,
  categoryName,
  adminId,
  teamName,
  departmentName,
  fieldName,
}: TeamUserCategoryModalProps) {
  const [users, setUsers] = useState<User[]>([])
  const [loading, setLoading] = useState(false)

  useEffect(() => {
    if (isOpen && category && adminId) {
      fetchUsers()
    }
  }, [isOpen, category, adminId])

  const fetchUsers = async () => {
    setLoading(true)
    try {
      const response = await fetch("/api/admin/team-users-by-category", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ adminId, category }),
      })
      const data = await response.json()
      setUsers(data)
    } catch (err) {
      console.error("Error fetching team users:", err)
    } finally {
      setLoading(false)
    }
  }

  return (
    <Dialog open={isOpen} onOpenChange={onClose}>
      <DialogContent className="max-w-2xl max-h-[80vh] overflow-y-auto" dir="rtl">
        <DialogHeader className="text-center" dir="rtl">
          <DialogTitle className="text-xl text-center" dir="rtl">
            {categoryName} - צוות {teamName}
          </DialogTitle>
          <DialogDescription className="text-center" dir="rtl">רשימת המשתמשים בקטגוריה זו מהצוות שלך</DialogDescription>
        </DialogHeader>

        {loading ? (
          <div className="text-center py-8">טוען משתמשים...</div>
        ) : (
          <div className="mt-4">
            {users.length > 0 ? (
              <Table>
                <TableHeader className="text-center" dir="rtl">
                  <TableRow className="text-center" dir="rtl">
                    <TableHead className="text-center">שם</TableHead>
                  </TableRow>
                </TableHeader>
                <TableBody className="text-center" dir="rtl">
                  {users.map((user) => (
                    <TableRow key={user.national_id}>
                      <TableCell className="font-medium">{user.name}</TableCell>
                    </TableRow>
                  ))}
                </TableBody>
              </Table>
            ) : (
              <div className="text-center py-8 text-gray-500">אין משתמשים בקטגוריה זו בצוות שלך</div>
            )}
            <div className="mt-4 text-sm text-gray-600 text-center">
              סה"כ: {users.length} משתמשים מצוות {teamName}
            </div>
          </div>
        )}
      </DialogContent>
    </Dialog>
  )
}
