// T2 Falcon Admin — Basic Send Application (BSA): seed data
// Marketplace & Applications .Mng → Basic Application.
// WhatsApp + Voice transactions for the Outbox / Scheduled grids, plus the
// sender IDs / templates / contact groups used by the compose wizard.

// ---- Sender IDs (verified numbers attached to the tenant) ----
const bsaWaSenders = ['+966 57 283 8628', '+962 79 655 0500', '+966 56 174 2284', '+962 77 680 4143', '+966 53 384 4111'];
const bsaVoiceSenders = ['+966 11 234 5678', '+966 11 234 5679', '+966 50 998 2200'];

// ---- WhatsApp templates (3-tier: type → language → name) ----
const bsaWaTemplates = [
  { id: 'wt1', name: 'Chat & Discover',     type: 'Marketing',      language: 'English', refId: 'REF-1001' },
  { id: 'wt2', name: 'Talk To Us Today',    type: 'Marketing',      language: 'English', refId: 'REF-1002' },
  { id: 'wt3', name: 'Meet The New Falcon', type: 'Authentication', language: 'English', refId: 'REF-1003' },
  { id: 'wt4', name: 'Chat With Care',      type: 'Utility',        language: 'English', refId: 'REF-1004' },
  { id: 'wt5', name: 'Launch & Chat',       type: 'Authentication', language: 'Arabic',  refId: 'REF-1005' },
];

// ---- Voice IVR templates (2-tier: category → name) ----
const bsaVoiceTemplates = [
  { id: 'vt1', name: 'Riyad Transactions', type: 'Dynamic', refId: 'REF-2001' },
  { id: 'vt2', name: 'Welcome Greeting',   type: 'Static',  refId: 'REF-2002' },
  { id: 'vt3', name: 'Appointment Confirm', type: 'Dynamic', refId: 'REF-2003' },
];

// ---- Contact groups the user can target ----
const bsaContactGroups = [
  { id: 'cg1', name: 'Contact Group 1', count: 257, columns: ['mobile', 'first_name', 'last_name', 'age', 'gender'] },
  { id: 'cg2', name: 'VIP Customers',   count: 96,  columns: ['phone', 'name', 'tier'] },
  { id: 'cg3', name: 'New Leads',       count: 120, columns: ['mobile', 'first_name', 'city'] },
  { id: 'cg4', name: 'Partner Leads',       count: 340, columns: ['mobile', 'first_name', 'company'], shared: true },
  { id: 'cg5', name: 'Regional Team List',  count: 512, columns: ['phone', 'name', 'region'],        shared: true },
];

// Build a transaction record (keeps the seed terse + consistent).
const bsaTxn = (id, sender, tplId, count, cost, created, status, extra) => {
  const tpl = [...bsaWaTemplates, ...bsaVoiceTemplates].find((x) => x.id === tplId) || {};
  return {
    id, sender, templateId: tplId,
    templateName: tpl.name, language: tpl.language || null, type: tpl.type,
    createdAt: created, recipientsCount: count, totalCost: cost,
    recipients: 'Contact Group 1', recipientsList: ['Contact Group 1'],
    status, ...(extra || {}),
  };
};

