{
  "task": "phase-j-chat-ads-integration",
  "project": "lnwAdsSJ88",
  "site": "https://adsfb.namnan.co.th",
  "date": "2026-07-12",
  "commit": "cee9512",
  "status": "PASS",
  "user_request": "เอาตรง https://adsfb.namnan.co.th/app.html#/chats เก็บลง Database ให้ละเอียด เตรียมเอามาทำ วิเคราะห์ อย่างละเอียด และ ถ้าเชื่อมโยง การยิงแอดได้ก็ต้องเชื่อม ออกแบบระบบมาอย่างละเอียด",

  "deliverables": {
    "tables_created": 4,
    "endpoints_created": 7,
    "ui_tabs_added": 1,
    "files_created": 3,
    "files_modified": 3,
    "nginx_blocks_added": 6,
    "screenshots": 4
  },

  "schema": {
    "chat_full_export": {
      "purpose": "Master denormalized table for analysis",
      "rows": 13,
      "key_fields": ["message_id (UNIQUE)", "conversation_id", "page_id", "page_name", "page_family", "campaign_id", "intent", "sender_id"],
      "indexes": ["conv", "page", "campaign", "sender", "date", "family", "intent"]
    },
    "ad_chat_link": {
      "purpose": "Many-to-many campaign ↔ conversation",
      "rows": 4,
      "key_fields": ["campaign_id", "conversation_id (UNIQUE)", "link_source", "link_confidence", "conversion_count"]
    },
    "chat_attribution_log": {
      "purpose": "Audit trail for attribution resolution",
      "rows": 0,
      "key_fields": ["conversation_id", "source", "campaign_id", "ad_id", "raw_payload"]
    },
    "chat_campaign_roi": {
      "purpose": "Materialized ROI per (campaign, page)",
      "rows": 1,
      "key_fields": ["campaign_id (PK)", "total_spend", "roas", "total_conversations", "cost_per_chat_thb", "chat_to_ad_conv_rate"]
    }
  },

  "endpoints": [
    "POST /admin/chat-ads-link/backfill",
    "GET /public/chat-campaign-roi",
    "GET /public/chat-campaign-detail",
    "GET /public/chat-intent-breakdown",
    "GET /public/chat-family-roi",
    "GET /public/chat-customer-ltv",
    "GET /public/chat-export-stats"
  ],

  "data_results": {
    "total_messages_exported": 13,
    "total_conversations": 8,
    "unique_senders": 8,
    "pages_with_chat": 4,
    "campaigns_with_chat": 1,
    "orphan_inbound": 7,
    "by_family": {
      "solar": 5,
      "farm": 4,
      "other": 2
    }
  },

  "solar_campaign_metrics": {
    "campaign_id": "120230799572200590",
    "campaign_name": "1 [buy]tsm ✅ไฟตุ้มโซล่าเซลล์ 250 วัตต์สว่างยันเช้า",
    "page": "เฮียหนวด โซล่าเซลล์ ไฟตุ้มโซล่าเซลล์ โคมไฟโซล่าเซลล์ สว่างยันเช้า",
    "ad_metrics": {
      "spend": 314.65,
      "revenue": 9294.00,
      "roas": 29.54,
      "cpa": 39.33,
      "impressions": 3175,
      "clicks": 369,
      "conversions": 8
    },
    "chat_metrics": {
      "conversations": 2,
      "messages": 5,
      "inbound_messages": 4,
      "outbound_messages": 1,
      "unique_senders": 3,
      "buy_intent_count": 1
    },
    "derived": {
      "buy_intent_rate": 0.25,
      "chat_to_ad_conv_rate": 0.125,
      "cost_per_chat_thb": 157.33
    }
  },

  "bugs_fixed": [
    "demo.ts: /ads-chats.html route was outside function scope → would crash on import",
    "index.ts: 4 critical route modules never registered (chatInbox, chatTemplates, chatHistory, campaignDetail)",
    "nginx: missing location blocks for /app.html, /campaign.html, etc. → 404 on all .html files"
  ],

  "verification": {
    "endpoints_tested": 7,
    "all_passed": true,
    "ui_tested": ["campaign.html overview", "campaign.html Ads ROI tab", "campaign.html Chats tab", "app.html Chats"],
    "screenshots": 4
  }
}
