import { DashboardLink as Link } from "@/components/layout/dashboard-link";
import { StatusBadge } from "@/components/shared/status-badge";
import { Button } from "@/components/ui/button";
import { Eye } from "lucide-react";
import { sessionStatusLabel, sessionTone } from "./presentation";
import type { Session } from "./types";

export function RecentSessions({
  data,
  error,
}: {
  data: Session[] | null;
  error: string | null;
}) {
  const sessions = data ?? [];
  return (
    <section className="detail-panel min-w-0 overflow-hidden">
      <div className="flex items-center justify-between gap-4 px-6 py-4">
        <h2 className="text-lg font-semibold">Recent Sessions</h2>
        <Link
          href="/sessions"
          className="rounded-lg border border-input px-4 py-3"
        >
          See all
        </Link>
      </div>
      <div className="overflow-x-auto">
        <table className="w-full text-sm">
          <thead className="border-y bg-muted/50">
            <tr>
              {["User", "Charging Station", "Status", "Action"].map((label) => (
                <th
                  key={label}
                  className="px-4 py-3 text-center text-xs font-medium text-muted-foreground"
                >
                  {label}
                </th>
              ))}
            </tr>
          </thead>
          <tbody>
            {error ? (
              <tr>
                <td
                  colSpan={4}
                  role="alert"
                  className="px-6 py-10 text-center text-destructive"
                >
                  {error}
                </td>
              </tr>
            ) : sessions.length ? (
              sessions.map((row) => (
                <tr key={row.id} className="border-b">
                  <td className="px-4 py-3.5 text-center">
                    <div className="flex items-center justify-center gap-3">
                      <span className="flex size-10 shrink-0 items-center justify-center rounded-full bg-muted">
                        {row.userName === "—"
                          ? "?"
                          : row.userName.trim().charAt(0).toUpperCase()}
                      </span>
                      <span className="truncate font-medium">{row.userName}</span>
                    </div>
                  </td>
                  <td className="px-4 py-3.5 text-center">
                    <Link
                      className="underline"
                      href={`/chargepoints/${row.chargepointId}`}
                    >
                      {row.chargepointName}
                    </Link>
                  </td>
                  <td className="px-4 py-3.5 text-center">
                    <StatusBadge tone={sessionTone(row.status)}>
                      {sessionStatusLabel(row.status)}
                    </StatusBadge>
                  </td>
                  <td className="px-4 py-3.5 text-center">
                    <Button asChild variant="outline" size="xs">
                      <Link
                        href={`/sessions/${row.id}`}
                        aria-label={`View details for session ${row.id}`}
                      >
                        <Eye aria-hidden="true" />
                        View Detail
                      </Link>
                    </Button>
                  </td>
                </tr>
              ))
            ) : (
              <tr>
                <td
                  colSpan={4}
                  className="px-6 py-10 text-center text-muted-foreground"
                >
                  No recent sessions found.
                </td>
              </tr>
            )}
          </tbody>
        </table>
      </div>
    </section>
  );
}
