// T2 Falcon Admin — Contracts & Cost Management seed data

// Statuses: 'active' | 'pending' | 'expired'

const seedContracts = {
  // Falcon view = all clients pooled. We key contracts to their owner-node id
  // (matches the tree node ids from data.jsx) so the right-hand list can be
  // scoped to the selected node when a row is clicked in the tree.
  aramco: [
    {
      id: 'C-20240915',
      farabiReferenceId: '1074829301',
      contractId: '20240915',
      name: 'Employee OTP & Two-Factor Authentication',
      creationDate: '12-Jan-2025',
      startDate: '01-Feb-2025',
      expirationDate: '31-Jan-2028',
      value: 8750000,
      remaining: 5238417.4250,
      status: 'active',
      currency: 'SAR',
      application: 'OTP Application',
      ownerNode: 'aramco',
    },
    {
      id: 'C-20230701',
      farabiReferenceId: '1074829318',
      contractId: '20230701',
      name: 'HSE Emergency Broadcast & Field Alerts',
      creationDate: '03-Mar-2025',
      startDate: '01-Apr-2025',
      expirationDate: '31-Mar-2027',
      value: 12400000,
      remaining: 4182650.9100,
      status: 'active',
      currency: 'SAR',
      application: 'Basic Send Transaction Application',
      ownerNode: 'aramco',
    },
    {
      id: 'C-20220120',
      farabiReferenceId: '1074829327',
      contractId: '20220120',
      name: 'Procurement Vendor Notifications',
      creationDate: '18-Nov-2024',
      startDate: '01-Dec-2024',
      expirationDate: '30-Nov-2026',
      value: 6300000,
      remaining: 6300000,
      status: 'active',
      currency: 'SAR',
      application: 'Two-Way Messaging Application',
      ownerNode: 'aramco',
    },
    {
      id: 'C-20251010',
      farabiReferenceId: '1074829334',
      contractId: '20251010',
      name: 'Shareholder & Investor Communications',
      creationDate: '22-Sep-2025',
      startDate: '01-Oct-2025',
      expirationDate: '30-Sep-2028',
      value: 18900000,
      remaining: 18900000,
      status: 'pending',
      currency: 'SAR',
      application: 'Marketing Application',
      ownerNode: 'aramco',
    },
    {
      id: 'C-20230805-a',
      farabiReferenceId: '1074829341',
      contractId: '20230805',
      name: 'HR Self-Service Notifications',
      creationDate: '05-Aug-2023',
      startDate: '01-Sep-2023',
      expirationDate: '31-Aug-2025',
      value: 3450000,
      remaining: null,
      status: 'expired',
      currency: 'SAR',
      application: 'Basic Send Transaction Application',
      ownerNode: 'aramco',
    },
    {
      id: 'C-20230805-b',
      farabiReferenceId: '1074829350',
      contractId: '20230811',
      name: 'IT Service Desk Alerts',
      creationDate: '14-Feb-2023',
      startDate: '01-Mar-2023',
      expirationDate: '28-Feb-2025',
      value: 2180000,
      remaining: null,
      status: 'expired',
      currency: 'SAR',
      application: 'Basic Send Transaction Application',
      ownerNode: 'aramco',
    },
    {
      id: 'C-20230805-c',
      farabiReferenceId: '1074829369',
      contractId: '20240620',
      name: 'Customer Care Voice OTP',
      creationDate: '02-Jun-2025',
      startDate: '20-Jun-2025',
      expirationDate: '19-Jun-2027',
      value: 5640000,
      remaining: 3920185.6075,
      status: 'active',
      currency: 'SAR',
      application: 'Voice OTP Application',
      ownerNode: 'aramco',
    },
  ],
  alrajhi: [
    {
      id: 'C-AR-001',
      farabiReferenceId: '1098341027',
      contractId: '20240301',
      name: 'Banking OTP & Transaction Alerts',
      creationDate: '15-Mar-2025',
      startDate: '01-Apr-2025',
      expirationDate: '31-Mar-2028',
      value: 21800000,
      remaining: 14276390.2150,
      status: 'active',
      currency: 'SAR',
      application: 'OTP Application',
      ownerNode: 'alrajhi',
    },
  ],
  snb: [
    {
      id: 'C-SNB-001',
      farabiReferenceId: '1086520934',
      contractId: '20240501',
      name: 'Card & Account Activity Notifications',
      creationDate: '10-Apr-2025',
      startDate: '01-May-2025',
      expirationDate: '30-Apr-2027',
      value: 16450000,
      remaining: 9817224.6800,
      status: 'active',
      currency: 'SAR',
      application: 'Basic Send Transaction Application',
      ownerNode: 'snb',
    },
  ],
  bupa: [
    {
      id: 'C-BU-001',
      farabiReferenceId: '1071903845',
      contractId: '20240210',
      name: 'Claims & Policy Renewal Reminders',
      creationDate: '12-Jan-2025',
      startDate: '01-Feb-2025',
      expirationDate: '31-Jan-2026',
      value: 4720000,
      remaining: 4720000,
      status: 'pending',
      currency: 'SAR',
      application: 'Marketing Application',
      ownerNode: 'bupa',
    },
  ],
};