// ===== WhatsApp — Outbox (execution time satisfied) =====
const seedBsaWaOutbox = [
  bsaTxn('TXN-100483', '+966 57 283 8628', 'wt1', 473, 1183, '27-Mar-2025 · 03:05 pm', 'completed', { recipientsList: ['Contact Group 1', 'VIP Customers', 'New Leads'] }),
  bsaTxn('TXN-100484', '+966 57 283 8628', 'wt1', 259, 648,  '27-Mar-2025 · 03:08 pm', 'completed', { manualRecipients: ['+966 57 283 8628', '+966 50 998 2210'] }),
  bsaTxn('TXN-100485', '+962 79 655 0500', 'wt4', 3,   8,    '27-Mar-2025 · 03:12 pm', 'completed', { recipients: '', recipientsList: [], manualRecipients: ['+966 55 123 4567', '+966 54 222 1180', '+966 53 700 4412'] }),
  bsaTxn('TXN-100482', '+966 57 283 8628', 'wt1', 96,  250, '27-Mar-2025 · 02:42 pm', 'completed'),
  bsaTxn('TXN-100481', '+962 79 655 0500', 'wt4', 120, 250, '27-Mar-2025 · 02:40 pm', 'completed'),
  bsaTxn('TXN-100480', '+966 57 283 8628', 'wt2', 257, 642, '27-Mar-2025 · 02:31 pm', 'in_progress', { recipientsCount: 142, totalCost: 355, targetCount: 257, targetCost: 642 }),
  bsaTxn('TXN-100479', '+962 79 655 0500', 'wt5', 98,  245, '26-Mar-2025 · 06:18 pm', 'completed'),
  bsaTxn('TXN-100478', '+966 56 174 2284', 'wt3', 120, 300, '26-Mar-2025 · 04:05 pm', 'partial', { recipientsCount: 84, totalCost: 210, plannedCount: 120, failReason: 'Insufficient balance — processed 84 of 120 recipients; the remaining 36 were not sent or charged.' }),
  bsaTxn('TXN-100477', '+966 56 174 2284', 'wt2', 257, 642, '26-Mar-2025 · 01:22 pm', 'completed'),
  bsaTxn('TXN-100476', '+962 77 680 4143', 'wt1', 120, 300, '25-Mar-2025 · 11:47 am', 'canceled', { recipientsCount: 47, totalCost: 118, plannedCount: 120, failReason: 'Canceled by user — processing stopped after 47 of 120 recipients; the remaining 73 were not sent or charged.' }),
  bsaTxn('TXN-100475', '+966 53 384 4111', 'wt4', 257, 642, '25-Mar-2025 · 10:10 am', 'completed'),
  bsaTxn('TXN-100474', '+966 57 283 8628', 'wt5', 64,  160, '24-Mar-2025 · 09:32 pm', 'failed', { recipientsCount: 0, totalCost: 0, plannedCount: 64, estimatedCost: 160, failReason: 'Aborted before processing — insufficient balance at execution time. No records were processed and nothing was charged.' }),
  bsaTxn('TXN-100473', '+962 79 655 0500', 'wt1', 312, 780, '24-Mar-2025 · 03:14 pm', 'completed'),
  bsaTxn('TXN-100472', '+966 56 174 2284', 'wt3', 45,  112, '23-Mar-2025 · 05:50 pm', 'completed'),
  bsaTxn('TXN-100471', '+966 53 384 4111', 'wt2', 188, 470, '23-Mar-2025 · 02:09 pm', 'completed'),
];

// ===== WhatsApp — Scheduled (due date not yet satisfied) =====
const seedBsaWaScheduled = [
  bsaTxn('TXN-100501', '+966 57 283 8628', 'wt1', 96,  240, '27-Mar-2025 · 03:10 pm', 'scheduled', { scheduledAt: '02-Jul-2026 · 09:00 am' }),
  bsaTxn('TXN-100502', '+962 79 655 0500', 'wt2', 257, 642, '27-Mar-2025 · 03:12 pm', 'scheduled', { scheduledAt: '05-Jul-2026 · 10:30 am' }),
  bsaTxn('TXN-100503', '+966 56 174 2284', 'wt4', 120, 300, '26-Mar-2025 · 06:40 pm', 'scheduled', { scheduledAt: '10-Jul-2026 · 08:00 am' }),
  bsaTxn('TXN-100504', '+966 53 384 4111', 'wt3', 64,  160, '25-Mar-2025 · 12:05 pm', 'deleted',   { scheduledAt: '12-Jul-2026 · 02:00 pm' }),
  bsaTxn('TXN-100505', '+962 77 680 4143', 'wt5', 188, 470, '24-Mar-2025 · 09:48 am', 'scheduled', { scheduledAt: '18-Jul-2026 · 11:15 am' }),
];

// ===== Voice — Outbox / Scheduled (IVR) =====
const seedBsaVoiceOutbox = [
  bsaTxn('TXN-200311', '+966 11 234 5678', 'vt1', 142, 568, '27-Mar-2025 · 01:20 pm', 'completed'),
  bsaTxn('TXN-200310', '+966 11 234 5679', 'vt2', 88,  264, '26-Mar-2025 · 05:02 pm', 'in_progress', { recipientsCount: 51, totalCost: 153, targetCount: 88, targetCost: 264 }),
  bsaTxn('TXN-200309', '+966 50 998 2200', 'vt3', 64,  192, '25-Mar-2025 · 03:33 pm', 'partial', { recipientsCount: 40, totalCost: 120, plannedCount: 64, failReason: 'Insufficient balance — call processing stopped after 40 of 64 recipients; the remaining 24 were not dialed or charged.' }),
  bsaTxn('TXN-200308', '+966 11 234 5678', 'vt1', 210, 840, '24-Mar-2025 · 10:15 am', 'completed'),
];
const seedBsaVoiceScheduled = [
  bsaTxn('TXN-200401', '+966 11 234 5678', 'vt1', 142, 568, '27-Mar-2025 · 01:25 pm', 'scheduled', { scheduledAt: '03-Jul-2026 · 09:30 am' }),
  bsaTxn('TXN-200402', '+966 50 998 2200', 'vt3', 64,  192, '26-Mar-2025 · 04:00 pm', 'scheduled', { scheduledAt: '08-Jul-2026 · 01:00 pm' }),
];

