/* portal-data.jsx — Single-customer view (Hotel Sereno) */

// The customer logged in
const PORTAL_CUSTOMER = CRM_BY_ID['HTL-26-0142'];   // Hotel Sereno, Comporta
const PORTAL_CONTRACT = CRM_CONTRACTS.find(c => c.id === 'HTL-26-0142');

// Enrich base products with full info
const PORTAL_BASE = PORTAL_CONTRACT.base.map(([code, qty]) => {
  const p = CRM_PRODUCTS.find(x => x.code === code);
  return { ...p, qty };
});

// Suggested extras (curated, based on past cycles)
const PORTAL_SUGGESTED = [
  { code: 'ALG-RPN-ML', reason: 'Pediu 8 unidades no Q2 · época alta'},
  { code: 'BGP-PCB-10', reason: 'Lobby · novo·' },
];

// Past cycles (Q1, Q2 done; Q3 awaiting; Q4..Q8 future)
const PORTAL_CYCLES = [
  {
    q: 1, label: 'Q1', date: '15.12.2024', state: 'delivered',
    invoice: 'F2024/0419', value: 3842.00,
    delivery: { carrier: 'CTT Expresso', tracking: 'CT938201741PT', date: '18.12.2024', boxes: 7 },
    extras: [],
  },
  {
    q: 2, label: 'Q2', date: '15.03.2025', state: 'delivered',
    invoice: 'F2025/0102', value: 4022.00,
    delivery: { carrier: 'CTT Expresso', tracking: 'CT102873440PT', date: '19.03.2025', boxes: 8 },
    extras: [['ALG-RPN-ML', 8]],
  },
  {
    q: 3, label: 'Q3', date: '15.06.2025', state: 'awaiting',
    invoice: null, value: 3842.00,
    delivery: null,
    extras: [],
  },
  { q: 4, label: 'Q4', date: '15.09.2025', state: 'future' },
  { q: 5, label: 'Q5', date: '15.12.2025', state: 'future' },
  { q: 6, label: 'Q6', date: '15.03.2026', state: 'future' },
  { q: 7, label: 'Q7', date: '15.06.2026', state: 'future' },
  { q: 8, label: 'Q8', date: '15.09.2026', state: 'future' },
];

// Recent activity (customer-facing)
const PORTAL_ACTIVITY = [
  { when: 'hoje', kind: 'cycle',    title: 'Ciclo Q3 aberto · extras pendentes', detail: 'D-7 a 8 SET · ajuste extras até dia 7' },
  { when: 'há 3 dias', kind: 'info', title: 'Catálogo atualizado',  detail: '2 produtos novos · ver Catálogo' },
  { when: '20.03.2025', kind: 'paid', title: 'Cobrança recebida',     detail: 'SEPA SDD · 4 022,00 € · F2025/0102' },
  { when: '19.03.2025', kind: 'shipped', title: 'Encomenda entregue',  detail: 'CTT Expresso · 8 caixas' },
  { when: '12.03.2025', kind: 'info', title: 'Extras Q2 confirmados', detail: '+1 SKU · +496,00 € · Roupão (8)' },
  { when: '13.12.2024', kind: 'paid', title: 'Primeira cobrança paga', detail: 'SEPA SDD · 3 842,00 €' },
  { when: '14.09.2024', kind: 'info', title: 'Contrato assinado',     detail: 'PDF v1.0 · 24 meses · 8 ciclos trimestrais' },
];

// Stripe/SEPA mandate
const PORTAL_MANDATE = {
  status: 'active',
  iban: 'PT50 0035 0000 0001 23456 78',
  bank: 'Caixa Geral de Depósitos',
  ref:  'MND-2024-09-14',
  signed: '14.09.2024',
  provider: 'Stripe',
};

// Delivery address
const PORTAL_DELIVERY = {
  name: 'Hotel Sereno',
  attn: 'Receção · Maria Antunes',
  street: 'Estrada da Praia da Comporta, lote 4',
  zip: '7570-337 Comporta',
  country: 'Portugal',
};

