"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { format } from "date-fns";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { sel } from "../../../lib/test/index.js";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow
} from "../../ui/table.js";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuTrigger
} from "../../ui/dropdown-menu.js";
import { Button } from "../../ui/button.js";
import { Avatar, AvatarFallback } from "../../ui/avatar.js";
import {
  MoreHorizontal,
  Users,
  Calendar,
  Eye,
  Users2
} from "lucide-react";
function TeamsTable({ teams, isLoading }) {
  const router = useRouter();
  const handleRowClick = (teamId) => {
    router.push(`/superadmin/teams/${teamId}`);
  };
  const getTeamInitials = (name) => {
    const words = name.split(" ");
    if (words.length >= 2) {
      return `${words[0][0]}${words[1][0]}`.toUpperCase();
    }
    return name.slice(0, 2).toUpperCase();
  };
  if (isLoading) {
    return /* @__PURE__ */ jsx("div", { className: "space-y-3", children: [...Array(5)].map((_, i) => /* @__PURE__ */ jsxs("div", { className: "flex items-center space-x-4 p-4", children: [
      /* @__PURE__ */ jsx("div", { className: "w-10 h-10 bg-muted rounded-full animate-pulse" }),
      /* @__PURE__ */ jsxs("div", { className: "space-y-2 flex-1", children: [
        /* @__PURE__ */ jsx("div", { className: "h-4 bg-muted rounded w-1/4 animate-pulse" }),
        /* @__PURE__ */ jsx("div", { className: "h-3 bg-muted rounded w-1/3 animate-pulse" })
      ] }),
      /* @__PURE__ */ jsx("div", { className: "w-20 h-6 bg-muted rounded animate-pulse" }),
      /* @__PURE__ */ jsx("div", { className: "w-8 h-8 bg-muted rounded animate-pulse" })
    ] }, i)) });
  }
  if (teams.length === 0) {
    return /* @__PURE__ */ jsxs("div", { className: "text-center py-8", children: [
      /* @__PURE__ */ jsx(Users2, { className: "mx-auto h-12 w-12 text-muted-foreground" }),
      /* @__PURE__ */ jsx("h3", { className: "mt-4 text-lg font-medium", children: "No teams found" }),
      /* @__PURE__ */ jsx("p", { className: "mt-2 text-sm text-muted-foreground", children: "There are no teams matching your filters." })
    ] });
  }
  return /* @__PURE__ */ jsx("div", { className: "rounded-md border", "data-cy": sel("superadmin.teams.table.element"), children: /* @__PURE__ */ jsxs(Table, { children: [
    /* @__PURE__ */ jsx(TableHeader, { children: /* @__PURE__ */ jsxs(TableRow, { children: [
      /* @__PURE__ */ jsx(TableHead, { children: "Team" }),
      /* @__PURE__ */ jsx(TableHead, { children: "Owner" }),
      /* @__PURE__ */ jsx(TableHead, { children: "Members" }),
      /* @__PURE__ */ jsx(TableHead, { children: "Created" }),
      /* @__PURE__ */ jsx(TableHead, { className: "text-right", children: "Actions" })
    ] }) }),
    /* @__PURE__ */ jsx(TableBody, { children: teams.map((team) => {
      var _a, _b;
      return /* @__PURE__ */ jsxs(
        TableRow,
        {
          "data-cy": sel("superadmin.teams.table.row", { id: team.id }),
          className: "cursor-pointer hover:bg-muted/50",
          style: { cursor: "pointer" },
          onClick: () => handleRowClick(team.id),
          children: [
            /* @__PURE__ */ jsx(TableCell, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-3", children: [
              /* @__PURE__ */ jsx(Avatar, { className: "h-9 w-9", children: /* @__PURE__ */ jsx(AvatarFallback, { className: "bg-primary/10 text-primary text-xs", children: getTeamInitials(team.name) }) }),
              /* @__PURE__ */ jsxs("div", { children: [
                /* @__PURE__ */ jsx("div", { className: "font-medium", children: team.name }),
                /* @__PURE__ */ jsxs("div", { className: "text-sm text-muted-foreground", children: [
                  "ID: ",
                  team.id.slice(0, 12),
                  "..."
                ] })
              ] })
            ] }) }),
            /* @__PURE__ */ jsx(TableCell, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
              /* @__PURE__ */ jsx(Avatar, { className: "h-6 w-6", children: /* @__PURE__ */ jsx(AvatarFallback, { className: "text-xs", children: ((_b = (_a = team.owner.name) == null ? void 0 : _a[0]) == null ? void 0 : _b.toUpperCase()) || "?" }) }),
              /* @__PURE__ */ jsxs("div", { children: [
                /* @__PURE__ */ jsx("div", { className: "text-sm font-medium", children: team.owner.name }),
                /* @__PURE__ */ jsx("div", { className: "text-xs text-muted-foreground", children: team.owner.email })
              ] })
            ] }) }),
            /* @__PURE__ */ jsx(TableCell, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
              /* @__PURE__ */ jsx(Users, { className: "h-4 w-4 text-muted-foreground" }),
              /* @__PURE__ */ jsx("span", { className: "font-medium", children: team.memberCount }),
              /* @__PURE__ */ jsx("span", { className: "text-muted-foreground text-sm", children: team.memberCount === 1 ? "member" : "members" })
            ] }) }),
            /* @__PURE__ */ jsx(TableCell, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 text-sm text-muted-foreground", children: [
              /* @__PURE__ */ jsx(Calendar, { className: "h-4 w-4" }),
              team.createdAt ? format(new Date(team.createdAt), "MMM dd, yyyy") : "Unknown"
            ] }) }),
            /* @__PURE__ */ jsx(TableCell, { className: "text-right", onClick: (e) => e.stopPropagation(), children: /* @__PURE__ */ jsxs(DropdownMenu, { children: [
              /* @__PURE__ */ jsx(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsxs(
                Button,
                {
                  variant: "ghost",
                  size: "sm",
                  className: "h-8 w-8 p-0",
                  "data-cy": sel("superadmin.teams.table.menuButton", { id: team.id }),
                  children: [
                    /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Open menu" }),
                    /* @__PURE__ */ jsx(MoreHorizontal, { className: "h-4 w-4" })
                  ]
                }
              ) }),
              /* @__PURE__ */ jsxs(DropdownMenuContent, { align: "end", children: [
                /* @__PURE__ */ jsx(DropdownMenuLabel, { children: "Actions" }),
                /* @__PURE__ */ jsx(DropdownMenuItem, { asChild: true, className: "cursor-pointer", children: /* @__PURE__ */ jsxs(Link, { href: `/superadmin/teams/${team.id}`, children: [
                  /* @__PURE__ */ jsx(Eye, { className: "mr-2 h-4 w-4" }),
                  "View Details"
                ] }) })
              ] })
            ] }) })
          ]
        },
        team.id
      );
    }) })
  ] }) });
}
export {
  TeamsTable
};