// ---- Template bodies (for the phone preview) + variables (for the mapping grid) ----
const bsaWaTemplateBodies = {
  wt1: { title: '👋 Hi {{first_name}}!', body: 'Have a question or need help? Just send us a message on WhatsApp — we’re here to assist you anytime. 💬', footer: 'WhatsApp Business Platform Sample Message', button: 'Chat with us' },
  wt2: { title: 'Welcome And Congratulations, {{first_name}}!!', body: 'This message demonstrates your ability to send a WhatsApp message notification. Thank you for taking the time to test with us.', footer: 'WhatsApp Business Platform Sample Message', button: 'Reply' },
  wt3: { title: 'Meet the new Falcon, {{first_name}}', body: 'Your verification code is *{{code}}*. It expires in 10 minutes.', footer: null, button: 'Copy code' },
  wt4: { title: 'Chat With Care', body: 'Hi {{first_name}}, your request *{{ticket}}* has been received. Our team will reach out shortly.', footer: null, button: null },
  wt5: { title: 'Launch & Chat', body: 'مرحباً {{first_name}}، رمز التحقق الخاص بك هو *{{code}}*.', footer: null, button: null },
};
const bsaTemplateVars = {
  wt1: ['first_name'], wt2: ['first_name'], wt3: ['first_name', 'code'],
  wt4: ['first_name', 'ticket'], wt5: ['first_name', 'code'],
  vt1: ['first_name', 'amount'], vt2: [], vt3: ['first_name', 'date'],
};

// ---- Sample conversation thread (Conversation page demo) ----
// Demonstrates every WhatsApp message type: text, emoji, image, voice note, document + reactions.
const bsaSampleConversation = [
  { id: 'm1', dir: 'out', org: 'Aramco-Marketing-Office Management', name: 'Jawad Lababneh',
    text: 'Welcome and congratulations! 🎉 This message demonstrates your ability to send a WhatsApp message notification. Thank you for taking the time to test with us.', time: '04:30 PM', status: 'read' },
  { id: 'm2', dir: 'in',
    text: 'Welcome And Congratulations!! This Message Demonstrates Your Ability To Send A WhatsApp Message Notification.', time: '02:41 PM' },
  { id: 'm3', dir: 'in', emoji: '🚀',
    text: 'Hello, I hope to receive the service.', time: '02:42 PM' },
  { id: 'm4', dir: 'out', org: 'Aramco-Marketing-Office Management', name: 'Jawad Lababneh',
    kind: 'image', caption: 'Here is your service brochure 📄', time: '04:35 PM', status: 'read', reaction: '❤️' },
  { id: 'm5', dir: 'in', kind: 'voice', dur: '0:14', time: '02:44 PM' },
  { id: 'm6', dir: 'out', org: 'Aramco-Marketing-Office Management', name: 'Jawad Lababneh',
    kind: 'doc', fileName: 'AI Credit Bureau on Block Project.pdf', fileMeta: '18 pages · PDF · 697 KB', time: '04:36 PM', status: 'read' },
  { id: 'm7', dir: 'in', text: 'Perfect 🙏 thank you!', time: '02:46 PM', reaction: '👍' },
];

