/**
 * rNetwork applet definitions — Contact Card.
 */

import type { AppletDefinition, AppletLiveData } from "../../shared/applet-types";

const contactCard: AppletDefinition = {
	id: "contact-card",
	label: "Contact Card",
	icon: "👤",
	accentColor: "#4f46e5",
	ports: [
		{ name: "did-in", type: "string", direction: "input" },
		{ name: "contact-out", type: "json", direction: "output" },
	],
	renderCompact(data: AppletLiveData): string {
		const { snapshot } = data;
		const name = (snapshot.name as string) || "Unknown";
		const did = (snapshot.did as string) || "";
		const shortDid = did ? `${did.slice(0, 16)}…` : "No DID";
		const trustScore = (snapshot.trustScore as number) || 0;
		const trustColor = trustScore >= 0.7 ? "#22c55e" : trustScore >= 0.4 ? "#f59e0b" : "#94a3b8";

		return `
			<div style="text-align:center">
				<div style="font-size:28px;margin-bottom:4px">👤</div>
				<div style="font-size:13px;font-weight:600;margin-bottom:2px">${name}</div>
				<div style="font-size:9px;color:#94a3b8;font-family:monospace;margin-bottom:6px">${shortDid}</div>
				<div style="font-size:11px;color:${trustColor}">Trust: ${Math.round(trustScore * 100)}%</div>
			</div>
		`;
	},
	onInputReceived(portName, value, ctx) {
		if (portName === "did-in" && typeof value === "string") {
			ctx.emitOutput("contact-out", { did: value, name: "", trustScore: 0 });
		}
	},
};

export const networkApplets: AppletDefinition[] = [contactCard];
