/* global React */
// Mock data — realistic CEE sector

const FOST_CATALOG = [
  { code: 'BAR-TH-104', name: 'Pompe à chaleur de type air/eau ou eau/eau', sector: 'Résidentiel', usage: 'Chauffage', zones: ['H1','H2','H3'], coupdepouce: true, avgCumac: 42500, count: 128 },
  { code: 'BAR-TH-143', name: 'Système solaire combiné', sector: 'Résidentiel', usage: 'Chauffage+ECS', zones: ['H1','H2','H3'], coupdepouce: true, avgCumac: 68000, count: 34 },
  { code: 'BAR-EN-101', name: 'Isolation de combles ou de toitures', sector: 'Résidentiel', usage: 'Enveloppe', zones: ['H1','H2','H3'], coupdepouce: false, avgCumac: 12800, count: 201 },
  { code: 'BAR-EN-103', name: 'Isolation d\'un plancher', sector: 'Résidentiel', usage: 'Enveloppe', zones: ['H1','H2','H3'], coupdepouce: false, avgCumac: 9400, count: 89 },
  { code: 'BAT-TH-102', name: 'Pompe à chaleur de type air/eau', sector: 'Tertiaire', usage: 'Chauffage', zones: ['H1','H2','H3'], coupdepouce: false, avgCumac: 125000, count: 47 },
  { code: 'BAT-EN-101', name: 'Isolation des combles et toitures', sector: 'Tertiaire', usage: 'Enveloppe', zones: ['H1','H2','H3'], coupdepouce: false, avgCumac: 58000, count: 62 },
  { code: 'BAT-TH-113', name: 'Raccordement à un réseau de chaleur', sector: 'Tertiaire', usage: 'Chauffage', zones: ['H1','H2','H3'], coupdepouce: false, avgCumac: 340000, count: 12 },
  { code: 'IND-UT-134', name: 'Système de récupération de chaleur', sector: 'Industrie', usage: 'Process', zones: ['H1','H2','H3'], coupdepouce: false, avgCumac: 890000, count: 8 },
  { code: 'IND-UT-121', name: 'Matelas pour l\'isolation de points singuliers', sector: 'Industrie', usage: 'Process', zones: ['H1','H2','H3'], coupdepouce: false, avgCumac: 45000, count: 24 },
  { code: 'IND-BA-112', name: 'Système de gestion technique du bâtiment', sector: 'Industrie', usage: 'Pilotage', zones: ['H1','H2','H3'], coupdepouce: false, avgCumac: 210000, count: 6 },
];

const STAGES = [
  { key: 'prospect',            label: 'Détection',         sub: 'prospect',           tone: 'muted' },
  { key: 'etude',               label: 'Étude',             sub: 'etude',              tone: 'amber' },
  { key: 'devis',               label: 'Devis',             sub: 'devis',              tone: 'amber' },
  { key: 'devis_signe',         label: 'Contractualisé',    sub: 'devis_signe',        tone: 'plasma' },
  { key: 'audit_planifie',      label: 'Travaux planifiés', sub: 'audit_planifie',     tone: 'plasma' },
  { key: 'dossier_constitue',   label: 'Contrôle',          sub: 'dossier_constitue',  tone: 'plasma' },
  { key: 'dossier_depose',      label: 'Déposé PNCEE',      sub: 'dossier_depose',     tone: 'signal' },
  { key: 'prime_versee',        label: 'Prime versée',      sub: 'prime_versee',       tone: 'signal' },
];

const CLIENTS = [
  { name: 'Ducretet Industries', org: 'ORG-0047', sector: 'Industrie métallurgique', siret: '412 847 693 00028' },
  { name: 'Résidence Mirabeau', org: 'ORG-0108', sector: 'Copropriété 48 lots', siret: '891 334 108 00014' },
  { name: 'Groupe Vélan Logistique', org: 'ORG-0032', sector: 'Entrepôts frigorifiques', siret: '734 221 556 00045' },
  { name: 'Château d\'Aubec SCI', org: 'ORG-0211', sector: 'Hôtellerie 4*', siret: '668 907 442 00011' },
  { name: 'Manufacture Pelletier', org: 'ORG-0019', sector: 'Papeterie', siret: '523 019 887 00022' },
  { name: 'Ville de Saint-Calais', org: 'ORG-0176', sector: 'Collectivité', siret: '217 200 411 00013' },
  { name: 'Cliniques Argenson', org: 'ORG-0093', sector: 'Santé', siret: '449 118 226 00036' },
  { name: 'Foncière Alma-Bretagne', org: 'ORG-0152', sector: 'Immobilier tertiaire', siret: '811 455 092 00017' },
];

