{
  "name": "WorkerVoice AI \u2014 Main Complaint Pipeline",
  "nodes": [
    {
      "id": "1",
      "name": "Webhook \u2014 Complaint Intake",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        250,
        300
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "workervoice",
        "responseMode": "responseNode",
        "options": {}
      }
    },
    {
      "id": "2",
      "name": "LibreTranslate \u2014 Normalise to English",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        500,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "http://libretranslate:5000/translate",
        "sendBody": true,
        "contentType": "json",
        "body": {
          "q": "={{ $json.complaint_text }}",
          "source": "auto",
          "target": "en"
        }
      }
    },
    {
      "id": "3",
      "name": "Groq LLaMA \u2014 Legal Analysis",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        750,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.groq.com/openai/v1/chat/completions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "contentType": "json",
        "body": {
          "model": "llama-3.3-70b-versatile",
          "max_tokens": 1000,
          "messages": [
            {
              "role": "system",
              "content": "You are an expert in Indian labour law. Analyse the complaint and respond ONLY with valid JSON: {\"violation_type\": \"\", \"applicable_law\": \"\", \"law_sections\": [], \"severity\": \"low|medium|high\", \"authority\": \"\", \"authority_portal\": \"\", \"complaint_formal\": \"\", \"legal_basis\": \"\", \"relief_sought\": []}"
            },
            {
              "role": "user",
              "content": "Complaint: {{ $json.translatedText }}\nViolation type stated: {{ $('Webhook \u2014 Complaint Intake').item.json.violation_type }}\nState: {{ $('Webhook \u2014 Complaint Intake').item.json.state }}\nEmployer: {{ $('Webhook \u2014 Complaint Intake').item.json.employer }}"
            }
          ]
        }
      }
    },
    {
      "id": "4",
      "name": "Parse Analysis JSON",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1000,
        300
      ],
      "parameters": {
        "jsCode": "const response = $input.item.json;\nconst content = response.choices[0].message.content;\nlet analysis;\ntry {\n  const match = content.match(/\\{[\\s\\S]*\\}/);\n  analysis = JSON.parse(match[0]);\n} catch(e) {\n  analysis = {\n    violation_type: $('Webhook \u2014 Complaint Intake').item.json.violation_type,\n    applicable_law: 'Code on Wages 2019',\n    severity: 'medium',\n    authority: 'State Labour Department',\n    authority_portal: 'Shram Suvidha Portal',\n    complaint_formal: content,\n    relief_sought: ['Appropriate action under applicable law']\n  };\n}\nreturn [{ json: { ...analysis, original: $('Webhook \u2014 Complaint Intake').item.json } }];"
      }
    },
    {
      "id": "5",
      "name": "Generate PDF \u2014 ReportLab",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1250,
        200
      ],
      "parameters": {
        "method": "POST",
        "url": "http://backend:8000/api/generate-pdf",
        "sendBody": true,
        "contentType": "json",
        "body": {
          "case_id": "={{ $json.original.case_id }}",
          "worker_name": "={{ $json.original.worker_name }}",
          "phone": "={{ $json.original.phone }}",
          "state": "={{ $json.original.state }}",
          "employer": "={{ $json.original.employer }}",
          "violation_type": "={{ $json.violation_type }}",
          "complaint_text": "={{ $json.complaint_formal }}",
          "legal_basis": "={{ $json.legal_basis }}",
          "relief_sought": "={{ $json.relief_sought }}",
          "wages_stolen": "={{ $json.original.wages_stolen }}",
          "anonymous": "={{ $json.original.anonymous }}"
        }
      }
    },
    {
      "id": "6",
      "name": "Authority Router \u2014 Select Portal",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        1250,
        400
      ],
      "parameters": {
        "mode": "expression",
        "output": "={{ ['Wage Theft','PF/ESI Violation'].includes($json.violation_type) ? 0 : $json.violation_type === 'Gig Platform Abuse' ? 1 : $json.violation_type === 'Wrongful Termination' ? 2 : 3 }}"
      }
    },
    {
      "id": "7",
      "name": "Submit \u2014 Shram Suvidha (Wage / PF)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1500,
        250
      ],
      "parameters": {
        "method": "POST",
        "url": "https://shramsuvidha.gov.in/api/complaint",
        "continueOnFail": true,
        "notes": "Real API endpoint \u2014 configure auth headers per portal docs"
      }
    },
    {
      "id": "8",
      "name": "Submit \u2014 DPIIT (Gig Platform)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1500,
        350
      ],
      "parameters": {
        "method": "POST",
        "url": "https://dpiit.gov.in/grievance/api",
        "continueOnFail": true
      }
    },
    {
      "id": "9",
      "name": "Submit \u2014 Labour Court (Termination)",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [
        1500,
        450
      ],
      "parameters": {
        "fromEmail": "={{ $env.GMAIL_USER }}",
        "toEmail": "labourcourt@kerala.gov.in",
        "subject": "Formal Complaint \u2014 Wrongful Termination \u2014 Case {{ $json.original.case_id }}",
        "emailType": "html",
        "message": "=<p>Please find attached a formal complaint for wrongful termination.</p><p>Case ID: {{ $json.original.case_id }}</p>"
      }
    },
    {
      "id": "10",
      "name": "Submit \u2014 State Labour Dept (Other)",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [
        1500,
        550
      ],
      "parameters": {
        "fromEmail": "={{ $env.GMAIL_USER }}",
        "toEmail": "labour@{{ $json.original.state }}.gov.in",
        "subject": "WorkerVoice AI \u2014 Complaint \u2014 Case {{ $json.original.case_id }}",
        "emailType": "html",
        "message": "=Formal complaint attached. Case: {{ $json.original.case_id }}"
      }
    },
    {
      "id": "11",
      "name": "NGO Alert \u2014 Gmail SMTP",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [
        1750,
        300
      ],
      "parameters": {
        "fromEmail": "={{ $env.GMAIL_USER }}",
        "toEmail": "alerts@workervoice-ngos.in",
        "subject": "\ud83d\udea8 New {{ $json.violation_type }} complaint \u2014 {{ $json.original.state }} \u2014 Case {{ $json.original.case_id }}",
        "emailType": "html",
        "message": "=<h2>WorkerVoice AI \u2014 NGO Alert</h2><p><strong>Case ID:</strong> {{ $json.original.case_id }}</p><p><strong>Violation:</strong> {{ $json.violation_type }}</p><p><strong>State:</strong> {{ $json.original.state }}</p><p><strong>Employer:</strong> {{ $json.original.employer }}</p><p><strong>Severity:</strong> {{ $json.severity }}</p><p>Please follow up with the worker if possible.</p>"
      }
    },
    {
      "id": "12",
      "name": "Update DB Status \u2014 Submitted",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        2000,
        300
      ],
      "parameters": {
        "method": "PATCH",
        "url": "http://backend:8000/api/complaint/{{ $json.original.case_id }}/status",
        "sendBody": true,
        "contentType": "json",
        "body": {
          "status": "submitted_to_authority",
          "step": "Submitted to {{ $json.authority_portal }}",
          "detail": "Routed to {{ $json.authority }} \u00b7 Violation: {{ $json.violation_type }}"
        }
      }
    },
    {
      "id": "13",
      "name": "Check Collective Trigger",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2000,
        450
      ],
      "parameters": {
        "jsCode": "// Check SQLite via backend API for collective complaint trigger\n// This is called after every submission to detect patterns\nconst data = $input.item.json;\nconst employer = data.original?.employer;\nconst violation = data.violation_type;\nreturn [{ json: { employer, violation, check_collective: !!(employer && violation) } }];"
      }
    },
    {
      "id": "14",
      "name": "Respond to FastAPI",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        2250,
        300
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"status\": \"processed\", \"case_id\": \"{{ $('Parse Analysis JSON').item.json.original.case_id }}\", \"violation\": \"{{ $('Parse Analysis JSON').item.json.violation_type }}\", \"authority\": \"{{ $('Parse Analysis JSON').item.json.authority_portal }}\" }"
      }
    }
  ],
  "connections": {
    "Webhook \u2014 Complaint Intake": {
      "main": [
        [
          {
            "node": "LibreTranslate \u2014 Normalise to English",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "LibreTranslate \u2014 Normalise to English": {
      "main": [
        [
          {
            "node": "Groq LLaMA \u2014 Legal Analysis",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Groq LLaMA \u2014 Legal Analysis": {
      "main": [
        [
          {
            "node": "Parse Analysis JSON",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Analysis JSON": {
      "main": [
        [
          {
            "node": "Generate PDF \u2014 ReportLab",
            "type": "main",
            "index": 0
          },
          {
            "node": "Authority Router \u2014 Select Portal",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Authority Router \u2014 Select Portal": {
      "main": [
        [
          {
            "node": "Submit \u2014 Shram Suvidha (Wage / PF)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Submit \u2014 DPIIT (Gig Platform)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Submit \u2014 Labour Court (Termination)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Submit \u2014 State Labour Dept (Other)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Submit \u2014 Shram Suvidha (Wage / PF)": {
      "main": [
        [
          {
            "node": "NGO Alert \u2014 Gmail SMTP",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Submit \u2014 DPIIT (Gig Platform)": {
      "main": [
        [
          {
            "node": "NGO Alert \u2014 Gmail SMTP",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Submit \u2014 Labour Court (Termination)": {
      "main": [
        [
          {
            "node": "NGO Alert \u2014 Gmail SMTP",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Submit \u2014 State Labour Dept (Other)": {
      "main": [
        [
          {
            "node": "NGO Alert \u2014 Gmail SMTP",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "NGO Alert \u2014 Gmail SMTP": {
      "main": [
        [
          {
            "node": "Update DB Status \u2014 Submitted",
            "type": "main",
            "index": 0
          },
          {
            "node": "Check Collective Trigger",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update DB Status \u2014 Submitted": {
      "main": [
        [
          {
            "node": "Respond to FastAPI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}