// ---- IVR prompt transcripts (recording filename → spoken text) ----
// Lets the Conversation render a written transcript beneath each voice-note bubble,
// mirroring WhatsApp's voice-note transcription. {variable} segments are filled at render time.
const bsaIvrTranscripts = {
  'routine-notification.wav': 'This is a routine notification from your service provider. To repeat this message, press 1.',
  // Al Rajhi Bank (AR)
  'alrajhi-welcome-ar.wav': 'Welcome to Al Rajhi Bank. For account services press 1, for card services press 2, for transfers press 3, or to reach customer service press 0.',
  'accounts-menu-ar.wav': 'Account services. For your account balance press 1, for a mini statement press 2.',
  'balance-ar.wav': 'Your current account balance will now be read to you.',
  'statement-ar.wav': 'Your mini statement is being prepared and will be sent by SMS.',
  'cards-menu-ar.wav': 'Card services. To block or report a card press 1.',
  'block-card-ar.wav': 'To block or report a lost or stolen card, please stay on the line.',
  'transfers-ar.wav': 'For local and international transfers, please follow the instructions.',
  'agent-ar.wav': 'Please hold while we connect you to a customer service representative.',
  // STC Billing (dynamic, AR)
  'stc-bill-intro-ar.wav': 'Welcome to STC billing. Your current outstanding amount is',
  'stc-riyals-ar.wav': 'Saudi riyals.',
  'stc-due-on-ar.wav': 'Your payment is due on',
  'stc-pay-ar.wav': 'To pay now, we will transfer you to a payment specialist.',
  'stc-repeat-ar.wav': 'To repeat this message, press 9.',
  // Saudia
  'saudia-welcome.wav': 'Welcome to Saudia. For a new booking press 1, for flight status press 2, for baggage services press 3, for Alfursan miles press 4.',
  'saudia-booking.wav': 'New booking. We will connect you to a booking agent.',
  'saudia-status.wav': 'Flight status. Please have your booking reference ready.',
  'saudia-baggage.wav': 'Baggage services. For lost or delayed baggage, please follow the prompts.',
  'saudia-alfursan.wav': 'Alfursan. Your miles balance and membership tier will be provided.',
  // Absher (dynamic, AR)
  'absher-greeting-ar.wav': 'This is an appointment reminder from Absher for',
  'absher-appt-ar.wav': 'Your appointment is scheduled for',
  'absher-confirm-ar.wav': 'Your attendance has been confirmed. Thank you.',
  'absher-resched-ar.wav': 'To reschedule your appointment, we will connect you to an agent.',
  // Riyad Bank + card-dispute / balance flow (EN)
  'riyad-welcome.wav': 'Welcome to Riyad Bank.',
  'riyad-main-menu.wav': 'Main menu. For recent activity press 1, to dispute a transaction press 2, to activate a card press 3.',
  'recent-activity.wav': 'Recent activity. Your latest transactions will be summarised.',
  'last-txn-intro.wav': 'Your last transaction was',
  'last-txn-on.wav': 'on',
  'transactions-info.wav': 'Here is a summary of your recent transactions.',
  'txn-details.wav': 'Transaction details will now be provided.',
  'current-balance-intro.wav': 'Your current available balance is',
  'current-balance-outro.wav': 'Thank you.',
  'balance-menu.wav': 'Balance services. To hear your current balance press 1.',
  'accounts-menu.wav': 'Account services. Please choose from the following options.',
  'mini-statement.wav': 'Your mini statement is being prepared.',
  'cards-menu.wav': 'Card services. Please choose from the following options.',
  'block-card.wav': 'To block a card, please stay on the line.',
  'activate-intro.wav': 'To activate your new card, please follow the instructions.',
  'dispute-intro.wav': 'To dispute a transaction, please tell us which one.',
  'dispute-reason-menu.wav': 'Select a reason: for an unauthorized charge press 1, for a duplicate charge press 2.',
  'unauthorized-intro.wav': 'You selected an unauthorized charge.',
  'duplicate-charge.wav': 'You selected a duplicate charge.',
  'confirm-dispute.wav': 'Your dispute has been recorded. A specialist will review it.',
  'submit-intro.wav': 'We are submitting your request.',
  'submit-outro.wav': 'Your request has been submitted successfully.',
  'operator-connect.wav': 'Connecting you to an operator. Please hold.',
};

