/*
|--------------------------------------------------------------------------
| Agência PSI — Edge Function: twilio-whatsapp-webhook
|--------------------------------------------------------------------------
| Recebe callbacks de status do Twilio para mensagens WhatsApp enviadas
| pelas subcontas de cada tenant.
|
| URL configurada no número de cada subconta:
|   https://<project>.supabase.co/functions/v1/twilio-whatsapp-webhook?tenant_id=<uuid>
|
| Eventos recebidos (MessageStatus):
|   queued, failed, sent, delivered, undelivered, read
|--------------------------------------------------------------------------
*/

import { adminClient, tenantDbForId } from '../_shared/tenant.ts'

const corsHeaders = {
  'Access-Control-Allow-Origin':  '*',
  'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
  'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
}

// Mapeia status Twilio → status interno
function mapStatus(twilioStatus: string): string | null {
  switch (twilioStatus) {
    case 'delivered': return 'delivered'
    case 'read':      return 'read'
    case 'failed':
    case 'undelivered': return 'failed'
    case 'sent':      return 'sent'
    default:          return null
  }
}

Deno.serve(async (req: Request) => {
  if (req.method === 'OPTIONS') return new Response('ok', { headers: corsHeaders })

  // Twilio envia POST com form-encoded
  if (req.method !== 'POST') {
    return new Response('ok', { status: 200, headers: corsHeaders })
  }

  try {
    const admin = adminClient()

    const url      = new URL(req.url)
    const tenantId = url.searchParams.get('tenant_id')

    const formData = await req.formData()
    const messageSid    = formData.get('MessageSid')    as string
    const messageStatus = formData.get('MessageStatus') as string
    const to            = formData.get('To')            as string
    const errorCode     = formData.get('ErrorCode')     as string | null
    const errorMessage  = formData.get('ErrorMessage')  as string | null

    if (!messageSid || !messageStatus) {
      return new Response('ok', { status: 200, headers: corsHeaders })
    }

    const internalStatus = mapStatus(messageStatus)
    if (!internalStatus) {
      // Status intermediário (queued, accepted, etc.) — ignora
      return new Response('ok', { status: 200, headers: corsHeaders })
    }

    // Atualiza notification_logs pelo provider_message_id
    const updateData: Record<string, unknown> = {
      provider_status: messageStatus,
      status:          internalStatus,
    }

    if (internalStatus === 'delivered') {
      updateData.delivered_at = new Date().toISOString()
    } else if (internalStatus === 'read') {
      updateData.read_at      = new Date().toISOString()
      updateData.delivered_at = new Date().toISOString()
    } else if (internalStatus === 'failed') {
      updateData.failed_at      = new Date().toISOString()
      updateData.failure_reason = errorMessage
        ? `${errorCode}: ${errorMessage}`
        : `Twilio status: ${messageStatus}`
    }

    // notification_logs vive no schema do tenant — precisamos do tenant_id da URL
    // pra resolver o schema. Sem ele não há como localizar o log.
    if (!tenantId) {
      console.warn(`[webhook] ${messageSid} → ${messageStatus} sem tenant_id na URL; não atualiza log`)
    } else {
      try {
        const tdb = await tenantDbForId(admin, tenantId)
        const { error } = await tdb
          .from('notification_logs')
          .update(updateData)
          .eq('provider_message_id', messageSid)

        if (error) {
          console.error('[webhook] Erro ao atualizar log:', error.message)
        } else {
          console.log(`[webhook] ${messageSid} → ${messageStatus} (tenant: ${tenantId})`)
        }
      } catch (e) {
        console.error('[webhook] schema indisponível pra tenant', tenantId, ':', (e as Error).message)
      }
    }

    // Twilio espera 200 TwiML vazio ou texto simples
    return new Response('<?xml version="1.0" encoding="UTF-8"?><Response></Response>', {
      status:  200,
      headers: { ...corsHeaders, 'Content-Type': 'text/xml' },
    })
  } catch (e) {
    console.error('[webhook] Erro:', e)
    return new Response('ok', { status: 200, headers: corsHeaders })
  }
})
