-- Latest WhatsApp campaign logs with error/provider payload details
SELECT
    cl.id,
    cl.campaign_id,
    c.title AS campaign_title,
    cl.channel,
    cl.status,
    s.full_name,
    s.wa_phone_e164,
    s.wa_opt_in,
    cl.provider_message_id,
    cl.error_code,
    cl.error_message,
    cl.provider_payload,
    cl.created_at,
    cl.updated_at
FROM campaign_logs cl
LEFT JOIN campaigns c ON c.id = cl.campaign_id
LEFT JOIN subscribers s ON s.id = cl.subscriber_id
WHERE cl.channel = 'whatsapp'
ORDER BY cl.id DESC
LIMIT 50;

-- Campaigns that still need queue processing
SELECT id, title, status, total_targets, created_at, updated_at, sent_at
FROM campaigns
WHERE status IN ('queued', 'sending', 'failed')
ORDER BY id DESC
LIMIT 50;

-- Queue health
SELECT COUNT(*) AS pending_jobs FROM jobs;
SELECT id, queue, failed_at, exception FROM failed_jobs ORDER BY failed_at DESC LIMIT 10;
