import { useQuery } from "@tanstack/react-query";
import type { AxiosError } from "axios";
import {
  Activity,
  AlertTriangle,
  BarChart3,
  CheckCircle2,
  Layers3,
  ShieldCheck,
} from "lucide-react";
import { type ReactNode, useMemo, useState } from "react";
import {
  type ClientSummary,
  fetchClients,
  fetchDevRPUsageDaily,
  fetchDevStats,
  type RPUsageDailyMetric,
  type RPUsagePeriod,
} from "../../lib/devApi";
import { t } from "../../lib/i18n";

type ClientDistribution = {
  activeClients: number;
  headlessClients: number;
  pkceClients: number;
  privateClients: number;
};

type DailyPoint = {
  date: string;
  loginRequests: number;
  otherRequests: number;
};

type SeriesSummary = {
  key: string;
  clientLabel: string;
  loginRequests: number;
  otherRequests: number;
  uniqueSubjects: number;
};

type MultiLineSeries = {
  key: string;
  clientLabel: string;
  color: UsageChartPalette;
  points: DailyPoint[];
};

type ClientFilterOption = {
  id: string;
  label: string;
};

type UsageChartPalette = {
  bar: string;
  line: string;
  point: string;
};

const usageChartPalettes: UsageChartPalette[] = [
  { bar: "#7dd3fc", line: "#10b981", point: "#059669" },
  { bar: "#f9a8d4", line: "#f97316", point: "#ea580c" },
  { bar: "#c4b5fd", line: "#6366f1", point: "#4f46e5" },
  { bar: "#86efac", line: "#14b8a6", point: "#0f766e" },
  { bar: "#fdba74", line: "#ef4444", point: "#dc2626" },
  { bar: "#93c5fd", line: "#8b5cf6", point: "#7c3aed" },
];

function buildClientDistribution(clients: ClientSummary[]): ClientDistribution {
  return clients.reduce<ClientDistribution>(
    (summary, client) => {
      if (client.status === "active") {
        summary.activeClients += 1;
      }

      if (client.metadata?.headless_login_enabled === true) {
        summary.headlessClients += 1;
      }

      if (client.type === "pkce") {
        summary.pkceClients += 1;
      } else {
        summary.privateClients += 1;
      }

      return summary;
    },
    {
      activeClients: 0,
      headlessClients: 0,
      pkceClients: 0,
      privateClients: 0,
    },
  );
}

function summarizeDaily(rows: RPUsageDailyMetric[]): DailyPoint[] {
  const byDate = new Map<string, DailyPoint>();
  for (const row of rows) {
    const current =
      byDate.get(row.date) ??
      ({
        date: row.date,
        loginRequests: 0,
        otherRequests: 0,
      } satisfies DailyPoint);
    current.loginRequests += row.loginRequests;
    current.otherRequests += row.otherRequests;
    byDate.set(row.date, current);
  }
  return Array.from(byDate.values()).sort((left, right) =>
    left.date.localeCompare(right.date),
  );
}

function summarizeSeries(rows: RPUsageDailyMetric[]): SeriesSummary[] {
  const bySeries = new Map<string, SeriesSummary>();
  for (const row of rows) {
    const key = row.clientId;
    const current =
      bySeries.get(key) ??
      ({
        key,
        clientLabel: row.clientName || row.clientId,
        loginRequests: 0,
        otherRequests: 0,
        uniqueSubjects: 0,
      } satisfies SeriesSummary);
    current.loginRequests += row.loginRequests;
    current.otherRequests += row.otherRequests;
    current.uniqueSubjects = Math.max(
      current.uniqueSubjects,
      row.uniqueSubjects,
    );
    bySeries.set(key, current);
  }
  return Array.from(bySeries.values())
    .sort(
      (left, right) =>
        right.loginRequests +
        right.otherRequests -
        (left.loginRequests + left.otherRequests),
    )
    .slice(0, 5);
}

