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
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import React, { useMemo, useState } from "react";
import "./overview.css";

/* ================= KPI CARD ================= */
function StackOverflowKpiCard({
  icon,
  title,
  value,
  subtitle,
  accent,
  trend,
  progress,
  onClick,
  isActive,
}) {
  const [isHovered, setIsHovered] = useState(false);

  return (
    <div
      className={`kpi-card ${accent || ""} ${isActive ? "active" : ""}`}
      onClick={onClick}
      onMouseEnter={() => setIsHovered(true)}
      onMouseLeave={() => setIsHovered(false)}
    >
      <div className="kpi-header">
        <span className={`kpi-icon ${isHovered ? "bounce" : ""}`}>{icon}</span>
        <span className="kpi-title">{title}</span>
        {trend && (
          <span className={`kpi-trend ${trend > 0 ? "positive" : "negative"}`}>
            {trend > 0 ? "↑" : "↓"} {Math.abs(trend)}%
          </span>
        )}
      </div>
      <div className="kpi-value-container">
        <div className="kpi-value">{value}</div>
        {progress !== undefined && (
          <div className="kpi-progress-bar">
            <div
              className="kpi-progress-fill"
              style={{ width: `${Math.min(progress, 100)}%` }}
            />
          </div>
        )}
      </div>
      {subtitle && <div className="kpi-sub">{subtitle}</div>}
    </div>
  );
}

/* ================= FEATURED METRIC ================= */
function StackOverflowFeaturedMetric({
  icon,
  title,
  value,
  subtitle,
  description,
}) {
  return (
    <div className="featured-metric">
      <div className="featured-icon">{icon}</div>
      <div className="featured-content">
        <div className="featured-title">{title}</div>
        <div className="featured-value">{value}</div>
        {subtitle && <div className="featured-subtitle">{subtitle}</div>}
        {description && (
          <div className="featured-description">{description}</div>
        )}
      </div>
    </div>
  );
}