// ---- AI-agent handoff scripts (voice call → text-chat continuation) ----
// When an IVR call ends in a "transfer", the thread continues as a structured chat with
// the Falcon AI Assistant on the recipient's preferred channel (WhatsApp / Instagram).
// Keyed by intent (derived from the template). Each message is { from:'ai'|'cust', text }.
const bsaAiHandoff = {
  billing: { agent: 'Falcon AI Assistant', msgs: [
    { from: 'ai', text: 'Hi 👋 This is the Falcon AI Assistant, continuing from your STC call. I can help you settle your outstanding bill of SAR 248.50, due 15 Jun 2026. Would you like to pay now?' },
    { from: 'cust', text: 'Yes, please.' },
    { from: 'ai', text: 'Great. I can charge the card ending 4471 on file, or send you a secure payment link — which do you prefer?' },
    { from: 'cust', text: 'Send me the link.' },
    { from: 'ai', text: 'Done ✅ A secure STC payment link has been sent to this number. It expires in 30 minutes. Anything else I can help with?' },
    { from: 'cust', text: "No, that's all — thank you!" },
    { from: 'ai', text: 'You are welcome. Your reference is STC-948213. Have a great day 🌟' },
  ] },
  travel: { agent: 'Falcon AI Assistant', msgs: [
    { from: 'ai', text: 'Hello! 👋 Falcon AI Assistant here, continuing from your Saudia call about a new booking. Where would you like to fly, and on which dates?' },
    { from: 'cust', text: 'Riyadh to Jeddah, next Friday.' },
    { from: 'ai', text: 'I found 3 flights for Fri 4 Jul: SV1021 07:40, SV1037 13:15, SV1055 19:50 — economy from SAR 410. Which works best?' },
    { from: 'cust', text: 'The morning one, SV1021.' },
    { from: 'ai', text: 'Great choice ✈️ I have held seat 14C on SV1021 for 20 minutes. Shall I send a secure link to complete payment?' },
    { from: 'cust', text: 'Yes, go ahead.' },
    { from: 'ai', text: 'Done ✅ Booking reference SV-7K2P9. The payment link is on its way. Safe travels 🌍' },
  ] },
  appointment: { agent: 'Falcon AI Assistant', msgs: [
    { from: 'ai', text: 'Hi 👋 This is the Falcon AI Assistant, following up on your Absher reminder. Your appointment is on 20 Jun 2026 at 09:30. Would you like to reschedule?' },
    { from: 'cust', text: 'Yes, can we move it to the afternoon?' },
    { from: 'ai', text: 'Sure. Afternoon slots on 22 Jun: 13:00, 14:30, 16:00 — which would you like?' },
    { from: 'cust', text: '14:30 please.' },
    { from: 'ai', text: 'Booked ✅ Your appointment is now 22 Jun 2026 at 14:30. Reference APP-553120 — a confirmation has been sent. Anything else?' },
    { from: 'cust', text: 'No thanks 🙏' },
    { from: 'ai', text: 'You are all set. Have a great day!' },
  ] },
  banking: { agent: 'Falcon AI Assistant', msgs: [
    { from: 'ai', text: 'Hello 👋 Falcon AI Assistant here, continuing from your Al Rajhi Bank call. I can help with accounts, cards, or transfers — what do you need?' },
    { from: 'cust', text: 'I need to block my card, I lost it.' },
    { from: 'ai', text: 'I can do that right away. For your security I have temporarily frozen the card ending 8842. Shall I order a replacement to your registered address?' },
    { from: 'cust', text: 'Yes, please.' },
    { from: 'ai', text: 'Done ✅ A new card is on the way (3–5 days) and the old one is blocked. Reference RJ-204871. Stay safe!' },
    { from: 'cust', text: 'Thank you so much.' },
    { from: 'ai', text: 'Anytime. Is there anything else I can help you with?' },
  ] },
  support: { agent: 'Falcon AI Assistant', msgs: [
    { from: 'ai', text: 'Hi 👋 This is the Falcon AI Assistant, continuing your call. How can I help you today?' },
    { from: 'cust', text: 'I had a question about my request.' },
    { from: 'ai', text: 'Of course — I have pulled up your details. Everything is on track and you will receive an update shortly. Anything else I can do?' },
    { from: 'cust', text: 'No, thank you!' },
    { from: 'ai', text: 'Happy to help. Have a great day 🌟' },
  ] },
};

Object.assign(window, {
  bsaWaSenders, bsaVoiceSenders, bsaWaTemplates, bsaVoiceTemplates, bsaContactGroups,
  seedBsaWaOutbox, seedBsaWaScheduled, seedBsaVoiceOutbox, seedBsaVoiceScheduled,
  bsaWaTemplateBodies, bsaTemplateVars, bsaSampleConversation,
  bsaIvrTranscripts, bsaAiHandoff,
});
