import { useState, useMemo } from 'react'; import { useProvisionPilot } from '../../api/pilots'; import { useConsortium } from '../../api/queries'; import { formatApiError } from '../../api/client'; import { X, ShieldPlus, Server, AlertCircle, Info } from 'lucide-react'; interface PilotProvisionModalProps { isOpen: boolean; onClose: () => void; onSuccess: (pilotId: string) => void; } const STANDARD_PURPOSES = [ 'AML_COLLABORATIVE_INVESTIGATION', 'CROSS_BORDER_FRAUD_RING_DETECTION', 'MULE_ACCOUNT_RECONCILIATION', ]; export default function PilotProvisionModal({ isOpen, onClose, onSuccess }: PilotProvisionModalProps) { const [pilotId, setPilotId] = useState(''); const [consortiumId, setConsortiumId] = useState(''); const [bankA, setBankA] = useState(''); const [bankB, setBankB] = useState(''); const [purpose, setPurpose] = useState('AML_COLLABORATIVE_INVESTIGATION'); const [jurisdiction, setJurisdiction] = useState(''); const [grantId, setGrantId] = useState(''); const [quotaTotal, setQuotaTotal] = useState(50); const [quotaPerBank, setQuotaPerBank] = useState(25); const [durationDays, setDurationDays] = useState(14); const [coordUrl, setCoordUrl] = useState(''); const [bankAUrl, setBankAUrl] = useState(''); const [bankBUrl, setBankBUrl] = useState(''); const [formError, setFormError] = useState(null); const cleanConsortiumId = consortiumId.trim(); const { data: consortium, isLoading: isConsortiumLoading } = useConsortium(cleanConsortiumId || undefined); const candidateBanks = useMemo(() => { if (!consortium?.members || consortium.members.length === 0) return []; return consortium.members.map((m) => m.bank_id); }, [consortium]); const provisionMutation = useProvisionPilot(); if (!isOpen) return null; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setFormError(null); const cleanPilotId = pilotId.trim(); const cleanBankA = bankA.trim().toLowerCase(); const cleanBankB = bankB.trim().toLowerCase(); if (!cleanPilotId || !cleanConsortiumId) { setFormError('Pilot ID and Governing Consortium ID are required.'); return; } if (!cleanBankA || !cleanBankB) { setFormError('Both Participating Institution A and B must be specified.'); return; } if (cleanBankA === cleanBankB) { setFormError('Participating Institution A and Institution B must be distinct institutional entities.'); return; } const topo: Record = {}; if (coordUrl.trim()) topo['coordinator_url'] = coordUrl.trim(); if (bankAUrl.trim()) { topo['bank_a_worker_url'] = bankAUrl.trim(); topo[`${cleanBankA}_worker_url`] = bankAUrl.trim(); } if (bankBUrl.trim()) { topo['bank_b_worker_url'] = bankBUrl.trim(); topo[`${cleanBankB}_worker_url`] = bankBUrl.trim(); } try { const res = await provisionMutation.mutateAsync({ pilot_id: cleanPilotId, consortium_id: cleanConsortiumId, participating_institutions: [cleanBankA, cleanBankB], allowed_purposes: [purpose.trim()], allowed_jurisdictions: jurisdiction.trim() ? [jurisdiction.trim()] : [], operational_grant_id: grantId.trim() || undefined, query_quota_total: Number(quotaTotal), query_quota_per_institution: Number(quotaPerBank), duration_days: Number(durationDays), topology: Object.keys(topo).length > 0 ? topo : undefined, }); onSuccess(res.pilot_id); onClose(); } catch (err) { setFormError(formatApiError(err, 'Failed to provision pilot.')); } }; return (
{/* Header */}

Provision Governed Two-Bank Controlled Pilot

Provisions an authorized bilateral pilot in DRAFT state. Calculates the initial immutable scope digest and establishes durable reservation quotas.

{formError && (
{formError}
)}
setPilotId(e.target.value)} placeholder="e.g. pilot_bilateral_2026_q4" pattern="^[a-zA-Z0-9_\-]+$" required className="w-full text-xs font-mono bg-white border border-slate-200 rounded-lg p-2.5 text-slate-900 placeholder-slate-500 focus:outline-none focus:border-cyan-500" /> Alphanumeric, underscores, hyphens only
setConsortiumId(e.target.value)} placeholder="e.g. cfi-consortium" required className="w-full text-xs font-mono bg-white border border-slate-200 rounded-lg p-2.5 text-slate-900 placeholder-slate-500 focus:outline-none focus:border-cyan-500" /> {cleanConsortiumId && ( {isConsortiumLoading ? 'Validating consortium...' : consortium ? `Consortium: ${consortium.name || consortium.consortium_id} (${consortium.status})` : 'Consortium membership will be verified by backend.'} )}
{candidateBanks.length > 0 ? ( ) : ( setBankA(e.target.value)} placeholder="e.g. bank_a" required className="w-full text-xs font-mono bg-white border border-slate-200 rounded-lg p-2.5 text-slate-900 placeholder-slate-500 focus:outline-none focus:border-cyan-500" /> )}
{candidateBanks.length > 0 ? ( ) : ( setBankB(e.target.value)} placeholder="e.g. bank_b" required className="w-full text-xs font-mono bg-white border border-slate-200 rounded-lg p-2.5 text-slate-900 placeholder-slate-500 focus:outline-none focus:border-cyan-500" /> )}
setJurisdiction(e.target.value)} placeholder="e.g. eu-central-1" className="w-full text-xs font-mono bg-white border border-slate-200 rounded-lg p-2.5 text-slate-900 placeholder-slate-500 focus:outline-none focus:border-cyan-500" />
setDurationDays(Number(e.target.value))} className="w-full text-xs font-mono bg-white border border-slate-200 rounded-lg p-2.5 text-slate-900 focus:outline-none focus:border-cyan-500" />
setQuotaTotal(Number(e.target.value))} className="w-full text-xs font-mono bg-white border border-slate-200 rounded-lg p-2.5 text-slate-900 focus:outline-none focus:border-cyan-500" />
setQuotaPerBank(Number(e.target.value))} className="w-full text-xs font-mono bg-white border border-slate-200 rounded-lg p-2.5 text-slate-900 focus:outline-none focus:border-cyan-500" />
setGrantId(e.target.value)} placeholder="Optional Grant Reference" className="w-full text-xs font-mono bg-white border border-slate-200 rounded-lg p-2.5 text-slate-900 placeholder-slate-500 focus:outline-none focus:border-cyan-500" />
{/* Topology Details */}
Operational Topology Endpoints
Optional during initial DRAFT
Provide explicit operational hostnames or URLs. If endpoints are not yet configured, the pilot will remain in truthful DRAFT state and preflight will require verified endpoints before approval.
setCoordUrl(e.target.value)} placeholder="https://coordinator.consortium..." className="w-full text-xs bg-white border border-slate-200 rounded p-1.5 text-slate-900 placeholder-slate-600" />
setBankAUrl(e.target.value)} placeholder="https://worker-a.bank..." className="w-full text-xs bg-white border border-slate-200 rounded p-1.5 text-slate-900 placeholder-slate-600" />
setBankBUrl(e.target.value)} placeholder="https://worker-b.bank..." className="w-full text-xs bg-white border border-slate-200 rounded p-1.5 text-slate-900 placeholder-slate-600" />
); }