import React from "react";
import { Button, Table, TableBody, TableCell, TableColumn, TableHeader, TableRow } from "@heroui/react";
import { useTranslation } from "react-i18next";

type UserItem = {
  id: number;
  name: string;
  email: string;
  is_admin: boolean;
  is_active: boolean;
};

type Props = {
  items: UserItem[];
  loading: boolean;
  onEdit: (item: UserItem) => void;
  onDeactivate: (item: UserItem) => void;
};

export default function AdminUsersTable({ items, loading, onEdit, onDeactivate }: Props) {
  const { t } = useTranslation("common");

  return (
    <Table aria-label="Users table">
      <TableHeader>
        <TableColumn>{t("admin_users_name") ?? "Jméno"}</TableColumn>
        <TableColumn>{t("admin_users_email") ?? "Email"}</TableColumn>
        <TableColumn>{t("admin_users_is_admin") ?? "Admin"}</TableColumn>
        <TableColumn>{t("admin_users_is_active") ?? "Aktivní"}</TableColumn>
        <TableColumn>{t("admin_users_actions") ?? "Akce"}</TableColumn>
      </TableHeader>
      <TableBody
        items={items}
        emptyContent={
          loading
            ? t("admin_users_loading") ?? "Načítám..."
            : t("admin_users_empty") ?? "Žádní uživatelé."
        }
      >
        {(item) => (
          <TableRow key={item.id}>
            <TableCell>{item.name}</TableCell>
            <TableCell>{item.email}</TableCell>
            <TableCell>{item.is_admin ? "ANO" : "NE"}</TableCell>
            <TableCell>{item.is_active ? "ANO" : "NE"}</TableCell>
            <TableCell>
              <div className="flex gap-2">
                <Button size="sm" variant="bordered" onPress={() => onEdit(item)}>
                  {t("admin_users_edit") ?? "Upravit"}
                </Button>
                <Button
                  size="sm"
                  color="danger"
                  variant="light"
                  onPress={() => onDeactivate(item)}
                  isDisabled={!item.is_active}
                >
                  {t("admin_users_deactivate") ?? "Deaktivovat"}
                </Button>
              </div>
            </TableCell>
          </TableRow>
        )}
      </TableBody>
    </Table>
  );
}
