{
  "name": "Kings & Queens — Recordatorio WhatsApp",
  "nodes": [
    {
      "id": "trigger-cron",
      "name": "Cron 20:00 Argentina",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [200, 300],
      "typeVersion": 1.2,
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 23 * * *"
            }
          ]
        }
      }
    },
    {
      "id": "pb-auth",
      "name": "PocketBase — Auth Superuser",
      "type": "n8n-nodes-base.httpRequest",
      "position": [420, 300],
      "typeVersion": 4.2,
      "parameters": {
        "method": "POST",
        "url": "https://kq-db.edd-os.com.ar/api/collections/_superusers/auth-with-password",
        "sendBody": true,
        "contentType": "json",
        "body": {
          "identity": "={{ $vars.KQ_PB_ADMIN_EMAIL }}",
          "password": "={{ $vars.KQ_PB_ADMIN_PASSWORD }}"
        },
        "options": {}
      }
    },
    {
      "id": "get-tomorrow-classes",
      "name": "PocketBase — Clases de mañana",
      "type": "n8n-nodes-base.httpRequest",
      "position": [640, 300],
      "typeVersion": 4.2,
      "parameters": {
        "method": "GET",
        "url": "https://kq-db.edd-os.com.ar/api/collections/classes/records",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{ $node['PocketBase — Auth Superuser'].json.token }}"
            }
          ]
        },
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "filter",
              "value": "=(date='{{ $now.plus({days:1}).toFormat('yyyy-MM-dd') }}'&&status='scheduled')"
            },
            {
              "name": "expand",
              "value": "student_id"
            },
            {
              "name": "perPage",
              "value": "200"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "split-classes",
      "name": "Para cada clase",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [860, 300],
      "typeVersion": 3,
      "parameters": {
        "batchSize": 1,
        "options": {}
      }
    },
    {
      "id": "get-profile",
      "name": "PocketBase — Perfil del alumno",
      "type": "n8n-nodes-base.httpRequest",
      "position": [1080, 300],
      "typeVersion": 4.2,
      "parameters": {
        "method": "GET",
        "url": "https://kq-db.edd-os.com.ar/api/collections/student_profiles/records",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{ $node['PocketBase — Auth Superuser'].json.token }}"
            }
          ]
        },
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "filter",
              "value": "=(user_id='{{ $json.student_id }}')"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "check-phone",
      "name": "¿Tiene teléfono?",
      "type": "n8n-nodes-base.if",
      "position": [1300, 300],
      "typeVersion": 2,
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": false },
          "conditions": [
            {
              "id": "has-phone",
              "leftValue": "={{ $json.items[0]?.phone }}",
              "rightValue": "",
              "operator": { "type": "string", "operation": "notEmpty" }
            }
          ]
        }
      }
    },
    {
      "id": "send-whatsapp",
      "name": "WhatsApp — Enviar recordatorio",
      "type": "n8n-nodes-base.httpRequest",
      "position": [1520, 200],
      "typeVersion": 4.2,
      "parameters": {
        "method": "POST",
        "url": "=https://graph.facebook.com/v18.0/{{ $vars.KQ_WHATSAPP_PHONE_ID }}/messages",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $vars.KQ_WHATSAPP_TOKEN }}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "body": {
          "messaging_product": "whatsapp",
          "to": "=549{{ $node['PocketBase — Perfil del alumno'].json.items[0].phone.replace(/\\D/g,'') }}",
          "type": "template",
          "template": {
            "name": "class_reminder",
            "language": { "code": "es_AR" },
            "components": [
              {
                "type": "body",
                "parameters": [
                  {
                    "type": "text",
                    "text": "={{ $node['Para cada clase'].json.expand?.student_id?.name || 'Alumno' }}"
                  },
                  {
                    "type": "text",
                    "text": "={{ $now.plus({days:1}).setLocale('es').toFormat('EEEE d \\\"de\\\" MMMM') }}"
                  },
                  {
                    "type": "text",
                    "text": "={{ $node['Para cada clase'].json.time_start }}"
                  }
                ]
              }
            ]
          }
        },
        "options": {}
      }
    },
    {
      "id": "mark-sent",
      "name": "PocketBase — Marcar recordatorio enviado",
      "type": "n8n-nodes-base.httpRequest",
      "position": [1740, 200],
      "typeVersion": 4.2,
      "parameters": {
        "method": "PATCH",
        "url": "=https://kq-db.edd-os.com.ar/api/collections/classes/records/{{ $node['Para cada clase'].json.id }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{ $node['PocketBase — Auth Superuser'].json.token }}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "body": {
          "reminder_sent": true
        },
        "options": {}
      }
    }
  ],
  "connections": {
    "Cron 20:00 Argentina": {
      "main": [[ { "node": "PocketBase — Auth Superuser", "type": "main", "index": 0 } ]]
    },
    "PocketBase — Auth Superuser": {
      "main": [[ { "node": "PocketBase — Clases de mañana", "type": "main", "index": 0 } ]]
    },
    "PocketBase — Clases de mañana": {
      "main": [[ { "node": "Para cada clase", "type": "main", "index": 0 } ]]
    },
    "Para cada clase": {
      "main": [[ { "node": "PocketBase — Perfil del alumno", "type": "main", "index": 0 } ]]
    },
    "PocketBase — Perfil del alumno": {
      "main": [[ { "node": "¿Tiene teléfono?", "type": "main", "index": 0 } ]]
    },
    "¿Tiene teléfono?": {
      "main": [
        [ { "node": "WhatsApp — Enviar recordatorio", "type": "main", "index": 0 } ],
        []
      ]
    },
    "WhatsApp — Enviar recordatorio": {
      "main": [[ { "node": "PocketBase — Marcar recordatorio enviado", "type": "main", "index": 0 } ]]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "notes": "Envía recordatorio de WhatsApp a cada alumno con clase programada para mañana. Requiere variables de entorno en n8n: KQ_PB_ADMIN_EMAIL, KQ_PB_ADMIN_PASSWORD, KQ_WHATSAPP_PHONE_ID, KQ_WHATSAPP_TOKEN. El campo reminder_sent debe existir en la colección classes."
}