/* ================= MAIN OVERVIEW ================= */
export default function StackOverflowKpiOverview({ rows = [] }) {
  const [selectedMetric, setSelectedMetric] = useState(null);
  const [viewMode, setViewMode] = useState("grid");

  const metrics = useMemo(() => {
    if (!rows.length) return null;

    // Demographics
    const ageGroups = {};
    const educationLevels = {};
    const countries = {};
    const employmentTypes = {};
    const orgSizes = {};
    const remoteWorkTypes = {};

    // Experience
    const yearsCodeRanges = {};
    const devTypes = new Set();
    const icOrPM = {};

    // Technology
    const languages = new Set();
    const databases = new Set();
    const platforms = new Set();
    const aiTools = new Set();

    // Salary & Satisfaction
    let totalSalary = 0;
    let salaryCount = 0;
    const jobSatLevels = {};

    // AI Usage
    let aiUsers = 0;
    let aiSentiments = { positive: 0, negative: 0, neutral: 0 };
    let aiThreats = { yes: 0, no: 0 };

    rows.forEach((row) => {
      // Demographics
      if (row.Age) {
        ageGroups[row.Age] = (ageGroups[row.Age] || 0) + 1;
      }
      if (row.EdLevel) {
        educationLevels[row.EdLevel] = (educationLevels[row.EdLevel] || 0) + 1;
      }
      if (row.Country) {
        countries[row.Country] = (countries[row.Country] || 0) + 1;
      }
      if (row.Employment) {
        employmentTypes[row.Employment] =
          (employmentTypes[row.Employment] || 0) + 1;
      }
      if (row.OrgSize) {
        orgSizes[row.OrgSize] = (orgSizes[row.OrgSize] || 0) + 1;
      }
      if (row.RemoteWork) {
        remoteWorkTypes[row.RemoteWork] =
          (remoteWorkTypes[row.RemoteWork] || 0) + 1;
      }

      // Experience
      if (row.YearsCode) {
        yearsCodeRanges[row.YearsCode] =
          (yearsCodeRanges[row.YearsCode] || 0) + 1;
      }
      if (row.DevType) {
        row.DevType.split(";").forEach((type) => devTypes.add(type.trim()));
      }
      if (row.ICorPM) {
        icOrPM[row.ICorPM] = (icOrPM[row.ICorPM] || 0) + 1;
      }

      // Technology
      if (row.LanguageHaveWorkedWith) {
        row.LanguageHaveWorkedWith.split(";").forEach((lang) =>
          languages.add(lang.trim())
        );
      }
      if (row.DatabaseHaveWorkedWith) {
        row.DatabaseHaveWorkedWith.split(";").forEach((db) =>
          databases.add(db.trim())
        );
      }
      if (row.PlatformHaveWorkedWith) {
        row.PlatformHaveWorkedWith.split(";").forEach((platform) =>
          platforms.add(platform.trim())
        );
      }
      if (row.AIModelsHaveWorkedWith) {
        row.AIModelsHaveWorkedWith.split(";").forEach((ai) =>
          aiTools.add(ai.trim())
        );
        aiUsers++;
      }

      // Salary
      if (row.CompTotal && row.CompTotal !== "NA" && row.CompTotal !== "") {
        const salary = parseFloat(row.CompTotal.replace(/,/g, ""));
        if (!isNaN(salary)) {
          totalSalary += salary;
          salaryCount++;
        }
      }

      // Job Satisfaction
      if (row.JobSat) {
        jobSatLevels[row.JobSat] = (jobSatLevels[row.JobSat] || 0) + 1;
      }

      // AI Sentiment
      if (row.AISent) {
        if (
          row.AISent.toLowerCase().includes("agree") ||
          row.AISent.toLowerCase().includes("positive")
        ) {
          aiSentiments.positive++;
        } else if (
          row.AISent.toLowerCase().includes("disagree") ||
          row.AISent.toLowerCase().includes("negative")
        ) {
          aiSentiments.negative++;
        } else {
          aiSentiments.neutral++;
        }
      }

      // AI Threat Perception
      if (row.AIThreat) {
        if (
          row.AIThreat.toLowerCase().includes("agree") ||
          row.AIThreat.toLowerCase() === "yes"
        ) {
          aiThreats.yes++;
        } else {
          aiThreats.no++;
        }
      }
    });

    const avgSalary = salaryCount > 0 ? totalSalary / salaryCount : 0;
    const aiAdoptionRate = (aiUsers / rows.length) * 100;
    const topCountry = Object.entries(countries).sort((a, b) => b[1] - a[1])[0];
    const topLanguage = Array.from(languages).length;
    const topDevType = Array.from(devTypes).length;

    return {
      totalRespondents: rows.length,
      avgSalary,
      salaryCount,
      uniqueCountries: Object.keys(countries).length,
      uniqueLanguages: topLanguage,
      uniqueDevTypes: topDevType,
      aiAdoptionRate,
      aiUsers,
      aiSentiments,
      aiThreats,
      topCountry: topCountry ? topCountry[0] : "N/A",
      topCountryCount: topCountry ? topCountry[1] : 0,
      remoteWorkPercentage:
        (((remoteWorkTypes["Remote"] || 0) + (remoteWorkTypes["Hybrid"] || 0)) /
          rows.length) *
        100,
      fullyRemotePercentage:
        ((remoteWorkTypes["Remote"] || 0) / rows.length) * 100,
      educationDiversity: Object.keys(educationLevels).length,
      avgYearsCode: calculateAverageYearsCode(yearsCodeRanges),
      jobSatisfaction: calculateJobSatisfaction(jobSatLevels),
      organizationSizes: Object.keys(orgSizes).length,
    };
  }, [rows]);

  function calculateAverageYearsCode(yearsCodeRanges) {
    let totalYears = 0;
    let totalRespondents = 0;

    Object.entries(yearsCodeRanges).forEach(([range, count]) => {
      const avgYears = parseCodeRange(range);
      if (avgYears !== null) {
        totalYears += avgYears * count;
        totalRespondents += count;
      }
    });

    return totalRespondents > 0 ? totalYears / totalRespondents : 0;
  }

  function parseCodeRange(range) {
    if (range.includes("More than 50")) return 55;
    if (range.includes("30-44")) return 37;
    if (range.includes("20-29")) return 25;
    if (range.includes("15-19")) return 17;
    if (range.includes("10-14")) return 12;
    if (range.includes("5-9")) return 7;
    if (range.includes("3-5")) return 4;
    if (range.includes("1-2")) return 1.5;
    if (range.includes("Less than 1")) return 0.5;
    return null;
  }

  function calculateJobSatisfaction(jobSatLevels) {
    const total = Object.values(jobSatLevels).reduce(
      (sum, count) => sum + count,
      0
    );
    const satisfied =
      (jobSatLevels["Very satisfied"] || 0) +
      (jobSatLevels["Slightly satisfied"] || 0);
    return total > 0 ? (satisfied / total) * 100 : 0;
  }

  if (!metrics) return null;

  const kpiData = [
    {
      icon: "πŸ‘₯",
      title: "Total Respondents",
      value: metrics.totalRespondents.toLocaleString(),
      subtitle: "Developer survey participants",
      accent: "accent-primary",
      trend: 15.2,
      progress: (metrics.totalRespondents / 100000) * 100,
    },

    {
      icon: "🌍",
      title: "Global Reach",
      value: metrics.uniqueCountries,
      subtitle: `Top: ${metrics.topCountry} (${metrics.topCountryCount})`,
      accent: "accent-info",
      trend: 5.3,
      progress: (metrics.uniqueCountries / 200) * 100,
    },
    {
      icon: "πŸ’»",
      title: "Languages Used",
      value: metrics.uniqueLanguages,
      subtitle: "Programming languages",
      accent: "accent-secondary",
      trend: 12.1,
      progress: (metrics.uniqueLanguages / 50) * 100,
    },
    {
      icon: "🏒",
      title: "Developer Types",
      value: metrics.uniqueDevTypes,
      subtitle: "Professional roles",
      accent: "accent-warning",
      trend: 7.8,
      progress: (metrics.uniqueDevTypes / 30) * 100,
    },

    {
      icon: "🏠",
      title: "Remote Work",
      value: `${metrics.remoteWorkPercentage.toFixed(1)}%`,
      subtitle: `${metrics.fullyRemotePercentage.toFixed(1)}% fully remote`,
      accent: "accent-success",
      trend: 18.5,
      progress: metrics.remoteWorkPercentage,
    },
    {
      icon: "πŸŽ“",
      title: "Education Diversity",
      value: metrics.educationDiversity,
      subtitle: "Education levels",
      accent: "accent-info",
      trend: 3.2,
      progress: (metrics.educationDiversity / 10) * 100,
    },
    // {
    //   icon: "πŸ“Š",
    //   title: "Avg Experience",
    //   value: `${metrics.avgYearsCode.toFixed(1)} yrs`,
    //   subtitle: "Years coding",
    //   accent: "accent-primary",
    //   trend: 2.1,
    //   progress: (metrics.avgYearsCode / 20) * 100,
    // },
  ];

  const insights = [
    // {
    //   type: "success",
    //   icon: "πŸš€",
    //   text: `AI adoption is booming with ${metrics.aiAdoptionRate.toFixed(
    //     1
    //   )}% of developers using AI tools, showing ${(
    //     (metrics.aiSentiments.positive / (metrics.aiUsers || 1)) *
    //     100
    //   ).toFixed(1)}% positive sentiment`,
    // },
    {
      type: "info",
      icon: "🌐",
      text: `Global developer community spans ${metrics.uniqueCountries} countries with ${metrics.uniqueLanguages} programming languages in use`,
    },
    {
      type: "success",
      icon: "🏠",
      text: `Remote work revolution: ${metrics.remoteWorkPercentage.toFixed(
        1
      )}% work remotely with ${metrics.fullyRemotePercentage.toFixed(
        1
      )}% fully remote`,
    },
    // {
    //   type: "warning",
    //   icon: "πŸ’°",
    //   text: `Average salary of $${metrics.avgSalary.toLocaleString()} across ${
    //     metrics.salaryCount
    //   } respondents, showing ${metrics.jobSatisfaction.toFixed(
    //     1
    //   )}% job satisfaction`,
    // },
  ];

  return (
    <div className="kpi-overview-container">
      {/* ================= HEADER ================= */}
      <div className="kpi-header-section">
        <div className="kpi-title-area">
          <h1 className="kpi-main-title">
            Stack Overflow Developer Survey Insights
          </h1>
          <div className="kpi-subtitle">
            <span className="live-indicator">●</span>
            Live developer community analytics and trends
          </div>
        </div>
        {/* <div className="kpi-controls">
          <button
            className={`view-toggle ${viewMode === "grid" ? "active" : ""}`}
            onClick={() => setViewMode("grid")}
          >
            Grid View
          </button>
          <button
            className={`view-toggle ${viewMode === "list" ? "active" : ""}`}
            onClick={() => setViewMode("list")}
          >
            List View
          </button>
        </div> */}
      </div>

      {/* ================= FEATURED METRICS ================= */}
      <div className="featured-metrics-row">
        <StackOverflowFeaturedMetric
          icon="πŸ‘₯"
          title="Developer Community"
          value={metrics.totalRespondents.toLocaleString()}
          subtitle={`${metrics.uniqueCountries} countries represented`}
          description="Global developer participation in annual survey"
        />
        {/* <StackOverflowFeaturedMetric
          icon="πŸ€–"
          title="AI Revolution"
          value={`${metrics.aiAdoptionRate.toFixed(1)}%`}
          subtitle={`${metrics.aiUsers.toLocaleString()} AI tool users`}
          description={`${(
            (metrics.aiSentiments.positive / (metrics.aiUsers || 1)) *
            100
          ).toFixed(1)}% positive AI sentiment`}
        /> */}
        <StackOverflowFeaturedMetric
          icon="🌍"
          title="Tech Diversity"
          value={metrics.uniqueLanguages}
          subtitle={`${metrics.uniqueDevTypes} developer roles`}
          description={`${metrics.uniqueCountries} countries, ${metrics.educationDiversity} education levels`}
        />
      </div>

      {/* ================= KPI GRID ================= */}
      <div className={`kpi-grid ${viewMode}`}>
        {kpiData.map((kpi, index) => (
          <StackOverflowKpiCard
            key={index}
            {...kpi}
            onClick={() =>
              setSelectedMetric(selectedMetric === index ? null : index)
            }
            isActive={selectedMetric === index}
          />
        ))}
      </div>

      {/* ================= DETAILED INSIGHTS ================= */}
      <div className="insights-section">
        <div className="insights-header">
          <h2>🎯 Developer Insights</h2>
          <div className="insights-subtitle">
            Key trends and observations from the developer community
          </div>
        </div>
        <div className="insights-grid">
          {insights.map((insight, index) => (
            <div key={index} className={`insight-card insight-${insight.type}`}>
              <div className="insight-icon">{insight.icon}</div>
              <div className="insight-text">{insight.text}</div>
            </div>
          ))}
        </div>
      </div>

      {/* ================= SUMMARY STATS ================= */}
      <div className="summary-stats">
        <div className="stat-item">
          <div className="stat-label">Community Growth</div>
          <div className="stat-value">+15.2%</div>
          <div className="stat-period">vs. last year</div>
        </div>
        {/* <div className="stat-item">
          <div className="stat-label">AI Adoption</div>
          <div className="stat-value">{metrics.aiAdoptionRate.toFixed(1)}%</div>
          <div className="stat-period">using AI tools</div>
        </div> */}
        <div className="stat-item">
          <div className="stat-label">Remote Work</div>
          <div className="stat-value">
            {metrics.remoteWorkPercentage.toFixed(1)}%
          </div>
          <div className="stat-period">work remotely</div>
        </div>
        {/* <div className="stat-item">
          <div className="stat-label">Job Satisfaction</div>
          <div className="stat-value">
            {metrics.jobSatisfaction.toFixed(1)}%
          </div>
          <div className="stat-period">satisfied developers</div>
        </div> */}
      </div>
    </div>
  );
}