const CITIES = [
  { name: 'Lille',      zone: 'H1a', lat: 50.63, lng: 3.07 },
  { name: 'Strasbourg', zone: 'H1b', lat: 48.58, lng: 7.75 },
  { name: 'Lyon',       zone: 'H1c', lat: 45.76, lng: 4.83 },
  { name: 'Nantes',     zone: 'H2a', lat: 47.22, lng: -1.55 },
  { name: 'Bordeaux',   zone: 'H2c', lat: 44.84, lng: -0.58 },
  { name: 'Toulouse',   zone: 'H2c', lat: 43.60, lng: 1.44 },
  { name: 'Marseille',  zone: 'H3',  lat: 43.30, lng: 5.37 },
  { name: 'Nice',       zone: 'H3',  lat: 43.70, lng: 7.27 },
];

function seedRng(seed) {
  let s = hashStr(seed);
  return () => { s = Math.imul(s ^ s >>> 15, 2246822507); s = Math.imul(s ^ s >>> 13, 3266489909); return ((s ^ s >>> 16) >>> 0) / 4294967296; };
}

function generateDossiers() {
  const rng = seedRng('dossiers-v1');
  const out = [];
  for (let i = 1; i <= 42; i++) {
    const fost = FOST_CATALOG[Math.floor(rng() * FOST_CATALOG.length)];
    const client = CLIENTS[Math.floor(rng() * CLIENTS.length)];
    const city = CITIES[Math.floor(rng() * CITIES.length)];
    const stage = STAGES[Math.floor(rng() * STAGES.length)];
    const cumac = Math.round(fost.avgCumac * (0.6 + rng() * 1.4));
    const risk = Math.floor(rng() * 100);
    const amount = Math.round(cumac * (0.0065 + rng() * 0.0025));
    const ageDays = Math.floor(rng() * 120);
    const assignees = ['N. Bazin', 'M. Ferrand', 'L. Chevreuil', 'K. Démian', 'T. Aubert', 'J. Noguera'];
    out.push({
      ref: `DOS-2026-${String(i).padStart(3, '0')}`,
      fost: fost.code,
      fostName: fost.name,
      client: client.name,
      org: client.org,
      siret: client.siret,
      city: city.name,
      zone: city.zone,
      cumac,
      amount,
      stage: stage.key,
      risk,
      ageDays,
      owner: assignees[Math.floor(rng() * assignees.length)],
      created: new Date(Date.now() - ageDays * 86400000).toISOString().slice(0, 10),
      pieces: { ok: Math.floor(rng() * 8) + 2, total: 10 },
      signatures: { done: Math.floor(rng() * 3), total: 3 },
      coherence: Math.floor(40 + rng() * 60),
    });
  }
  return out;
}

const DOSSIERS = generateDossiers();

// 30-day timeseries
function genSeries(base, jitter, len = 30) {
  const rng = seedRng('series-' + base);
  return Array.from({ length: len }, (_, i) => Math.max(0, base + Math.sin(i / 3) * jitter * 0.5 + (rng() - 0.5) * jitter));
}

const METRICS = {
  cumacPipeline: 12_847_000,
  cumacDeposited: 4_290_000,
  primesEngagees: 94_500,
  primesVersees: 31_200,
  dossiersActifs: DOSSIERS.length,
  tauxConversion: 68.4,
  scoreMoyen: 82,
  delaiMoyenDepot: 41,
  depositsLast30: genSeries(6, 3).map(Math.round),
  cumacLast30: genSeries(420, 180),
  primesLast30: genSeries(3.2, 1.6),
};

// Activity feed
const ACTIVITY = [
  { at: 'il y a 2 min', who: 'Co-pilote', type: 'ai', title: 'Score de risque recalculé', detail: 'DOS-2026-017 — passé de 72 à 58 (↓ coherence AH)', tone: 'plasma' },
  { at: 'il y a 8 min', who: 'M. Ferrand', type: 'sign', title: 'AH signée par bénéficiaire', detail: 'DOS-2026-024 · Résidence Mirabeau', tone: 'signal' },
  { at: 'il y a 14 min', who: 'Système', type: 'pncee', title: 'Dépôt PNCEE accepté', detail: 'DOS-2026-009 · +42 300 € versés J+38', tone: 'signal' },
  { at: 'il y a 23 min', who: 'Co-pilote', type: 'ai', title: 'Anomalie détectée', detail: 'DOS-2026-031 — date AH antérieure au devis', tone: 'copper' },
  { at: 'il y a 41 min', who: 'L. Chevreuil', type: 'upload', title: '4 pièces ajoutées', detail: 'DOS-2026-018 · OCR en cours', tone: 'muted' },
  { at: 'il y a 1 h',   who: 'Odoo',       type: 'sync', title: 'Sync Odoo terminée', detail: '128 opportunités · 3 diff. résolues', tone: 'muted' },
  { at: 'il y a 1 h',   who: 'Veille',     type: 'ai', title: 'Nouvel arrêté publié au JORF', detail: 'BAR-TH-104 — bonification précarité prolongée', tone: 'amber' },
  { at: 'il y a 2 h',   who: 'N. Bazin',   type: 'stage', title: 'Contractualisé', detail: 'DOS-2026-022 · Château d\'Aubec', tone: 'muted' },
];

Object.assign(window, { FOST_CATALOG, STAGES, CLIENTS, CITIES, DOSSIERS, METRICS, ACTIVITY });