// Rate card (per contract — same structure shared in this mock).
const seedRateCard = [
  { name: 'WhatsApp',   unit: 'One KSA Transaction', value: '0.2100 SAR' },
  { name: 'Voice',      unit: 'One KSA Second',      value: '0.0450 SAR' },
  { name: 'AI-ChatGPT', unit: 'One API Call',        value: '0.0850 SAR' },
  { name: 'SMS',        unit: 'Per Message',         value: '0.0450 SAR' },
  { name: 'Email',      unit: 'Per Message',         value: '0.0120 SAR' },
];

// Country columns shared by Voice + WhatsApp matrices.
const detailsCountries = ['KSA', 'UAE', 'Oman', 'Qatar', 'Kwt', 'Ymn', 'Jordan', 'Egypt', 'USA', 'UK', 'India'];

// Contract Details — table per CommChannel.
const seedContractDetails = {
  // Per-country rates (SAR). Columns: KSA, UAE, Oman, Qatar, Kwt, Ymn, Jordan,
  // Egypt, USA, UK, India — realistic messaging termination economics.
  Voice: {
    kind: 'matrix',
    application: 'Voice OTP Application',
    currency: 'SAR',
    columns: detailsCountries,
    rows: [
      { label: 'High',     values: ['0.0480','0.0920','0.1150','0.1080','0.0990','0.1620','0.1340','0.0760','0.0220','0.0290','0.0180'] },
      { label: 'Normal',   values: ['0.0450','0.0880','0.1100','0.1020','0.0950','0.1550','0.1290','0.0720','0.0210','0.0270','0.0170'] },
      { label: 'Very Low', values: ['0.0420','0.0840','0.1050','0.0980','0.0910','0.1480','0.1240','0.0690','0.0200','0.0260','0.0160'] },
    ],
  },
  WhatsApp: {
    kind: 'matrix',
    application: 'Basic Send Transaction Application',
    currency: 'SAR',
    columns: detailsCountries,
    rows: [
      { label: 'Authentication', values: ['0.1850','0.1320','0.2400','0.2100','0.1950','0.4200','0.2800','0.0950','0.0540','0.0680','0.0098'] },
      { label: 'Utility',        values: ['0.1450','0.1080','0.1980','0.1720','0.1610','0.3650','0.2350','0.0790','0.0480','0.0610','0.0085'] },
      { label: 'Advertisement',  values: ['0.2650','0.1920','0.3380','0.2980','0.2780','0.5400','0.3720','0.1280','0.0720','0.0910','0.0140'] },
      { label: 'Service',        values: ['0.0250','0.0220','0.0310','0.0290','0.0270','0.0480','0.0350','0.0160','0.0090','0.0110','0.0030'] },
    ],
  },
  'AI Service': {
    kind: 'flat',
    application: 'AI Chat Application',
    currency: 'SAR',
    rows: [
      { label: 'Type',  value: 'Global' },
      { label: 'Token', value: '0.0085' },
    ],
  },
};

// Addons (entitlements + rate card on this contract).
const seedAddons = {
  entitlements: [
    { id: 'add-1', name: 'Voice Sender Name',     amount: '50,000', original: '120,000', unit: 'SAR' },
    { id: 'add-2', name: 'Email Domain Branding', amount: '12,000', original: '20,000',  unit: 'SAR' },
  ],
  rateCard: [
    /* Rate-card rows no longer carry an `original` value (per request) */
    { id: 'rc-1', name: 'Voice Sender Name',     value: '230' },
    { id: 'rc-2', name: 'Email Domain Branding', value: '95'  },
  ],
};

window.seedContracts = seedContracts;
window.seedRateCard = seedRateCard;
window.seedContractDetails = seedContractDetails;
window.seedAddons = seedAddons;

// Format a numeric string with thousands separators as the user types, e.g.
// "10000" -> "10,000", "10000.5" -> "10,000.5". Keeps a single decimal point,
// strips any other non-digit characters (including existing commas).
window.cmFmtThousands = function (str) {
  if (str == null) return '';
  let s = String(str).replace(/[^\d.]/g, '');
  const dot = s.indexOf('.');
  let intPart = dot === -1 ? s : s.slice(0, dot);
  let decPart = dot === -1 ? '' : '.' + s.slice(dot + 1).replace(/\./g, '');
  intPart = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  return intPart + decPart;
};

// Format a price / per-unit rate as the user types — numbers only, thousands
// separators on the integer part (comma every 3 digits) and at most 4 decimal
// places, e.g. "912345" -> "912,345", "12345.6789" -> "12,345.6789",
// "0.045" -> "0.045", "12.98765" -> "12.9876".
window.cmNumDecimal = function (str) {
  if (str == null) return '';
  let s = String(str).replace(/[^\d.]/g, '');
  const dot = s.indexOf('.');
  let intPart = dot === -1 ? s : s.slice(0, dot);
  // first decimal point only; cap the fraction at 4 digits
  let frac = dot === -1 ? '' : s.slice(dot + 1).replace(/\./g, '').slice(0, 4);
  intPart = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  return intPart + (dot === -1 ? '' : '.' + frac);
};
