All files / src/app/admin/tenants page.tsx

100% Statements 50/50
92.94% Branches 79/85
100% Functions 24/24
100% Lines 49/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688                                                                                                                                      2x                             222x 222x           2x                                                                               111x           111x                   112x 112x 112x 112x   112x 45x 40x   40x 40x 40x   1x     112x 111x     112x 114x   112x 111x       155x 108x         112x 112x 111x 111x     112x                                                                                                                                                                                                                                                                                             111x 111x 111x 111x       108x 155x     111x                                                                                           25x                                                           91x     67x                                                               23x     23x     27x     23x       23x     26x     23x                                                                                                                                                     44x                                                                                                                                                                                       264x                                                                                                      
"use client";
 
import { fetchApi } from "@/lib/api";
import { useEffect, useState } from "react";
import {
  AlertTriangle,
  Building2,
  ChevronDown,
  ChevronUp,
  Circle,
  Info,
  Loader2,
  Users,
  ShieldCheck,
  Activity,
  Clock,
  Edit,
} from "lucide-react";
import Link from "next/link";
 
interface VPA {
  id: string;
  state: string;
  type: string;
  cellId: string;
}
 
interface ProfileProperties {
  "cfm.vpa.state"?: {
    participantContextId?: string;
    credentialRequest?: string;
    holderPid?: string;
  };
  displayName?: string;
  [key: string]: unknown;
}
 
interface ParticipantProfile {
  id: string;
  version: number;
  identifier: string;
  tenantId: string;
  participantRoles: Record<string, string[]>;
  vpas: VPA[];
  properties: ProfileProperties;
  error?: boolean;
}
 
interface Tenant {
  id: string;
  version: number;
  properties: Record<string, string>;
  participantProfiles: ParticipantProfile[];
}
 
interface Participant {
  "@id": string;
  identity: string;
  state: string;
}
 
// CFM TenantManager stores roles in its own taxonomy
// (CLINIC/CRO/HDAB/REGISTRY/etc.) which doesn't match the EDC role names
// (DATA_HOLDER/DATA_USER/HDAB_AUTHORITY/EDC_ADMIN) the rest of this page,
// the Role badge, and the RBAC Summary panel were originally written
// against. Map CFM → EDC here so every consumer (badge label, access
// level pill, RBAC counts) lights up consistently.
const CFM_TO_EDC_ROLE: Record<string, string> = {
  CLINIC: "DATA_HOLDER",
  HOSPITAL: "DATA_HOLDER",
  REGISTRY: "DATA_HOLDER",
  CRO: "DATA_USER",
  PHARMA: "DATA_USER",
  RESEARCHER: "DATA_USER",
  HDAB: "HDAB_AUTHORITY",
  AUTHORITY: "HDAB_AUTHORITY",
  OPERATOR: "EDC_ADMIN",
  ADMIN: "EDC_ADMIN",
};
 
function normalizeRole(t: Tenant): string {
  const raw =
    t.properties.ehdsParticipantType || t.properties.role || "Unknown";
  return CFM_TO_EDC_ROLE[raw.toUpperCase()] ?? raw;
}
 
const ROLE_CONFIG: Record<
  string,
  { label: string; color: string; bg: string; level: string }
> = {
  EDC_ADMIN: {
    label: "Admin",
    color: "text-[var(--role-admin-text)]",
    bg: "bg-[var(--role-admin-bg)]",
    level: "Level 4",
  },
  DATA_HOLDER: {
    label: "Data Holder",
    color: "text-[var(--role-holder-text)]",
    bg: "bg-[var(--role-holder-bg)]",
    level: "Level 3",
  },
  DATA_USER: {
    label: "Researcher",
    color: "text-[var(--role-user-text)]",
    bg: "bg-[var(--role-user-bg)]",
    level: "Level 2",
  },
  HDAB_AUTHORITY: {
    label: "HDAB",
    color: "text-[var(--role-hdab-text)]",
    bg: "bg-[var(--role-hdab-bg)]",
    level: "Level 3",
  },
  TRUST_CENTER_OPERATOR: {
    label: "Trust Center",
    color: "text-[var(--role-trust-text)]",
    bg: "bg-[var(--role-trust-bg)]",
    level: "Level 3",
  },
  PATIENT: {
    label: "Patient",
    color: "text-[var(--role-patient-text)]",
    bg: "bg-[var(--role-patient-bg)]",
    level: "Level 1",
  },
};
 
