{
  "name": "Snack Voice Commander - Prise Commande IA",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "commande-snack",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Webhook Commande",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const data = $input.first().json;\nconst menuPrix = {\n  \"burger\": 8, \"cheeseburger\": 9, \"double burger\": 12,\n  \"frites\": 4, \"grandes frites\": 5, \"nuggets\": 6,\n  \"wrap\": 7, \"salade\": 8, \"coca\": 3, \"fanta\": 3,\n  \"eau\": 2, \"ice tea\": 3, \"sundae\": 4, \"milkshake\": 5,\n  \"tacos\": 9, \"kebab\": 8, \"panini\": 6\n};\nlet prixTotal = 0;\nconst commandeTexte = data.commande.toLowerCase();\nfor (const [item, prix] of Object.entries(menuPrix)) {\n  const regex = new RegExp(`(\\\\d+)?\\\\s*${item}s?`, 'gi');\n  const matches = commandeTexte.match(regex);\n  if (matches) {\n    matches.forEach(match => {\n      const qtyMatch = match.match(/\\d+/);\n      const qty = qtyMatch ? parseInt(qtyMatch[0]) : 1;\n      prixTotal += prix * qty;\n    });\n  }\n}\nif (prixTotal === 0) prixTotal = 15;\nlet delaiMinutes = 30 + Math.floor(prixTotal / 20) * 5;\nconst codeCommande = 'CMD-' + Date.now().toString(36).toUpperCase();\nconst now = new Date();\nreturn {\n  json: {\n    date: now.toLocaleDateString('fr-FR'),\n    heure: now.toLocaleTimeString('fr-FR', {hour: '2-digit', minute: '2-digit'}),\n    nom_client: data.nom_client || 'Client',\n    telephone: data.telephone,\n    adresse: data.adresse,\n    commande: data.commande,\n    prix: prixTotal.toFixed(2),\n    delai: delaiMinutes,\n    code_commande: codeCommande,\n    statut: 'En preparation'\n  }\n};"
      },
      "id": "calcul-prix-delai",
      "name": "Calcul Prix et Delai",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        470,
        300
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "Commandes"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Date": "={{ $json.date }}",
            "Heure": "={{ $json.heure }}",
            "Nom": "={{ $json.nom_client }}",
            "Telephone": "={{ $json.telephone }}",
            "Adresse": "={{ $json.adresse }}",
            "Commande": "={{ $json.commande }}",
            "Prix": "={{ $json.prix }}",
            "Delai": "={{ $json.delai }}",
            "Code": "={{ $json.code_commande }}",
            "Statut": "={{ $json.statut }}"
          }
        },
        "options": {}
      },
      "id": "google-sheets-commandes",
      "name": "Google Sheets - Enregistrer Commande",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.4,
      "position": [
        690,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "Cuisine"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Heure": "={{ $json.heure }}",
            "Code": "={{ $json.code_commande }}",
            "Commande": "={{ $json.commande }}",
            "Nom": "={{ $json.nom_client }}",
            "Adresse": "={{ $json.adresse }}",
            "Statut": "A PREPARER"
          }
        },
        "options": {}
      },
      "id": "google-sheets-cuisine",
      "name": "Google Sheets - Bon Cuisine",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.4,
      "position": [
        910,
        200
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "from": "SNACK",
        "to": "={{ $json.telephone }}",
        "message": "=Bonjour {{ $json.nom_client }} !\n\nVotre commande est confirmee :\n{{ $json.commande }}\n\nPrix : {{ $json.prix }} EUR\nLivraison estimee : {{ $json.delai }} minutes\nCode commande : {{ $json.code_commande }}\n\nMerci de votre confiance !"
      },
      "id": "sms-client",
      "name": "SMS Client - Confirmation",
      "type": "n8n-nodes-base.vonage",
      "typeVersion": 1,
      "position": [
        910,
        300
      ],
      "credentials": {
        "vonageApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "from": "SNACK",
        "to": "+33XXXXXXXXX",
        "message": "=NOUVELLE COMMANDE !\n\nCode : {{ $json.code_commande }}\nClient : {{ $json.nom_client }}\nTel : {{ $json.telephone }}\nAdresse : {{ $json.adresse }}\n\nCommande : {{ $json.commande }}\n\nPrix : {{ $json.prix }} EUR\nDelai annonce : {{ $json.delai }} min\nHeure : {{ $json.heure }}"
      },
      "id": "sms-restaurateur",
      "name": "SMS Restaurateur - Notification",
      "type": "n8n-nodes-base.vonage",
      "typeVersion": 1,
      "position": [
        910,
        400
      ],
      "credentials": {
        "vonageApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"success\": true, \"message\": \"Commande enregistree\", \"code_commande\": \"{{ $json.code_commande }}\", \"prix\": \"{{ $json.prix }}\", \"delai\": \"{{ $json.delai }}\" }"
      },
      "id": "response-webhook",
      "name": "Reponse Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1130,
        300
      ]
    }
  ],
  "connections": {
    "Webhook Commande": {
      "main": [
        [
          {
            "node": "Calcul Prix et Delai",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calcul Prix et Delai": {
      "main": [
        [
          {
            "node": "Google Sheets - Enregistrer Commande",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets - Enregistrer Commande": {
      "main": [
        [
          {
            "node": "Google Sheets - Bon Cuisine",
            "type": "main",
            "index": 0
          },
          {
            "node": "SMS Client - Confirmation",
            "type": "main",
            "index": 0
          },
          {
            "node": "SMS Restaurateur - Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SMS Client - Confirmation": {
      "main": [
        [
          {
            "node": "Reponse Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 1
}