/* Geteilte Team-Daten – single source of truth für about.html UND karriere.html.
   Wird VOR den seitenspezifischen Scripten geladen via:
     <script type="text/babel" src="team-data.jsx"></script>
   Alle Variablen liegen auf window, damit sie aus anderen <script>-Blöcken
   sichtbar sind (klassische, nicht-modul Scripte). */

function tenureLabel(since, prefix) {
  if (!since) return prefix || '';
  const start = new Date(since);
  const now = new Date();
  if (isNaN(start.getTime())) return prefix || '';
  if (start > now) {
    return (prefix ? prefix + ' · ' : '') + 'Neu im Team · ' + start.getFullYear();
  }
  let years = now.getFullYear() - start.getFullYear();
  const anniv = new Date(now.getFullYear(), start.getMonth(), start.getDate());
  if (anniv > now) years -= 1;
  if (years <= 0) {
    return (prefix ? prefix + ' · ' : '') + 'Neu im Team · ' + start.getFullYear();
  }
  const label = 'Seit ' + start.getFullYear() + ' · ' + years + ' Jahr' + (years === 1 ? '' : 'e');
  return prefix ? prefix + ' · ' + label : label;
}

function tenureYears(since) {
  if (!since) return 0;
  const start = new Date(since);
  const now = new Date();
  if (isNaN(start.getTime()) || start > now) return 0;
  let years = now.getFullYear() - start.getFullYear();
  const anniv = new Date(now.getFullYear(), start.getMonth(), start.getDate());
  if (anniv > now) years -= 1;
  return Math.max(0, years);
}

window.tenureLabel = tenureLabel;
window.tenureYears = tenureYears;

window.teamGFs = [
  {
    name: 'Peter Sprus',
    initials: 'PS',
    photo: 'assets/team/peter.webp',
    photoFun: 'assets/team/peter-fun.webp',
    role: 'Gründer · Geschäftsführer',
    since: '2005-05-30',
    yearsAt: 'Seit Gründung · ' + (window.companyYears ? window.companyYears() : (new Date().getFullYear() - 2005)) + ' Jahre',
    sub: 'Dipl.-Ing. (FH)',
    cred: 'Mitglied der Bayerischen Ingenieurekammer-Bau',
    co: ['DESIGN', 'BAUTRÄGER'],
  },
  {
    name: 'Jan Sprus',
    initials: 'JS',
    photo: 'assets/team/jan.webp',
    photoFun: 'assets/team/jan-fun.webp',
    role: 'Geschäftsführer kfm.',
    since: '2021-06-01',
    yearsAt: tenureLabel('2021-06-01'),
    sub: 'Staatlich geprüfter Bauzeichner',
    cred: 'Kaufmännische Leitung & Digitalisierung',
    co: ['DESIGN', 'BAUTRÄGER'],
    linkedin: 'https://www.linkedin.com/in/jansprus',
  },
  {
    name: 'Quirin Hellthaler',
    initials: 'QH',
    photo: 'assets/team/quirin.webp',
    photoFun: 'assets/team/quirin-fun.webp',
    role: 'Geschäftsführer tech.',
    since: '2018-09-17',
    yearsAt: tenureLabel('2018-09-17'),
    sub: 'Staatlich geprüfter Bautechniker',
    cred: 'Bauleitung',
    co: ['DESIGN', 'BAUTRÄGER'],
  },
];