function RoleBadge({ role }: { role: string }) {
  const cfg = ROLE_CONFIG[role] ?? {
    label: role,
    color: "text-[var(--text-secondary)]",
    bg: "bg-[var(--surface-2)]",
    level: "—",
  };
  return (
    <span
      className={`px-3 py-1 rounded-full text-xs font-bold ${cfg.color} ${cfg.bg}`}
    >
      {cfg.level}
    </span>
  );
}
 
export default function AdminTenantsPage() {
  const [tenants, setTenants] = useState<Tenant[]>([]);
  const [participants, setParticipants] = useState<Participant[]>([]);
  const [loading, setLoading] = useState(true);
  const [expanded, setExpanded] = useState<string | null>(null);
 
  useEffect(() => {
    fetchApi("/api/admin/tenants")
      .then((r) => (r.ok ? r.json() : ({} as Record<string, unknown>)))
      .then((d: Record<string, unknown>) => {
        setTenants((d.tenants as Tenant[]) || []);
        setParticipants((d.participants as Participant[]) || []);
        setLoading(false);
      })
      .catch(() => setLoading(false));
  }, []);
 
  const totalProfiles = tenants.reduce(
    (s, t) => s + (t.participantProfiles?.length || 0),
    0,
  );
  const activeParticipants = participants.filter(
    (p) => p.state === "ACTIVE",
  ).length;
  const disposedCount = tenants.reduce((s, t) => {
    return (
      s +
      (t.participantProfiles || []).reduce((ps, pp) => {
        const allDisposed =
          pp.vpas?.length > 0 && pp.vpas.every((v) => v.state === "disposed");
        return ps + (allDisposed ? 1 : 0);
      }, 0)
    );
  }, 0);
 
  const roleCounts: Record<string, number> = {};
  tenants.forEach((t) => {
    const role = normalizeRole(t);
    roleCounts[role] = (roleCounts[role] || 0) + 1;
  });
 
  return (
    <div className="min-h-screen bg-[var(--bg)]">
      <main className="px-8 py-10 max-w-7xl mx-auto space-y-8">
        {/* ── Page header ── */}
        <div className="flex flex-col md:flex-row md:items-end justify-between gap-4">
          <div>
            <h1 className="page-header">Tenant Management</h1>
            <p className="text-[var(--text-secondary)] text-lg mt-1">
              Manage EHDS participant organisations, roles, and dataspace access
            </p>
            <p className="text-xs text-[var(--text-secondary)] mt-2 max-w-2xl flex items-start gap-1.5">
              <Info
                size={14}
                className="mt-0.5 shrink-0 text-[var(--accent)]"
              />
              <span>
                <strong className="text-[var(--text-primary)]">
                  VPA (Virtual Participant Agent)
                </strong>{" "}
                — a runtime instance provisioned by the CFM Tenant Manager for a
                participant. Each tenant typically runs three VPAs:
                <code>cfm.connector</code> (EDC-V control plane),{" "}
                <code>cfm.credentialservice</code> (IdentityHub), and{" "}
                <code>cfm.dataplane</code> (DCore data plane). State{" "}
                <code>disposed</code> means the agent was terminated by a demo
                reset — re-seed the tenants to recreate.
              </span>
            </p>
          </div>
          <Link
            href="/admin"
            className="text-sm font-bold text-[var(--accent)] hover:underline uppercase tracking-wider"
          >
            ← Operator Dashboard
          </Link>
        </div>
 
        {loading ? (
          <div className="flex items-center gap-2 text-[var(--text-secondary)]">
            <Loader2 size={16} className="animate-spin" />
            Loading tenants…
          </div>
        ) : (
          <>
            <div className="grid grid-cols-1 lg:grid-cols-12 gap-6">
              {/* ── Left column: stats + table ── */}
              <div className="lg:col-span-8 space-y-6">
                {/* Bento stat grid */}
                <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
                  <div className="stat-card border-l-[var(--accent)]">
                    <div className="flex justify-between items-start mb-4">
                      <div className="p-2 bg-[var(--accent)]/5 rounded-lg">
                        <Building2 size={20} className="text-[var(--accent)]" />
                      </div>
                    </div>
                    <p className="text-sm font-medium text-[var(--text-secondary)]">
                      Total Tenants
                    </p>
                    <p className="text-3xl font-black text-[var(--text-primary)] mt-1 tabular-nums">
                      {tenants.length}
                    </p>
                  </div>
                  <div className="stat-card border-l-[var(--success-text)]">
                    <div className="flex justify-between items-start mb-4">
                      <div className="p-2 bg-[var(--success)]/5 rounded-lg">
                        <Activity
                          size={20}
                          className="text-[var(--success-text)]"
                        />
                      </div>
                      {activeParticipants > 0 && (
                        <span className="text-xs font-bold text-[var(--success-text)] bg-[var(--success)]/10 px-2 py-0.5 rounded-full">
                          {activeParticipants} active
                        </span>
                      )}
                    </div>
                    <p className="text-sm font-medium text-[var(--text-secondary)]">
                      Participant Profiles
                    </p>
                    <p className="text-3xl font-black text-[var(--text-primary)] mt-1 tabular-nums">
                      {totalProfiles}
                    </p>
                  </div>
                  <div className="stat-card border-l-[var(--warning-text)]">
                    <div className="flex justify-between items-start mb-4">
                      <div className="p-2 bg-[var(--warning)]/5 rounded-lg">
                        <Clock
                          size={20}
                          className="text-[var(--warning-text)]"
                        />
                      </div>
                      {disposedCount > 0 && (
                        <span className="animate-pulse w-2 h-2 rounded-full bg-[var(--warning)]" />
                      )}
                    </div>
                    <p
                      className="text-sm font-medium text-[var(--text-secondary)] flex items-center gap-1.5"
                      title="Virtual Participant Agents (one per dataspace profile per tenant). Disposed = terminated by demo reset; re-seed to recreate."
                    >
                      Disposed VPAs
                      <Info
                        size={12}
                        className="text-[var(--text-secondary)] opacity-60"
                      />
                    </p>
                    <p className="text-3xl font-black text-[var(--text-primary)] mt-1 tabular-nums">
                      {disposedCount}
                    </p>
                  </div>
                </div>
 
                {/* Tenants table */}
                <div className="bg-[var(--surface-card)] rounded-xl shadow-sm overflow-hidden border border-[var(--border)]">
                  <div className="p-6 border-b border-[var(--border)] flex items-center justify-between gap-4">
                    <h3 className="text-lg font-bold text-[var(--text-primary)]">
                      Registered Organisations
                    </h3>
                    <Link
                      href="/admin/policies"
                      className="px-4 py-2 text-sm font-bold text-white bg-[var(--accent)] rounded-xl hover:opacity-90 transition-opacity"
                    >
                      Policy Definitions →
                    </Link>
                  </div>
 
                  {tenants.length === 0 ? (
                    <p className="p-6 text-[var(--text-secondary)] text-sm">
                      No tenants registered — connect to live stack
                    </p>
                  ) : (
                    <div className="overflow-x-auto">
                      <table className="w-full text-left">
                        <thead className="bg-[var(--surface)] text-xs font-bold text-[var(--text-secondary)] uppercase tracking-wider">
                          <tr>
                            <th className="px-6 py-4">Organisation</th>
                            <th className="px-6 py-4">Role</th>
                            <th className="px-6 py-4">Access Level</th>
                            <th className="px-6 py-4">Status</th>
                            <th className="px-6 py-4 text-right">Details</th>
                          </tr>
                        </thead>
                        <tbody className="divide-y divide-[var(--border)]">
                          {tenants.map((t) => {
                            const isOpen = expanded === t.id;
                            const role = normalizeRole(t);
                            const roleCfg = ROLE_CONFIG[role] ?? null;
                            const allVpasDisposed = (
                              t.participantProfiles || []
                            ).some(
                              (pp) =>
                                pp.vpas?.length > 0 &&
                                pp.vpas.every((v) => v.state === "disposed"),
                            );
 
                            return (
                              <>
                                <tr
                                  key={t.id}
                                  className="hover:bg-[var(--surface)] transition-colors"
                                >
                                  <td className="px-6 py-4">
                                    <div className="flex items-center gap-3">
                                      <div className="w-10 h-10 rounded-full bg-[var(--accent)]/10 flex items-center justify-center shrink-0">
                                        <Building2
                                          size={18}
                                          className="text-[var(--accent)]"
                                        />
                                      </div>
                                      <div>
                                        <p className="text-sm font-bold text-[var(--text-primary)]">
                                          {t.properties.displayName || t.id}
                                        </p>
                                        <p className="text-[10px] text-[var(--text-secondary)]">
                                          {t.properties.organization || t.id}
                                        </p>
                                      </div>
                                    </div>
                                  </td>
                                  <td className="px-6 py-4 text-sm text-[var(--text-primary)]">
                                    {roleCfg?.label ?? role}
                                  </td>
                                  <td className="px-6 py-4">
                                    <RoleBadge role={role} />
                                  </td>
                                  <td className="px-6 py-4">
                                    {allVpasDisposed ? (
                                      <div className="flex items-center gap-2 text-xs font-medium text-[var(--warning-text)]">
                                        <span className="w-1.5 h-1.5 rounded-full bg-[var(--warning)]" />
                                        Disposed
                                      </div>
                                    ) : (
                                      <div className="flex items-center gap-2 text-xs font-medium text-[var(--success-text)]">
                                        <span className="w-1.5 h-1.5 rounded-full bg-[var(--success)]" />
                                        Active
                                      </div>
                                    )}
                                  </td>
                                  <td className="px-6 py-4 text-right">
                                    <button
                                      onClick={() =>
                                        setExpanded(isOpen ? null : t.id)
                                      }
                                      className="p-2 text-[var(--text-secondary)] hover:text-[var(--accent)] transition-colors"
                                      aria-label={
                                        isOpen
                                          ? "Collapse details"
                                          : "Expand details"
                                      }
                                    >
                                      {isOpen ? (
                                        <ChevronUp size={16} />
                                      ) : (
                                        <ChevronDown size={16} />
                                      )}
                                    </button>
                                  </td>
                                </tr>
 
                                {/* Expanded detail row */}
                                {isOpen && (
                                  <tr key={`${t.id}-detail`}>
                                    <td
                                      colSpan={5}
                                      className="px-6 py-4 bg-[var(--surface)] border-b border-[var(--border)]"
                                    >
                                      <div className="space-y-4">
                                        {/* Properties */}
                                        <div className="grid grid-cols-2 md:grid-cols-4 gap-3">
                                          {Object.entries(t.properties)
                                            .filter(
                                              ([k]) => k !== "displayName",
                                            )
                                            .map(([k, v]) => (
                                              <div
                                                key={k}
                                                className="bg-[var(--surface-card)] rounded-xl px-3 py-2.5"
                                              >
                                                <p className="text-[10px] text-[var(--text-secondary)] uppercase tracking-wider font-bold">
                                                  {k}
                                                </p>
                                                <p className="text-xs text-[var(--text-primary)] font-medium mt-0.5 break-all">
                                                  {v}
                                                </p>
                                              </div>
                                            ))}
                                          <div className="bg-[var(--surface-card)] rounded-xl px-3 py-2.5">
                                            <p className="text-[10px] text-[var(--text-secondary)] uppercase tracking-wider font-bold">
                                              Tenant ID
                                            </p>
                                            <p className="text-xs text-[var(--text-primary)] font-mono mt-0.5 break-all">
                                              {t.id}
                                            </p>
                                          </div>
                                        </div>
 
                                        {/* Participant profiles */}
                                        {t.participantProfiles?.length > 0 && (
                                          <div>
                                            <p className="section-label">
                                              Dataspace Profiles
                                            </p>
                                            <div className="grid md:grid-cols-2 gap-3">
                                              {t.participantProfiles.map(
                                                (pp, i) => {
                                                  const ctxId =
                                                    pp.properties?.[
                                                      "cfm.vpa.state"
                                                    ]?.participantContextId;
                                                  const ctx = ctxId
                                                    ? participants.find(
                                                        (p) =>
                                                          p["@id"] === ctxId,
                                                      )
                                                    : undefined;
                                                  const roles = Object.values(
                                                    pp.participantRoles || {},
                                                  ).flat();
                                                  const allDisposed =
                                                    pp.vpas?.length > 0 &&
                                                    pp.vpas.every(
                                                      (v) =>
                                                        v.state === "disposed",
                                                    );
 
                                                  return (
                                                    <div
                                                      key={i}
                                                      className={`p-4 rounded-xl border text-xs space-y-3 ${
                                                        pp.error
                                                          ? "border-[var(--warning)]/30 bg-[var(--warning)]/5"
                                                          : "border-[var(--border)] bg-[var(--surface-card)]"
                                                      }`}
                                                    >
                                                      {allDisposed && (
                                                        <div className="flex items-center gap-1.5 text-[var(--warning-text)] text-[11px]">
                                                          <AlertTriangle
                                                            size={12}
                                                          />
                                                          VPAs disposed — re-run{" "}
                                                          <code className="bg-[var(--warning)]/10 px-1 rounded font-mono">
                                                            seed-health-tenants.sh
                                                          </code>
                                                        </div>
                                                      )}
                                                      <div className="grid grid-cols-2 gap-x-4 gap-y-2">
                                                        <div>
                                                          <span className="text-[var(--text-secondary)]">
                                                            Profile ID{" "}
                                                          </span>
                                                          <span className="text-[var(--text-primary)] font-mono">
                                                            {pp.id}
                                                          </span>
                                                        </div>
                                                        <div>
                                                          <span className="text-[var(--text-secondary)]">
                                                            DID{" "}
                                                          </span>
                                                          <span className="text-[var(--text-primary)] font-mono break-all">
                                                            {pp.identifier
                                                              ? decodeURIComponent(
                                                                  pp.identifier,
                                                                )
                                                              : "—"}
                                                          </span>
                                                        </div>
                                                        <div>
                                                          <span className="text-[var(--text-secondary)]">
                                                            Roles{" "}
                                                          </span>
                                                          <span className="text-[var(--text-primary)]">
                                                            {roles.length > 0
                                                              ? roles.join(", ")
                                                              : "—"}
                                                          </span>
                                                        </div>
                                                        {ctx && (
                                                          <div>
                                                            <span className="text-[var(--text-secondary)]">
                                                              State{" "}
                                                            </span>
                                                            <span
                                                              className={`font-bold ${
                                                                ctx.state ===
                                                                "ACTIVE"
                                                                  ? "text-[var(--success-text)]"
                                                                  : ctx.state ===
                                                                      "CREATED"
                                                                    ? "text-[var(--accent)]"
                                                                    : "text-[var(--warning-text)]"
                                                              }`}
                                                            >
                                                              {ctx.state}
                                                            </span>
                                                          </div>
                                                        )}
                                                      </div>
                                                      {pp.vpas?.length > 0 && (
                                                        <div className="flex flex-wrap gap-2 pt-1">
                                                          {pp.vpas.map((v) => (
                                                            <span
                                                              key={v.id}
                                                              className={`flex items-center gap-1 px-2 py-1 rounded-full text-[10px] font-semibold ${
                                                                v.state ===
                                                                "active"
                                                                  ? "bg-[var(--success)]/10 text-[var(--success-text)]"
                                                                  : v.state ===
                                                                      "disposed"
                                                                    ? "bg-[var(--surface-2)] text-[var(--text-secondary)]"
                                                                    : "bg-[var(--warning)]/10 text-[var(--warning-text)]"
                                                              }`}
                                                            >
                                                              <Circle
                                                                size={5}
                                                                className={
                                                                  v.state ===
                                                                  "active"
                                                                    ? "fill-[var(--success)]"
                                                                    : "fill-current opacity-50"
                                                                }
                                                              />
                                                              {v.type.replace(
                                                                "cfm.",
                                                                "",
                                                              )}{" "}
                                                              {v.state}
                                                            </span>
                                                          ))}
                                                        </div>
                                                      )}
                                                    </div>
                                                  );
                                                },
                                              )}
                                            </div>
                                          </div>
                                        )}
                                      </div>
                                    </td>
                                  </tr>
                                )}
                              </>
                            );
                          })}
                        </tbody>
                      </table>
                    </div>
                  )}
                </div>
              </div>
 
              {/* ── Right column: RBAC Summary ── */}
              <div className="lg:col-span-4">
                <div className="bg-[var(--surface)] rounded-xl p-8 h-full">
                  <h3 className="text-lg font-bold text-[var(--text-primary)] mb-6">
                    RBAC Summary
                  </h3>
                  <div className="space-y-4">
                    {[
                      {
                        role: "EDC_ADMIN",
                        icon: ShieldCheck,
                        label: "Administrators",
                        sub: "Full Access (L4)",
                        color: "text-[var(--role-admin-text)]",
                        bg: "bg-[var(--role-admin-bg)]",
                      },
                      {
                        role: "DATA_HOLDER",
                        icon: Building2,
                        label: "Data Holders",
                        sub: "Write Access (L3)",
                        color: "text-[var(--role-holder-text)]",
                        bg: "bg-[var(--role-holder-bg)]",
                      },
                      {
                        role: "DATA_USER",
                        icon: Users,
                        label: "Researchers",
                        sub: "Read-only (L1–2)",
                        color: "text-[var(--role-user-text)]",
                        bg: "bg-[var(--role-user-bg)]",
                      },
                      {
                        role: "HDAB_AUTHORITY",
                        icon: ShieldCheck,
                        label: "HDAB",
                        sub: "Governance (L3)",
                        color: "text-[var(--role-hdab-text)]",
                        bg: "bg-[var(--role-hdab-bg)]",
                      },
                    ].map(({ role, icon: Icon, label, sub, color, bg }) => (
                      <div
                        key={role}
                        className="flex items-center justify-between p-4 bg-[var(--surface-card)] rounded-xl"
                      >
                        <div className="flex items-center gap-3">
                          <div
                            className={`w-10 h-10 rounded-full ${bg} flex items-center justify-center shrink-0`}
                          >
                            <Icon size={18} className={color} />
                          </div>
                          <div>
                            <p className="text-sm font-bold text-[var(--text-primary)]">
                              {label}
                            </p>
                            <p className="text-xs text-[var(--text-secondary)]">
                              {sub}
                            </p>
                          </div>
                        </div>
                        <span className="text-sm font-black text-[var(--text-primary)] tabular-nums">
                          {roleCounts[role] ?? 0}
                        </span>
                      </div>
                    ))}
                  </div>
 
                  {/* Security notice */}
                  <div className="mt-8 p-6 bg-[var(--accent)] text-white dark:text-gray-900 rounded-xl">
                    <p className="text-xs font-bold uppercase tracking-widest mb-2 opacity-80">
                      Security Notice
                    </p>
                    <p className="text-sm mb-4 opacity-90">
                      All Level 4 changes require secondary hardware key
                      validation per EHDS Art. 50.
                    </p>
                    <Link
                      href="/admin/audit"
                      className="text-xs font-bold border border-white/30 px-3 py-1.5 rounded-full hover:bg-white/10 transition-colors inline-block"
                    >
                      Audit Logs
                    </Link>
                  </div>
                </div>
              </div>
            </div>
          </>
        )}
      </main>
    </div>
  );
}