
/* global React */
// =============================================================================
// Nugget — node catalog. Defines the loan lifecycle flow.
// =============================================================================

const NODE_CATALOG = {
  // ---------- Triggers ----------
  webhook: {
    kind: 'trigger', accent: 'green',
    name: 'Webhook', sub: 'HTTP endpoint',
    icon: 'bolt', tag: 'TRIGGER',
    description: 'Fires when an external service posts to this endpoint.',
    fields: { url: 'https://api.nugget.ai/hooks/wf-072', auth: 'Bearer token' },
  },
  schedule: {
    kind: 'trigger', accent: 'green',
    name: 'Schedule', sub: 'Run on a cadence',
    icon: 'calendar-clock', tag: 'TRIGGER',
    description: 'Triggers the workflow on a recurring schedule.',
    fields: { cron: 'Every 15 min', timezone: 'Asia/Kolkata' },
  },
  manual: {
    kind: 'trigger', accent: 'green',
    name: 'Manual run', sub: 'Trigger from console',
    icon: 'play-circle', tag: 'TRIGGER',
    description: 'Operator kicks off the run from the dashboard.',
    fields: { allow: 'admins, analysts' },
  },

  // ---------- Complete Loan Lifecycle ----------
  fos: {
    kind: 'agent', accent: 'peach',
    name: 'FOS - Lead Capture', sub: 'Initial customer contact',
    icon: 'map-search', tag: 'STEP 1',
    description: 'Field officer captures customer information via voice recording. Lead is created in the system.',
    inputs: ['customer_contact'],
    outputs: ['lead_created'],
    fields: {
      method: 'Voice',
      data: 'Name · Business · Loan amount · Location · Loan purpose',
      example: 'Dushyant Garg · ₹40L',
      status: 'Lead created',
    },
  },
  leadmgmt: {
    kind: 'agent', accent: 'blue',
    name: 'LMS - Lead Queue', sub: 'Lead qualification',
    icon: 'filter-list', tag: 'STEP 2',
    description: 'Lead appears in LMS table. Team reviews, qualifies, and routes leads. Receives real-time status updates from all downstream processes (calls, credit, approval).',
    inputs: ['lead_from_fos', 'status_updates'],
    outputs: ['qualified', 'needs_followup', 'rejected'],
    fields: {
      qualification: 'Amount check · Data validation',
      filter: '₹30L - ₹50L',
      updates: 'Call status · Credit decision · Approval',
      views: 'Table · Kanban',
    },
  },
  campaign: {
    kind: 'agent', accent: 'green',
    name: 'NCM - Campaign Grid', sub: 'Activity tracking',
    icon: 'campaign', tag: 'STEP 3',
    description: 'Lead appears in NCM campaign grid. Team tracks all activities and receives real-time status updates from calls, credit evaluation, and approval stages.',
    inputs: ['lead_from_fos', 'status_updates'],
    outputs: ['activity_tracked', 'timeline_updated'],
    fields: {
      tracking: 'Calls · Credit · Approval status',
      timeline: 'Real-time event updates',
      updates: 'Auto-synced from all stages',
      realtime: 'Live status feed',
    },
  },
  call_out: {
    kind: 'action', accent: 'green',
    name: 'Call - Initiation', sub: 'Details collection · Form completion',
    icon: 'phone', tag: 'STEP 4',
    description: 'Call to customer to capture additional info and verify details. Updates LMS/NCM with call status, notes, and next steps in real-time.',
    fields: {
      type: 'AI Voice Agent · Automated outbound',
      purpose: 'Info capture · Verification',
      updates: 'Status → LMS/NCM',
    },
  },
  loan: {
    kind: 'agent', accent: 'lilac',
    name: 'Credit Team', sub: 'Evaluation & approval',
    icon: 'receipt-long', tag: 'STEP 5',
    description: 'Credit team performs underwriting checks and makes approval decision. Decision and details are immediately updated in LMS/NCM for visibility.',
    inputs: ['customer_info', 'call_data'],
    outputs: ['approved', 'rejected', 'more_info_needed'],
    fields: {
      evaluation: 'Credit score · Income verification',
      checks: 'Bureau · Banking · Documents',
      decision: 'Approve · Reject · Request more info',
      updates: 'Status → LMS/NCM in real-time',
    },
  },
  call_out_approval: {
    kind: 'action', accent: 'blue',
    name: 'Call - Plan Presentation', sub: 'Plan presentation · Negotiation',
    icon: 'phone', tag: 'STEP 6',
    description: 'Call to customer with approval decision. Shares loan amount, interest rate, tenure, and next steps. Updates LMS/NCM with call outcome.',
    fields: {
      type: 'AI Voice Agent · Plan presented · Rate negotiated · Meeting scheduled',
      purpose: 'Decision sharing · Terms discussion',
      info: 'Amount · Rate · Tenure · Documents',
      example: '15.5% rate negotiated · Wednesday 11AM meeting confirmed',
      updates: 'Call status → LMS/NCM',
    },
  },
  fos_close: {
    kind: 'agent', accent: 'peach',
    name: 'FOS - Final Handoff', sub: 'Loan closure & docs',
    icon: 'check-circle', tag: 'STEP 7',
    description: 'Field officer receives final approval details, collects signed documents from customer, and completes disbursement process.',
    fields: {
      stage: 'Approved · Ready for disbursal',
      documents: 'Signed agreement · Bank details',
      handoff: 'Via in-person meeting · Vikram',
      sla: '24-48 hours',
    },
  },
  kyc: {
    kind: 'agent', accent: 'amber',
    name: 'Campaign Tracking', sub: 'Activity monitoring',
    icon: 'safe-shield-fill', tag: 'AGENT',
    description: 'Tracks campaign activities from NCM, logs calls, updates timeline events, and monitors lead engagement.',
    inputs: ['ncm_lead'],
    outputs: ['activity_logged', 'status_updated'],
    fields: {
      tracking: 'Calls · Timeline · Status',
      events: 'Call logs · Status changes',
      realtime: 'Live activity feed',
      agents: '24 active',
    },
    metrics: { activities: '1,540', tracked: '92.4%', avgLatency: '480ms' },
  },

  // ---------- Logic / control ----------
  branch: {
    kind: 'logic', accent: 'blue',
    name: 'Branch', sub: 'If / else',
    icon: 'arrow-split', tag: 'LOGIC',
    description: 'Routes the run based on conditional rules on the incoming payload.',
    fields: { condition: 'score >= 0.62', truePath: 'qualified', falsePath: 'nurture' },
  },
  delay: {
    kind: 'logic', accent: 'blue',
    name: 'Delay', sub: 'Wait before next step',
    icon: 'hourglass-bottom', tag: 'LOGIC',
    description: 'Pauses execution for a fixed or dynamic interval.',
    fields: { duration: '15 min' },
  },

  // ---------- Actions / outputs ----------
  notify: {
    kind: 'action', accent: 'ink',
    name: 'Send notification', sub: 'Slack / Email / push',
    icon: 'bell', tag: 'ACTION',
    description: 'Sends an out-of-band notification to an operator or channel.',
    fields: { target: '#ops-loans', template: 'kyc_review' },
  },
  webhook_out: {
    kind: 'action', accent: 'ink',
    name: 'Webhook out', sub: 'POST to URL',
    icon: 'arrow-outward', tag: 'ACTION',
    description: 'POSTs the current payload to an external endpoint.',
    fields: { url: 'https://crm.acme.in/inbound', retries: '3 × exponential' },
  },
  store: {
    kind: 'action', accent: 'ink',
    name: 'Save to datastore', sub: 'Append row',
    icon: 'database', tag: 'ACTION',
    description: 'Persists the payload to the configured datastore.',
    fields: { table: 'leads.qualified', mode: 'upsert' },
  },
};

// ----- icon resolution: fall back to a generic if dock glyph is missing
function resolveIcon(name) {
  if (!name) return 'apps';
  // try the name, then '-1' filled variant, then generic
  return name;
}

// =============================================================================
// Curved edge path
// =============================================================================
function bezierPath(x1, y1, x2, y2) {
  const dx = Math.max(40, Math.abs(x2 - x1) * 0.5);
  return `M ${x1} ${y1} C ${x1 + dx} ${y1}, ${x2 - dx} ${y2}, ${x2} ${y2}`;
}

Object.assign(window, { NODE_CATALOG, resolveIcon, bezierPath });
