{
    "name": "FoodSnap - Daily Report (Cron)",
    "nodes": [
        {
            "parameters": {
                "rule": {
                    "interval": [
                        {
                            "field": "cronExpression",
                            "expression": "0 20 * * *"
                        }
                    ]
                }
            },
            "type": "n8n-nodes-base.schedule",
            "typeVersion": 1.1,
            "position": [
                -300,
                -740
            ],
            "id": "schedule-trigger",
            "name": "Every Day 20h"
        },
        {
            "parameters": {
                "operation": "executeQuery",
                "query": "SELECT \n  p.id as user_id,\n  p.phone_e164\nFROM public.profiles p\nJOIN public.food_analyses f ON f.user_id = p.id\nWHERE f.created_at >= CURRENT_DATE\nGROUP BY p.id, p.phone_e164;",
                "options": {}
            },
            "type": "n8n-nodes-base.postgres",
            "typeVersion": 2.6,
            "position": [
                -80,
                -740
            ],
            "id": "get-active-users",
            "name": "Get Users Active Today",
            "credentials": {
                "postgres": {
                    "id": "2JDD2OJz4cAsWb0J",
                    "name": "foodsnap_supabase"
                }
            }
        },
        {
            "parameters": {
                "batchSize": 1,
                "options": {}
            },
            "type": "n8n-nodes-base.splitInBatches",
            "typeVersion": 3,
            "position": [
                140,
                -740
            ],
            "id": "split-users",
            "name": "Split Users"
        },
        {
            "parameters": {
                "operation": "executeQuery",
                "query": "SELECT \n  SUM(total_calories) as total_cals, \n  SUM(total_protein) as total_prot,\n  SUM(total_carbs) as total_carbs, \n  SUM(total_fat) as total_fat,\n  COUNT(*) as meal_count,\n  AVG(nutrition_score)::numeric(10,1) as avg_score\nFROM public.food_analyses \nWHERE user_id = '{{ $json.user_id }}'::uuid \nAND created_at >= CURRENT_DATE;",
                "options": {}
            },
            "type": "n8n-nodes-base.postgres",
            "typeVersion": 2.6,
            "position": [
                360,
                -740
            ],
            "id": "get-daily-stats",
            "name": "Get Daily Stats",
            "credentials": {
                "postgres": {
                    "id": "2JDD2OJz4cAsWb0J",
                    "name": "foodsnap_supabase"
                }
            }
        },
        {
            "parameters": {
                "modelId": {
                    "__rl": true,
                    "value": "models/gemini-pro",
                    "mode": "list",
                    "cachedResultName": "models/gemini-pro"
                },
                "promptType": "define",
                "text": "=Você é um Nutricionista IA do FoodSnap.\n\nDados do Usuário Hoje:\n- Refeições: {{ $json.meal_count }}\n- Calorias Totais: {{ $json.total_cals }} kcal\n- Proteínas: {{ $json.total_prot }}g\n- Carbos: {{ $json.total_carbs }}g\n- Gorduras: {{ $json.total_fat }}g\n- Score Médio (0-100): {{ $json.avg_score }}\n\nCrie uma mensagem curta (máx 3 linhas) para o WhatsApp.\n1. Elogie se bateu meta (assuma 2000kcal base se não tiver dado).\n2. Dê uma dica rápida para amanhã baseada nos macros.\n3. Termine motivacional.\n4. Use emojis.\n5. Não use markdown bold (*) excessivamente, só em palavras chave.\n\nExemplo:\n\"Olá! Hoje você mandou bem nas proteínas (120g)! 💪 Amanhã tente reduzir um pouco a gordura no jantar. Continue assim! 🚀\"",
                "options": {}
            },
            "type": "@n8n/n8n-nodes-langchain.googleGemini",
            "typeVersion": 1,
            "position": [
                580,
                -740
            ],
            "id": "generate-insight",
            "name": "Generate Insight",
            "credentials": {
                "googlePalmApi": {
                    "id": "T2uIVBcjJ9h8BFCC",
                    "name": "Backup APIKEY"
                }
            }
        },
        {
            "parameters": {
                "resource": "messages-api",
                "instanceName": "FoodSnap",
                "remoteJid": "={{ $('Split Users').item.json.phone_e164.replace('+', '') }}",
                "messageText": "=📊 *FoodSnap Diário*\n\nHoje você registrou {{ $('Get Daily Stats').item.json.meal_count }} refeições.\n🔥 *{{ $('Get Daily Stats').item.json.total_cals }} kcal* | 🥩 {{ $('Get Daily Stats').item.json.total_prot }}g Prot\n\n{{ $json.text }}\n\n_Até amanhã!_",
                "options_message": {}
            },
            "type": "n8n-nodes-evolution-api.evolutionApi",
            "typeVersion": 1,
            "position": [
                800,
                -740
            ],
            "id": "send-whatsapp",
            "name": "Enviar WhatsApp",
            "credentials": {
                "evolutionApi": {
                    "id": "nGWBERcZoQWgdxgk",
                    "name": "FoodSnap"
                }
            }
        }
    ],
    "connections": {
        "Every Day 20h": {
            "main": [
                [
                    {
                        "node": "Get Users Active Today",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Get Users Active Today": {
            "main": [
                [
                    {
                        "node": "Split Users",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Split Users": {
            "main": [
                [
                    {
                        "node": "Get Daily Stats",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Get Daily Stats": {
            "main": [
                [
                    {
                        "node": "Generate Insight",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Generate Insight": {
            "main": [
                [
                    {
                        "node": "Enviar WhatsApp",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Enviar WhatsApp": {
            "main": [
                [
                    {
                        "node": "Split Users",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        }
    }
}