window.teamMembers = [
  /* Susanne als Direktvertrieb steht oben – sie ist erste Anlaufstelle
     für Käufer*innen. Danach Mitarbeiter nach Firmenzugehörigkeit. */
  {
    name: 'Susanne Sprus',
    initials: 'SS',
    photo: 'assets/team/susanne.webp',
    photoFun: 'assets/team/susanne-fun.webp',
    role: 'Vertriebsleitung',
    since: '2017-02-07',
    yearsAt: tenureLabel('2017-02-07'),
    sub: 'Dipl.-Kauffrau (Univ.)',
    cred: 'Immobilienmaklerin (IHK) · Bankkauffrau (IHK) · Dipl.-Hdl. (Univ.)',
    co: ['BAUTRÄGER'],
    contact: { tel: '+4986416945017', mail: 'susanne.sprus@sprusdesign.de' },
    highlight: true,
    highlightBadge: 'Direktvertrieb · ohne Maklerprovision',
  },
  {
    name: 'Thomas Kaiser',
    initials: 'TK',
    photo: 'assets/team/thomas-kaiser.webp',
    photoFun: 'assets/team/thomas-kaiser-fun.webp',
    role: 'Bauleitung',
    since: '2006-02-01',
    yearsAt: tenureLabel('2006-02-01'),
    sub: 'Innenarchitekt (studiert)',
    cred: 'Bauleitung mit Blick fürs Innendetail',
    legacy: 'Ehem. Gesellschafter & Prokurist',
    co: ['DESIGN'],
  },
  {
    name: 'Kerstin Klauser',
    initials: 'KK',
    photo: 'assets/team/kerstin.webp',
    photoFun: 'assets/team/kerstin-fun.webp',
    role: 'Planung',
    since: '2008-01-01',
    yearsAt: tenureLabel('2008-01-01'),
    sub: 'Staatlich geprüfte Bauzeichnerin',
    cred: 'Entwurf · Werkpläne',
    co: ['DESIGN'],
  },
  {
    name: 'Anita Garisch',
    initials: 'AG',
    photo: 'assets/team/anita.webp',
    photoFun: 'assets/team/anita-fun.webp',
    role: 'Sekretariat',
    since: '2012-05-01',
    yearsAt: tenureLabel('2012-05-01'),
    sub: 'Büromanagement',
    cred: 'Korrespondenz · Termine · Organisation',
    co: ['DESIGN', 'BAUTRÄGER'],
  },
  {
    name: 'Judith Sprus',
    initials: 'JS',
    photo: 'assets/team/judith.webp',
    photoFun: 'assets/team/judith-fun.webp',
    role: 'Sekretariat',
    since: '2012-09-01',
    yearsAt: tenureLabel('2012-09-01'),
    sub: 'Büroleitung',
    cred: 'Telefonzentrale · Schnittstelle Kunden',
    co: ['DESIGN', 'BAUTRÄGER'],
  },
  {
    name: 'Peter Kelling',
    initials: 'PK',
    photo: 'assets/team/peter-kelling.webp',
    photoFun: 'assets/team/peter-kelling-fun.webp',
    role: 'Planung',
    since: '2025-05-07',
    yearsAt: tenureLabel('2025-05-07'),
    sub: 'Staatlich geprüfter Bauzeichner',
    cred: 'Detailplanung · Konstruktion',
    co: ['DESIGN'],
  },
  {
    name: 'Helmut Niedermaier',
    initials: 'HN',
    photo: 'assets/team/helmut.webp',
    photoFun: 'assets/team/helmut-fun.webp',
    role: 'Bauleitung',
    since: '2026-05-15',
    yearsAt: tenureLabel('2026-05-15'),
    sub: 'Staatlich geprüfter Bautechniker',
    cred: 'Baustellen-Koordination · Qualitätskontrolle',
    co: ['DESIGN'],
  },
];

window.teamStats = (function () {
  const all = window.teamGFs.concat(window.teamMembers);
  const count = all.length;
  const sumYears = all.reduce((acc, p) => acc + tenureYears(p.since), 0);
  const avg = count ? Math.round(sumYears / count) : 0;
  /* "Familie" zählt zur core team, aber wir splitten für Detail-Stats. */
  const familyCount = all.filter(p => /Familie/.test(p.yearsAt || '') || p.name === 'Peter Sprus').length;
  const bautraegerCount = all.filter(p => p.co.includes('BAUTRÄGER')).length;
  const designCount = all.filter(p => p.co.includes('DESIGN')).length;
  return { count, avgTenureYears: avg, familyCount, bautraegerCount, designCount };
})();