// Account contacts
const PORTAL_CONTACTS = [
  { role: 'Principal',  name: 'Maria Antunes',  email: 'maria@hotelsereno.pt',     phone: '+351 265 488 220', avatar:'M' },
  { role: 'Faturação',  name: 'Carlos Penalva', email: 'contas@hotelsereno.pt',    phone: '+351 265 488 221', avatar:'C' },
  { role: 'Receção',    name: 'Beatriz Loureiro', email: 'rec@hotelsereno.pt',      phone: '+351 265 488 222', avatar:'B' },
];

// The RVDE relationship contact (the human on the other side)
const PORTAL_REP = {
  name: 'Jorge Libório',
  role: 'RVDE · gerente',
  email: 'jorge@rvde.pt',
  phone: '+351 962 408 117',
  avatar: 'J',
  bio: 'Acompanha o teu contrato desde o primeiro ciclo.',
};

// Compute D-7 deadline (mock: today is 1 Sept 2025; D-7 = 8 Sept)
const PORTAL_TODAY    = '01 SET 2025';
const PORTAL_D7_DATE  = '08 SET 2025';
const PORTAL_D7_DAYS  = 7; // dias até D-7
const PORTAL_CHARGE_DATE = '15 SET 2025';

// ── Suspended state context (SEPA failed) ────────────────
// Timeline: 15 SET cobrança falhou · 18 SET retry1 falhou · 21 SET retry2 falhou
// "today" in suspended view = 22 SET 2025 (day after 2nd retry)
const PORTAL_SUSPENDED = {
  today:    '22 SET 2025',
  failedOn: '15 SET 2025',
  reasonCode: 'AC04',
  reasonLabel: 'Conta encerrada ou inacessível',
  amount: 3842.00,
  invoice: 'F2025/0431',
  retries: [
    { date: '15 SET', status: 'failed', label: 'Tentativa 1 · falhou', code: 'AC04' },
    { date: '18 SET', status: 'failed', label: 'Tentativa 2 · falhou', code: 'AC04' },
    { date: '21 SET', status: 'failed', label: 'Tentativa 3 · falhou', code: 'AC04' },
  ],
  deadline:    '28 SET 2025',
  daysToResolve: 6,
};

// ── Renewal window state (D-60 before contract end) ──────
// Contract: 14.09.2024 → 14.09.2026, renewal window opens 60d before
// "today" in renewal view = 15 JUL 2026 (60 days before 14 SET 2026)
const PORTAL_RENEWAL = {
  today:        '15 JUL 2026',
  endDate:      '14 SET 2026',
  noticeBy:     '15 JUL 2026',     // deadline to denounce (60d before end)
  daysToDecide: 60,
  newStart:     '15 SET 2026',
  newEnd:       '14 SET 2028',
  currentValue: 3842.00,           // per cycle, current contract
  // Jorge's renegotiation suggestion (Spa might be growing, slight adjustment)
  suggestedValue: 4112.00,
  suggestedChanges: [
    { code: 'CDR-SHM-5L', from: 10, to: 12, reason: '+2 · ocupação Verão' },
    { code: 'LNH-TLR-46', from: 80, to: 96, reason: '+16 · novo serviço de spa' },
  ],
  // Past 22 months of relationship (numbers to celebrate)
  cyclesDelivered: 7,
  totalValue:    27894.00,
  productsSent:  3640,
};

// ── Ended state context ──────────────────────────────────
// Contract ran out at 14.09.2026, in "ended" view today = 20 SET 2026
const PORTAL_ENDED = {
  today:       '20 SET 2026',
  endedOn:     '14 SET 2026',
  cyclesDone:  8,
  totalValue:  30736.00,
  lastInvoice: 'F2026/0612',
  archiveUntil:'14 SET 2027',  // 12 months read-only access
};

Object.assign(window, {
  PORTAL_CUSTOMER, PORTAL_CONTRACT, PORTAL_BASE, PORTAL_SUGGESTED,
  PORTAL_CYCLES, PORTAL_ACTIVITY, PORTAL_MANDATE, PORTAL_DELIVERY,
  PORTAL_CONTACTS, PORTAL_REP,
  PORTAL_TODAY, PORTAL_D7_DATE, PORTAL_D7_DAYS, PORTAL_CHARGE_DATE,
  PORTAL_SUSPENDED, PORTAL_RENEWAL, PORTAL_ENDED,
});
