import { AppModal } from './AppModal';

/**
 * Trust-on-first-use, modeled on `p4 trust` / SSH known_hosts.
 *
 * mode="trust": first connection to an https server with no pinned
 * fingerprint yet. Shows the fingerprint for out-of-band verification (e.g.
 * against the server's startup log); accepting pins it and proceeds to log in.
 *
 * mode="mismatch": a later connection presented a *different* fingerprint
 * than the one pinned. This is a hard stop — there is deliberately no
 * "continue anyway" here. The only way forward is the explicit
 * forget-saved-identity action, which drops the pin and returns the next
 * login attempt to first-connect trust.
 */
export function TrustServerModal({ mode, host, cert, onAccept, onDecline, onForget }) {
  if (mode === 'trust') {
    return (
      <AppModal title="Trust this server?" icon="assets/fingerprint.svg" onClose={onDecline}>
        <p>
          First connection to <strong>{host}</strong>. Verify this fingerprint
          matches the one printed in the server&apos;s startup log before
          accepting:
        </p>
        <code className="trust-fingerprint">{cert?.fingerprint ?? 'unavailable'}</code>
        <p className="trust-details">
          {cert?.subject} · valid {cert?.not_before} → {cert?.not_after}
        </p>
        <p>
          If it matches, this identity is remembered and every future
          connection is verified against it.
        </p>
        <div className="revision-modal-actions">
          <button type="button" className="revision-pill-btn" onClick={onDecline}>
            Cancel
          </button>
          <button
            type="button"
            className="revision-pill-btn revision-pill-btn--primary"
            onClick={onAccept}
          >
            Trust and connect
          </button>
        </div>
      </AppModal>
    );
  }

  return (
    <AppModal title="Server identity changed" icon="assets/circle_exclaim.svg" onClose={onDecline}>
      <p>
        <strong>{host}</strong> presented a different certificate than the one
        you trusted. This can mean the server was legitimately reinstalled —
        or that someone is intercepting the connection.
      </p>
      <p>
        <strong>Do not proceed until you know which.</strong> Confirm with the
        server admin. If the identity change is expected, forget the saved
        identity and go through first-connect trust again.
      </p>
      <div className="revision-modal-actions">
        <button
          type="button"
          className="revision-pill-btn revision-pill-btn--primary"
          onClick={onDecline}
        >
          Close
        </button>
        <button
          type="button"
          className="revision-pill-btn revision-pill-btn--danger"
          onClick={onForget}
        >
          Forget saved identity…
        </button>
      </div>
    </AppModal>
  );
}