function buildMultiLineSeries(rows: RPUsageDailyMetric[]): MultiLineSeries[] {
  const dates = summarizeDaily(rows).map((point) => point.date);
  const byClient = new Map<
    string,
    {
      clientLabel: string;
      byDate: Map<string, DailyPoint>;
    }
  >();

  for (const row of rows) {
    const current = byClient.get(row.clientId) ?? {
      clientLabel: row.clientName || row.clientId,
      byDate: new Map<string, DailyPoint>(),
    };
    const point =
      current.byDate.get(row.date) ??
      ({
        date: row.date,
        loginRequests: 0,
        otherRequests: 0,
      } satisfies DailyPoint);
    point.loginRequests += row.loginRequests;
    point.otherRequests += row.otherRequests;
    current.byDate.set(row.date, point);
    byClient.set(row.clientId, current);
  }

  return Array.from(byClient.entries())
    .sort((left, right) =>
      left[1].clientLabel.localeCompare(right[1].clientLabel),
    )
    .map(([clientId, entry], index) => ({
      key: clientId,
      clientLabel: entry.clientLabel,
      color: usageChartPalettes[index % usageChartPalettes.length],
      points: dates.map(
        (date) =>
          entry.byDate.get(date) ??
          ({
            date,
            loginRequests: 0,
            otherRequests: 0,
          } satisfies DailyPoint),
      ),
    }));
}

function parseDateParts(date: string) {
  const parts = date.split("-");
  if (parts.length === 3) {
    return {
      year: Number(parts[0]),
      month: Number(parts[1]),
      day: Number(parts[2]),
      monthText: parts[1],
      dayText: parts[2],
    };
  }
  return null;
}

function getISOWeekNumber(year: number, month: number, day: number) {
  const date = new Date(Date.UTC(year, month - 1, day));
  const dayOfWeek = date.getUTCDay() || 7;
  date.setUTCDate(date.getUTCDate() + 4 - dayOfWeek);
  const yearStart = new Date(Date.UTC(date.getUTCFullYear(), 0, 1));
  return Math.ceil(((date.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);
}

function getISOWeekThursday(year: number, month: number, day: number) {
  const date = new Date(Date.UTC(year, month - 1, day));
  const dayOfWeek = date.getUTCDay() || 7;
  date.setUTCDate(date.getUTCDate() + 4 - dayOfWeek);
  return date;
}

function formatPeriodLabel(date: string, period: RPUsagePeriod) {
  const parts = parseDateParts(date);
  if (!parts) {
    return date;
  }
  if (period === "month") {
    return `${parts.monthText}월`;
  }
  if (period === "week") {
    const weekNumber = String(
      getISOWeekNumber(parts.year, parts.month, parts.day),
    ).padStart(2, "0");
    const weekThursday = getISOWeekThursday(parts.year, parts.month, parts.day);
    const weekMonth = weekThursday.getUTCMonth() + 1;
    const weekDay = weekThursday.getUTCDate();
    const weekMonthText = String(weekMonth).padStart(2, "0");
    const weekOfMonth = Math.min(5, Math.max(1, Math.ceil(weekDay / 7)));
    return `${weekNumber}(${weekMonthText}월${weekOfMonth}주)`;
  }
  return `${parts.monthText}.${parts.dayText}`;
}

function formatMetric(value: number | undefined) {
  return value === undefined ? "-" : value.toLocaleString();
}

function OverviewMetric({
  icon,
  label,
  value,
}: {
  icon: ReactNode;
  label: string;
  value: string;
}) {
  return (
    <span className="inline-flex items-center gap-2 whitespace-nowrap text-sm">
      <span className="text-muted-foreground">{icon}</span>
      <span className="text-muted-foreground">{label}</span>
      <span className="font-semibold tabular-nums">{value}</span>
    </span>
  );
}

function RPUsageMixedChart({
  period,
  rows,
  palette,
  multiLineSeries,
}: {
  period: RPUsagePeriod;
  rows: RPUsageDailyMetric[];
  palette?: UsageChartPalette;
  multiLineSeries?: MultiLineSeries[];
}) {
  const colors = palette ?? usageChartPalettes[0];
  const daily = summarizeDaily(rows);
  const series = summarizeSeries(rows);
  const chartWidth = 720;
  const chartHeight = 230;
  const padX = 48;
  const padTop = 32;
  const padBottom = 34;
  const innerWidth = chartWidth - padX * 2;
  const innerHeight = chartHeight - padTop - padBottom;
  const maxValue = Math.max(
    1,
    ...daily.map((point) => point.loginRequests + point.otherRequests),
    ...daily.map((point) => point.loginRequests),
  );
  const slot = daily.length > 0 ? innerWidth / daily.length : innerWidth;
  const barWidth = Math.min(28, Math.max(10, slot * 0.42));
  const y = (value: number) =>
    padTop + innerHeight - (value / maxValue) * innerHeight;
  const x = (index: number) => padX + slot * index + slot / 2;
  const linePoints = daily
    .map((point, index) => `${x(index)},${y(point.loginRequests)}`)
    .join(" ");
  const multiLinePoints = multiLineSeries?.map((seriesItem) => ({
    ...seriesItem,
    pointsAttr: seriesItem.points
      .map((point, index) => `${x(index)},${y(point.loginRequests)}`)
      .join(" "),
  }));

  if (daily.length === 0) {
    return (
      <div className="flex min-h-[210px] items-center justify-center text-sm text-muted-foreground">
        {t("msg.dev.dashboard.chart.empty", "표시할 RP 이용 집계가 없습니다.")}
      </div>
    );
  }

  return (
    <div className="space-y-3">
      <div className="overflow-x-auto">
        <svg
          role="img"
          aria-label={t("ui.dev.dashboard.chart.aria", "RP 요청 현황")}
          viewBox={`0 0 ${chartWidth} ${chartHeight}`}
          className="h-[235px] min-w-[720px] w-full"
        >
          <title>{t("ui.dev.dashboard.chart.aria", "RP 요청 현황")}</title>
          <g transform="translate(510 10)">
            <rect
              x="0"
              y="3"
              width="10"
              height="10"
              rx="2"
              fill={colors.bar}
              fillOpacity="0.7"
            />
            <text x="16" y="12" className="fill-muted-foreground text-[11px]">
              {t("ui.dev.dashboard.chart.other_requests", "기타 요청")}
            </text>
            {!multiLinePoints || multiLinePoints.length === 0 ? (
              <>
                <line
                  x1="78"
                  x2="98"
                  y1="8"
                  y2="8"
                  stroke={colors.line}
                  strokeWidth="3"
                  strokeLinecap="round"
                />
                <text
                  x="104"
                  y="12"
                  className="fill-muted-foreground text-[11px]"
                >
                  {t("ui.dev.dashboard.chart.login_requests", "로그인 요청")}
                </text>
              </>
            ) : null}
          </g>
          {[0, 0.25, 0.5, 0.75, 1].map((ratio) => {
            const gridY = padTop + innerHeight * ratio;
            const label = Math.round(maxValue * (1 - ratio));
            return (
              <g key={ratio}>
                <line
                  x1={padX}
                  x2={chartWidth - padX}
                  y1={gridY}
                  y2={gridY}
                  stroke="currentColor"
                  className="text-border"
                  strokeWidth="1"
                />
                <text
                  x={padX - 12}
                  y={gridY + 4}
                  textAnchor="end"
                  className="fill-muted-foreground text-[11px]"
                >
                  {label}
                </text>
              </g>
            );
          })}
          {daily.map((point, index) => {
            const center = x(index);
            const otherHeight = (point.otherRequests / maxValue) * innerHeight;
            return (
              <g key={point.date}>
                <rect
                  x={center - barWidth / 2}
                  y={padTop + innerHeight - otherHeight}
                  width={barWidth}
                  height={otherHeight}
                  rx="3"
                  fill={colors.bar}
                  fillOpacity="0.7"
                />
                <text
                  x={center}
                  y={chartHeight - 12}
                  textAnchor="middle"
                  className="fill-muted-foreground text-[11px]"
                >
                  {formatPeriodLabel(point.date, period)}
                </text>
              </g>
            );
          })}
          {!multiLinePoints || multiLinePoints.length === 0 ? (
            <>
              <polyline
                points={linePoints}
                fill="none"
                stroke={colors.line}
                strokeWidth="3"
                strokeLinecap="round"
                strokeLinejoin="round"
              />
              {daily.map((point, index) => (
                <circle
                  key={`${point.date}-login`}
                  cx={x(index)}
                  cy={y(point.loginRequests)}
                  r="4"
                  fill={colors.point}
                  stroke="hsl(var(--background))"
                  strokeWidth="2"
                />
              ))}
            </>
          ) : (
            multiLinePoints.map((seriesItem) => (
              <g key={seriesItem.key}>
                <polyline
                  points={seriesItem.pointsAttr}
                  fill="none"
                  stroke={seriesItem.color.line}
                  strokeWidth="3"
                  strokeLinecap="round"
                  strokeLinejoin="round"
                />
                {seriesItem.points.map((point, index) => (
                  <circle
                    key={`${seriesItem.key}-${point.date}`}
                    cx={x(index)}
                    cy={y(point.loginRequests)}
                    r="3.5"
                    fill={seriesItem.color.point}
                    stroke="hsl(var(--background))"
                    strokeWidth="2"
                  />
                ))}
              </g>
            ))
          )}
        </svg>
      </div>

      {multiLinePoints && multiLinePoints.length > 0 ? (
        <div className="grid gap-x-6 gap-y-2 border-t border-border/60 pt-2 text-xs md:grid-cols-2 xl:grid-cols-3">
          {multiLinePoints.map((item) => (
            <div key={item.key} className="flex min-w-0 items-center gap-2">
              <span
                className="h-2.5 w-2.5 rounded-full"
                style={{ backgroundColor: item.color.line }}
              />
              <span className="truncate font-medium">{item.clientLabel}</span>
            </div>
          ))}
        </div>
      ) : series.length > 0 ? (
        <div className="grid gap-x-6 gap-y-2 border-t border-border/60 pt-2 text-xs md:grid-cols-2 xl:grid-cols-3">
          {series.map((item) => (
            <div key={item.key} className="flex min-w-0 items-center gap-2">
              <span className="truncate font-medium">{item.clientLabel}</span>
              <span className="ml-auto whitespace-nowrap tabular-nums">
                {t(
                  "ui.dev.dashboard.chart.series",
                  "로그인 {{login}} / 기타 {{other}} / 사용자 {{subjects}}",
                  {
                    login: item.loginRequests.toLocaleString(),
                    other: item.otherRequests.toLocaleString(),
                    subjects: item.uniqueSubjects.toLocaleString(),
                  },
                )}
              </span>
            </div>
          ))}
        </div>
      ) : null}
    </div>
  );
}

function DashboardPage() {
  const [period, setPeriod] = useState<RPUsagePeriod>("day");
  const [selectedClientIds, setSelectedClientIds] = useState<string[]>([]);
  const usageDays = period === "day" ? 14 : period === "week" ? 84 : 90;
  const statsQuery = useQuery({
    queryKey: ["dev-dashboard-stats"],
    queryFn: fetchDevStats,
    retry: false,
  });
  const clientsQuery = useQuery({
    queryKey: ["dev-dashboard-clients"],
    queryFn: fetchClients,
    retry: false,
  });
  const usageQuery = useQuery({
    queryKey: ["dev-dashboard-rp-usage", usageDays, period],
    queryFn: () =>
      fetchDevRPUsageDaily({
        days: usageDays,
        period,
      }),
    retry: false,
  });

  const clients = clientsQuery.data?.items ?? [];
  const distribution = useMemo(
    () => buildClientDistribution(clients),
    [clients],
  );
  const visibleClients = useMemo(
    () =>
      [...clients]
        .sort((left, right) => {
          const statusCompare = (left.status || "").localeCompare(
            right.status || "",
          );
          if (statusCompare !== 0) {
            return statusCompare;
          }
          return (left.name || left.id).localeCompare(right.name || right.id);
        })
        .slice(0, 6),
    [clients],
  );
  const clientFilterOptions = useMemo<ClientFilterOption[]>(
    () =>
      [...clients]
        .map((client) => ({
          id: client.id,
          label: client.name || client.id,
        }))
        .sort((left, right) => left.label.localeCompare(right.label)),
    [clients],
  );
  const stats = statsQuery.data;
  const usageRows = usageQuery.data?.items ?? [];
  const filteredUsageRows = useMemo(() => {
    if (selectedClientIds.length === 0) {
      return usageRows;
    }
    const selectedSet = new Set(selectedClientIds);
    return usageRows.filter((row) => selectedSet.has(row.clientId));
  }, [selectedClientIds, usageRows]);
  const selectedMultiLineSeries = useMemo(
    () => buildMultiLineSeries(filteredUsageRows),
    [filteredUsageRows],
  );
  const usageError = usageQuery.error as AxiosError<{ error?: string }> | null;
  const usageStatus = usageError?.response?.status;
  const usageErrorMessage =
    usageError?.response?.data?.error ?? usageError?.message ?? "";
  const usageErrorText =
    usageStatus === 403
      ? t(
          "msg.dev.dashboard.chart.forbidden",
          "현재 계정에는 RP 이용 통계를 볼 권한이 없습니다.",
        )
      : usageStatus === 503
        ? t(
            "msg.dev.dashboard.chart.service_unavailable",
            "RP 이용 통계 집계 서비스가 아직 준비되지 않았습니다.",
          )
        : usageStatus === 500
          ? t(
              "msg.dev.dashboard.chart.server_error",
              "RP 이용 통계 조회 중 서버 오류가 발생했습니다.",
            )
          : t(
              "msg.dev.dashboard.chart.unavailable_with_reason",
              "RP 이용 통계 API 응답을 확인할 수 없습니다. {{reason}}",
              {
                reason:
                  usageErrorMessage ||
                  t("err.common.unknown", "알 수 없는 오류"),
              },
            );
  const isAllClientsSelected = selectedClientIds.length === 0;

  const toggleClientSelection = (clientId: string) => {
    setSelectedClientIds((current) => {
      if (current.includes(clientId)) {
        const next = current.filter((item) => item !== clientId);
        return next;
      }
      return [...current, clientId];
    });
  };

  const selectAllClients = () => {
    setSelectedClientIds([]);
  };

  return (
    <div className="space-y-4 animate-in fade-in duration-500">
      <div className="flex flex-wrap items-end justify-between gap-4">
        <div className="space-y-1">
          <h2 className="text-2xl font-semibold tracking-tight">
            {t("ui.dev.dashboard.title", "Dashboard")}
          </h2>
          <p className="text-sm text-muted-foreground">
            {t(
              "msg.dev.dashboard.description",
              "연동 앱 구성과 인증 운영 지표를 한 곳에서 확인합니다.",
            )}
          </p>
        </div>
      </div>

      <div className="flex flex-wrap items-center gap-x-6 gap-y-2 border-y border-border/60 py-2">
        <OverviewMetric
          icon={<ShieldCheck size={14} />}
          label={t("ui.dev.dashboard.summary.total_clients", "총 RP 수")}
          value={formatMetric(stats?.total_clients ?? clients.length)}
        />
        <OverviewMetric
          icon={<CheckCircle2 size={14} />}
          label={t("ui.dev.dashboard.summary.active_clients", "활성 RP 수")}
          value={formatMetric(distribution.activeClients)}
        />
        <OverviewMetric
          icon={<Activity size={14} />}
          label={t("ui.dev.dashboard.summary.active_sessions", "활성 세션 수")}
          value={formatMetric(stats?.active_sessions)}
        />
        <OverviewMetric
          icon={<AlertTriangle size={14} />}
          label={t(
            "ui.dev.dashboard.summary.auth_failures_24h",
            "24시간 인증 실패 수",
          )}
          value={formatMetric(stats?.auth_failures_24h)}
        />
      </div>

      <section className="space-y-3">
        <div className="flex flex-wrap items-center justify-between gap-3">
          <div className="flex items-center gap-2">
            <BarChart3 size={18} className="text-primary" />
            <div className="space-y-1">
              <h3 className="text-base font-semibold">
                {t(
                  "ui.dev.dashboard.chart.title",
                  "애플리케이션별 로그인요청/기타 요청 현황",
                )}
              </h3>
              <p className="text-sm text-muted-foreground">
                {t(
                  "msg.dev.dashboard.chart.filter_description",
                  "전체 또는 선택한 애플리케이션만 기준으로 그래프를 확인합니다.",
                )}
              </p>
            </div>
          </div>
          <div className="flex h-8 items-center gap-1" aria-label="집계 단위">
            {[
              ["day", t("ui.dev.dashboard.chart.period_day", "일")],
              ["week", t("ui.dev.dashboard.chart.period_week", "주")],
              ["month", t("ui.dev.dashboard.chart.period_month", "월")],
            ].map(([value, label]) => (
              <button
                key={value}
                type="button"
                aria-pressed={period === value}
                onClick={() => setPeriod(value as RPUsagePeriod)}
                className={`h-8 rounded px-3 text-xs font-medium transition-colors ${
                  period === value
                    ? "bg-primary text-primary-foreground"
                    : "bg-muted/60 hover:bg-muted"
                }`}
              >
                {label}
              </button>
            ))}
          </div>
        </div>

        <div className="flex flex-wrap gap-2 rounded-xl border border-border/60 bg-card/60 p-3">
          <label className="inline-flex items-center gap-2 rounded-full border border-border/60 px-3 py-1.5 text-xs">
            <input
              type="checkbox"
              checked={isAllClientsSelected}
              onChange={selectAllClients}
              className="h-3.5 w-3.5"
            />
            <span>{t("ui.dev.dashboard.chart.filter_all", "전체")}</span>
          </label>
          {clientFilterOptions.map((client) => (
            <label
              key={client.id}
              className="inline-flex items-center gap-2 rounded-full border border-border/60 px-3 py-1.5 text-xs"
            >
              <input
                type="checkbox"
                checked={selectedClientIds.includes(client.id)}
                onChange={() => toggleClientSelection(client.id)}
                className="h-3.5 w-3.5"
              />
              <span>{client.label}</span>
            </label>
          ))}
        </div>

        {usageQuery.isError ? (
          <div className="text-sm text-muted-foreground">{usageErrorText}</div>
        ) : isAllClientsSelected ? (
          <RPUsageMixedChart rows={filteredUsageRows} period={period} />
        ) : (
          <RPUsageMixedChart
            rows={filteredUsageRows}
            period={period}
            multiLineSeries={selectedMultiLineSeries}
          />
        )}
      </section>

      <div className="grid gap-4 lg:grid-cols-[1.1fr,0.9fr]">
        <section className="rounded-xl border border-border/60 bg-card p-6">
          <div className="flex items-center gap-2">
            <Layers3 size={18} className="text-primary" />
            <h3 className="text-base font-semibold">
              {t(
                "ui.dev.dashboard.distribution.title",
                "애플리케이션 구성 요약",
              )}
            </h3>
          </div>
          <p className="mt-1 text-sm text-muted-foreground">
            {t(
              "msg.dev.dashboard.distribution.description",
              "애플리케이션 유형과 headless login 사용 현황을 빠르게 확인합니다.",
            )}
          </p>
          <div className="mt-4 grid gap-3 sm:grid-cols-3">
            <div className="rounded-xl border border-border/60 bg-secondary/30 p-4">
              <p className="text-sm text-muted-foreground">
                {t("ui.dev.dashboard.distribution.private", "Server side App")}
              </p>
              <p className="mt-2 text-2xl font-semibold tabular-nums">
                {distribution.privateClients.toLocaleString()}
              </p>
            </div>
            <div className="rounded-xl border border-border/60 bg-secondary/30 p-4">
              <p className="text-sm text-muted-foreground">
                {t("ui.dev.dashboard.distribution.pkce", "PKCE")}
              </p>
              <p className="mt-2 text-2xl font-semibold tabular-nums">
                {distribution.pkceClients.toLocaleString()}
              </p>
            </div>
            <div className="rounded-xl border border-border/60 bg-secondary/30 p-4">
              <p className="text-sm text-muted-foreground">
                {t("ui.dev.dashboard.distribution.headless", "Headless Login")}
              </p>
              <p className="mt-2 text-2xl font-semibold tabular-nums">
                {distribution.headlessClients.toLocaleString()}
              </p>
            </div>
          </div>
        </section>

        <section className="rounded-xl border border-border/60 bg-card p-6">
          <div className="flex items-center gap-2">
            <Layers3 size={18} className="text-primary" />
            <h3 className="text-base font-semibold">
              {t("ui.dev.dashboard.recent.title", "내 애플리케이션")}
            </h3>
          </div>
          <p className="mt-1 text-sm text-muted-foreground">
            {t(
              "msg.dev.dashboard.recent.empty",
              "현재 계정이 접근할 수 있는 RP를 확인합니다.",
            )}
          </p>
          <div className="mt-4 space-y-2">
            {visibleClients.length === 0 ? (
              <p className="text-sm text-muted-foreground">
                {t(
                  "msg.dev.dashboard.recent.none",
                  "표시할 연동 앱이 없습니다.",
                )}
              </p>
            ) : (
              visibleClients.map((client) => (
                <div
                  key={client.id}
                  className="flex items-center justify-between gap-3 rounded-lg border border-border/40 px-3 py-2"
                >
                  <div className="min-w-0">
                    <p className="truncate font-medium">
                      {client.name || t("ui.dev.clients.untitled", "Untitled")}
                    </p>
                    <p className="truncate text-xs text-muted-foreground">
                      {client.id}
                    </p>
                  </div>
                  <div className="text-right text-xs text-muted-foreground">
                    <p>
                      {client.metadata?.headless_login_enabled === true
                        ? t(
                            "ui.dev.clients.type.private_headless",
                            "Server side App (Headless Login)",
                          )
                        : client.type === "private"
                          ? t("ui.dev.clients.type.private", "Server side App")
                          : t("ui.dev.clients.type.pkce", "PKCE")}
                    </p>
                    <p>
                      {client.status === "active"
                        ? t("ui.dev.clients.status.active", "활성")
                        : client.status === "inactive"
                          ? t("ui.dev.clients.status.inactive", "비활성")
                          : client.status || "-"}
                    </p>
                  </div>
                </div>
              ))
            )}
          </div>
        </section>
      </div>
    </div>
  );
}

export default DashboardPage;
