All files / src/browser/cia data-loader.ts

58.8% Statements 157/267
37.31% Branches 103/276
53.94% Functions 41/76
61.27% Lines 144/235

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    2x     2x                                                 2x     14x 14x       2x                                                                                                                                                                                                                                 27x 27x   156x 27x   27x 106x 106x     106x 106x 106x 106x 3506x 3506x 58x 3448x 578x 578x   2870x     106x   106x 106x 684x 684x 684x   106x   106x                   30x     30x       30x 30x 30x 30x 27x 27x 27x             3x 3x                                                                                                                                                                                                 9x         9x 362x 4x       4x     9x 29x 29x 29x 29x 9x   1x     9x 51x 51x 51x 51x 51x   51x 1x     50x 50x   50x                         9x 29x 29x 29x 29x 29x 29x   103x     29x               3x     26x                   9x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         2x           2x 2x 2x           2x 2x 2x   2x 2x 2x 2x 2x       2x     2x 1x 8x 64x 56x         1x 1x 2x   2x 2x           2x     1x 8x 8x 64x 56x 64x           2x   32x 16x 16x 16x 16x 16x           16x   2x                                   1x   1x 2x 1x                   1x                         1x 1x         1x 3x 2x           1x 3x 2x           1x                           1x         1x 2x 1x           1x 2x 1x                 1x                           1x   1x 2x 1x             1x                                                                                    
/**
 * @module CIA/DataLoader
 * @category Intelligence Platform - Data Acquisition & Pipeline Management
 *
 * @description
 * CIA Intelligence Data Loader & Pipeline Orchestrator.
 * Core data acquisition module implementing multi-source intelligence data loading
 * from the Citizen Intelligence Agency (CIA) Platform. Manages CSV export ingestion
 * for 19+ intelligence product categories and JSON fallback for model-generated
 * electoral forecasts. Provides resilient data pipeline with local-first strategy
 * and remote fallback capabilities.
 *
 * @author Hack23 AB - Data Pipeline Engineering
 * @license Apache-2.0
 * @version 2.0.0
 * @since 2024
 
 *
 * @intelligence CIA Platform Data Pipeline Orchestrator — core data acquisition module implementing multi-source intelligence data loading from 19+ CIA product categories. Manages CSV export ingestion and JSON fallback for electoral forecasts. Provides resilient pipeline with local-first strategy and remote fallback.
 *
 * @business Data infrastructure investment — the CIA data pipeline is the foundation for all analytical products. Pipeline reliability directly impacts user experience and platform credibility. Modular architecture enables future data source expansion (European Parliament, Nordic councils).
 *
 * @marketing Data transparency asset — transparent data sourcing (CIA Platform, open government data) builds trust with all audience segments. Data pipeline documentation demonstrates commitment to accuracy and verifiability, key messaging for press and academic audiences.
 * */
 
/* ------------------------------------------------------------------ */
/*  Interfaces                                                        */
/* ------------------------------------------------------------------ */
 
/** Definition for a single CSV data source mapping. */
export interface CSVSourceDefinition {
  /** Relative path within the csvBaseURL directory. */
  local: string;
  /** Human-readable description of the data product. */
  description: string;
}
 
/** Map of all known CSV source categories. */
export interface CSVSourceMap {
  personStatus: CSVSourceDefinition;
  riskByParty: CSVSourceDefinition;
  riskLevels: CSVSourceDefinition;
  annualBallots: CSVSourceDefinition;
  crisisResilience: CSVSourceDefinition;
  partyPerformance: CSVSourceDefinition;
  partyMetrics: CSVSourceDefinition;
  partyMomentum: CSVSourceDefinition;
  partyMembers: CSVSourceDefinition;
  influenceMetrics: CSVSourceDefinition;
  riskSummary: CSVSourceDefinition;
  committeeProductivity: CSVSourceDefinition;
  committeeActivity: CSVSourceDefinition;
  partyEffectiveness: CSVSourceDefinition;
  electionForecast: CSVSourceDefinition;
  coalitionScenarios: CSVSourceDefinition;
  coalitionAlignment: CSVSourceDefinition;
  genderByParty: CSVSourceDefinition;
  experienceByParty: CSVSourceDefinition;
  ministryEffectiveness: CSVSourceDefinition;
  annualDocTypes: CSVSourceDefinition;
  decisionTrends: CSVSourceDefinition;
  electionRegions: CSVSourceDefinition;
  governmentRoles: CSVSourceDefinition;
  riskEvolution: CSVSourceDefinition;
  behavioralPatterns: CSVSourceDefinition;
}
 
/** A single parsed CSV row (header-keyed, auto-typed values). */
export interface CSVRow {
  [key: string]: string | number;
}
 
/* ── Overview dashboard shapes ── */
 
export interface KeyMetrics {
  totalMPs: number;
  totalParties: number;
  totalRiskRules: number;
  governmentCoalition: string;
  coalitionSeats: number;
  oppositionSeats: number;
  majorityMargin: number;
}
 
export interface RiskAlerts {
  critical: number;
  major: number;
  minor: number;
  last90Days: { critical: number; major: number; minor: number };
}
 
export interface ParliamentActivity {
  votesLastMonth: number;
  documentsProcessed: number;
  motionsSubmitted: number;
  committeeMeetings: number;
}
 
export interface CoalitionStability {
  stabilityScore: number;
  riskLevel: string;
  defectionProbability: number;
  ideologicalTension: string;
}
 
export interface DataQuality {
  completeness: number;
  lastDataSync: string;
  coverage: string;
}
 
export interface OverviewDashboard {
  title: string;
  description: string;
  lastUpdated: string;
  keyMetrics: KeyMetrics;
  riskAlerts: RiskAlerts;
  parliamentActivity: ParliamentActivity;
  coalitionStability: CoalitionStability;
  dataQuality: DataQuality;
  _source: string;
}
 
/* ── Party performance shapes ── */
 
export interface PartyMetricsData {
  seats: number;
  voteShare: number;
  memberCount: number;
  documentsAuthored: number;
  motionsSubmitted: number;
  successRate: number;
}
 
export interface PartyVoting {
  totalVotes: number;
  cohesionScore: number;
  rebellionRate: number;
}
 
export interface PartyTrends {
  supportTrend: string;
  activityTrend: string;
  performanceLevel: string;
}
 
export interface PartyEntry {
  id: string;
  partyName: string;
  shortName: string;
  metrics: PartyMetricsData;
  voting: PartyVoting;
  trends: PartyTrends;
  _source: string;
}
 
export interface PartyPerformance {
  title: string;
  description: string;
  lastUpdated: string;
  parties: PartyEntry[];
  _source: string;
}
 
/* ── Top-10 shapes ── */
 
export interface MPRanking {
  rank: number;
  id: string;
  firstName: string;
  lastName: string;
  party: string;
  role: string;
  influenceScore: number;
  networkConnections: number;
  brokerClassification: string;
  riskLevel: string;
  riskScore: number;
  _source: string;
}
 
export interface Top10Influential {
  title: string;
  description: string;
  lastUpdated: string;
  methodology: string;
  rankings: MPRanking[];
  _source: string;
}
 
/* ── Committee network shapes ── */
 
export interface CommitteeEntry {
  id: string;
  name: string;
  memberCount: number;
  influenceScore: number;
  documentsProcessed: number;
  productivityLevel: string;
  meetingsPerYear: number;
  keyIssues: string[];
  _source: string;
}
 
export interface NetworkNode {
  id: string;
  name: string;
  size: number;
}
 
export interface NetworkEdge {
  source: string;
  target: string;
  weight: number;
  type: string;
}
 
export interface CommitteeNetwork {
  title: string;
  description: string;
  lastUpdated: string;
  committees: CommitteeEntry[];
  networkGraph: { nodes: NetworkNode[]; edges: NetworkEdge[] };
  crossCommitteeMPs: unknown[];
  _source: string;
}
 
/* ── Voting patterns shapes ── */
 
export interface VotingMatrix {
  labels: string[];
  partyNames: string[];
  agreementMatrix: number[][];
}
 
export interface RebellionEntry {
  party: string;
  rebellionRate: number;
  trend: string;
}
 
export interface VotingPatterns {
  title: string;
  description: string;
  lastUpdated: string;
  analysisPeriod: string;
  votingMatrix: VotingMatrix;
  keyIssues: unknown[];
  rebellionTracking: RebellionEntry[];
  _source: string;
}
 
/* ── Election analysis (JSON model) ── */
 
export interface ElectionAnalysis {
  forecast: {
    parties: Array<{
      name: string;
      currentSeats: number;
      predictedSeats: number;
      change: number;
      voteShare: number;
      confidenceInterval?: { min: number; max: number };
    }>;
  };
  coalitionScenarios: Array<{
    name: string;
    composition: string[];
    totalSeats: number;
    probability: number;
    majority: boolean;
    riskLevel: string;
  }>;
  keyFactors: string[];
  electionDate?: string;
}
 
/* ── Ministry dashboard shapes ── */
 
export interface MinistryEntry {
  name: string;
  effectiveness: string;
  documentsProduced: number;
  governmentBills: number;
  year: number;
  quarter: number;
}
 
export interface MinistryDashboard {
  title: string;
  description: string;
  lastUpdated: string;
  ministries: MinistryEntry[];
  _source: string;
}
 
/* ── Demographics shapes ── */
 
export interface GenderEntry {
  party: string;
  gender: string;
  count: number;
}
 
export interface ExperienceEntry {
  party: string;
  experienceLevel: string;
  politicianCount: number;
}
 
export interface DemographicsDashboard {
  title: string;
  description: string;
  lastUpdated: string;
  genderByParty: GenderEntry[];
  experienceByParty: ExperienceEntry[];
  _source: string;
}
 
/* ── Document activity shapes ── */
 
export interface DocumentTypeEntry {
  year: number;
  documentType: string;
  docCount: number;
}
 
export interface DecisionTrendEntry {
  year: number;
  month: number;
  decisionCount: number;
  approvedDecisions: number;
  rejectedDecisions: number;
  approvalRate: number;
}
 
export interface DocumentActivityDashboard {
  title: string;
  description: string;
  lastUpdated: string;
  documentTypes: DocumentTypeEntry[];
  decisionTrends: DecisionTrendEntry[];
  _source: string;
}
 
/* ── Risk evolution shapes ── */
 
export interface RiskEvolutionEntry {
  period: string;
  severity: string;
  politicianCount: number;
  avgRiskScore: number;
}
 
export interface RiskEvolutionDashboard {
  title: string;
  description: string;
  lastUpdated: string;
  entries: RiskEvolutionEntry[];
  _source: string;
}
 
/* ── Aggregate payload ── */
 
export interface CIADataPayload {
  overview: OverviewDashboard;
  election: ElectionAnalysis;
  partyPerf: PartyPerformance;
  top10: Top10Influential;
  committees: CommitteeNetwork;
  votingPatterns: VotingPatterns;
  ministry: MinistryDashboard;
  demographics: DemographicsDashboard;
  documentActivity: DocumentActivityDashboard;
  riskEvolution: RiskEvolutionDashboard;
}
 
/* ------------------------------------------------------------------ */
/*  CIADataLoader class                                               */
/* ------------------------------------------------------------------ */
 
export class CIADataLoader {
  readonly csvBaseURL: string;
  readonly fallbackURL: string;
 
  /** The 8 parties represented in the Swedish Riksdag. */
  static readonly RIKSDAG_PARTIES = ['S', 'M', 'SD', 'C', 'V', 'KD', 'L', 'MP'];
 
  /** Mapping of full Swedish committee names to their Riksdag org codes. */
  static readonly COMMITTEE_ORG_CODES: Record<string, string> = {
    'Konstitutionsutskottet': 'KU',
    'Civilutskottet': 'CU',
    'Trafikutskottet': 'TU',
    'Näringsutskottet': 'NU',
    'Miljö- och jordbruksutskottet': 'MJU',
    'Utrikesutskottet': 'UU',
    'Arbetsmarknadsutskottet': 'AU',
    'Socialförsäkringsutskottet': 'SfU',
    'Socialutskottet': 'SoU',
    'Justitieutskottet': 'JuU',
    'Skatteutskottet': 'SkU',
    'EU-nämnden': 'EUN',
    'Kulturutskottet': 'KrU',
    'Utbildningsutskottet': 'UbU',
    'Finansutskottet': 'FiU',
    'Försvarsutskottet': 'FöU',
    'Lagutskottet': 'LU',
    'Bostadsutskottet': 'BoU'
  };
 
  /**
   * Heuristic divisor to estimate meetings/year from committee document counts.
   * Assumption: ~25 published documents per active committee meeting.
   */
  static readonly COMMITTEE_DOCS_PER_MEETING_ESTIMATE = 25;
 
  constructor() {
    this.csvBaseURL = '../cia-data/';
    this.fallbackURL = 'https://raw.githubusercontent.com/Hack23/cia/master/service.data.impl/sample-data/';
  }
 
  /** CSV data source definitions – maps to real PostgreSQL view exports. */
  static CSV_SOURCES: CSVSourceMap = {
    personStatus: {
      local: 'distribution_person_status.csv',
      description: 'Active MP counts by status'
    },
    riskByParty: {
      local: 'distribution_risk_by_party.csv',
      description: 'Risk levels per party'
    },
    riskLevels: {
      local: 'distribution_politician_risk_levels.csv',
      description: 'Aggregate risk level distribution'
    },
    annualBallots: {
      local: 'voting/distribution_annual_ballots.csv',
      description: 'Annual ballot/vote counts'
    },
    crisisResilience: {
      local: 'risk/distribution_crisis_resilience.csv',
      description: 'Coalition stability/resilience scores'
    },
    partyPerformance: {
      local: 'party/distribution_party_performance.csv',
      description: 'Party metrics (docs, motions, performance level)'
    },
    partyMetrics: {
      local: 'party/view_party_performance_metrics_sample.csv',
      description: 'Full party metrics with win rate, rebel rate, absence rate'
    },
    partyMomentum: {
      local: 'party/distribution_party_momentum.csv',
      description: 'Party trend direction and stability'
    },
    partyMembers: {
      local: 'party/distribution_annual_party_members.csv',
      description: 'Annual party membership counts'
    },
    influenceMetrics: {
      local: 'politician/view_riksdagen_politician_influence_metrics_sample.csv',
      description: 'MP influence scores and network connections'
    },
    riskSummary: {
      local: 'politician/view_politician_risk_summary_sample.csv',
      description: 'MP risk scores and assessments'
    },
    committeeProductivity: {
      local: 'committee/distribution_committee_productivity.csv',
      description: 'Committee productivity and member counts'
    },
    committeeActivity: {
      local: 'committee/distribution_committee_activity.csv',
      description: 'Committee document counts'
    },
    partyEffectiveness: {
      local: 'party/distribution_party_effectiveness_trends.csv',
      description: 'Party effectiveness trends with win rate'
    },
    electionForecast: {
      local: 'election/election_forecast.csv',
      description: 'Election 2026 seat predictions per party'
    },
    coalitionScenarios: {
      local: 'election/coalition_scenarios.csv',
      description: 'Coalition scenario probability modeling'
    },
    coalitionAlignment: {
      local: 'party/distribution_coalition_alignment.csv',
      description: 'Real party-pair voting alignment rates'
    },
    genderByParty: {
      local: 'party/distribution_gender_by_party.csv',
      description: 'Gender distribution per party'
    },
    experienceByParty: {
      local: 'party/distribution_experience_by_party.csv',
      description: 'Experience levels per party'
    },
    ministryEffectiveness: {
      local: 'ministry/distribution_ministry_effectiveness.csv',
      description: 'Ministry effectiveness assessments'
    },
    annualDocTypes: {
      local: 'voting/distribution_annual_document_types.csv',
      description: 'Annual document type counts'
    },
    decisionTrends: {
      local: 'voting/distribution_decision_trends.csv',
      description: 'Decision approval trends over time'
    },
    electionRegions: {
      local: 'election/distribution_election_regions.csv',
      description: 'MPs per election region'
    },
    governmentRoles: {
      local: 'view_riksdagen_goverment_role_member_sample.csv',
      description: 'Government minister role assignments'
    },
    riskEvolution: {
      local: 'distribution_risk_evolution_temporal.csv',
      description: 'Risk score changes over time'
    },
    behavioralPatterns: {
      local: 'party/distribution_behavioral_patterns_by_party.csv',
      description: 'Behavioral risk patterns per party'
    }
  };
 
  /**
   * Parse CSV text into array of objects using header row as keys.
   * @param csvText - Raw CSV text
   * @returns Parsed rows
   */
  parseCSV(csvText: string): CSVRow[] {
    const lines = csvText.trim().split('\n');
    Iif (lines.length < 2) return [];
 
    const headers = lines[0].split(',').map(h => h.trim().replace(/^"|"$/g, ''));
    const rows: CSVRow[] = [];
 
    for (let i = 1; i < lines.length; i++) {
      const line = lines[i].trim();
      Iif (!line) continue;
 
      // Simple CSV parsing (handles basic quoting)
      const values: string[] = [];
      let current = '';
      let inQuotes = false;
      for (let j = 0; j < line.length; j++) {
        const ch = line[j];
        if (ch === '"') {
          inQuotes = !inQuotes;
        } else if (ch === ',' && !inQuotes) {
          values.push(current.trim());
          current = '';
        } else {
          current += ch;
        }
      }
      values.push(current.trim());
 
      const row: CSVRow = {};
      headers.forEach((h, idx) => {
        const val = values[idx] || '';
        const num = Number(val);
        row[h] = val !== '' && !isNaN(num) ? num : val;
      });
      rows.push(row);
    }
    return rows;
  }
 
  /**
   * Load CSV with local-first fallback.
   * @param localPath - Path relative to csvBaseURL
   * @param fallbackPath - Optional fallback path
   * @returns Parsed CSV rows
   */
  async loadCSV(localPath: string, fallbackPath?: string): Promise<CSVRow[]> {
    const urls: string[] = [
      `${this.csvBaseURL}${localPath}`
    ];
    Iif (fallbackPath) {
      urls.push(`${this.fallbackURL}${fallbackPath}`);
    }
 
    for (const url of urls) {
      try {
        const response = await fetch(url);
        if (!response.ok) continue;
        const text = await response.text();
        const rows = this.parseCSV(text);
        Eif (rows.length > 0) return rows;
      } catch (e: unknown) {
        const message = e instanceof Error ? e.message : String(e);
        console.warn(`Failed to load CSV from ${url}:`, message);
      }
    }
 
    console.warn(`No data loaded for ${localPath}`);
    return [];
  }
 
  /**
   * Build overview dashboard from CSV sources.
   * Replaces overview-dashboard.json.
   */
  async loadOverviewDashboard(): Promise<OverviewDashboard> {
    const [personStatus, riskByParty, riskLevels, annualBallots, resilience] = await Promise.all([
      this.loadCSV(CIADataLoader.CSV_SOURCES.personStatus.local),
      this.loadCSV(CIADataLoader.CSV_SOURCES.riskByParty.local),
      this.loadCSV(CIADataLoader.CSV_SOURCES.riskLevels.local),
      this.loadCSV(CIADataLoader.CSV_SOURCES.annualBallots.local),
      this.loadCSV(CIADataLoader.CSV_SOURCES.crisisResilience.local)
    ]);
 
    // Count active MPs
    const activeRow = personStatus.find(r => r.status === 'Tjänstgörande riksdagsledamot');
    const totalMPs = activeRow ? (activeRow.person_count as number) : 349;
 
    // Count unique parties from risk data (only real riksdag parties)
    const riksdagParties = CIADataLoader.RIKSDAG_PARTIES;
    const partiesInData = new Set(
      riskByParty.map(r => r.party as string).filter(p => riksdagParties.includes(p))
    );
    const totalParties = partiesInData.size || 8;
 
    // Risk alerts from risk_by_party
    const highRisk = riskByParty.filter(r => r.risk_level === 'HIGH');
    const medRisk = riskByParty.filter(r => r.risk_level === 'MEDIUM');
    const lowRisk = riskByParty.filter(r => r.risk_level === 'LOW');
    const critical = highRisk.reduce((sum, r) => sum + ((r.politician_count as number) || 0), 0);
    const major = medRisk.reduce((sum, r) => sum + ((r.politician_count as number) || 0), 0);
    const minor = lowRisk.reduce((sum, r) => sum + ((r.politician_count as number) || 0), 0);
 
    // Total risk rules from risk levels
    const totalRiskRules = riskLevels.length > 0
      ? riskLevels.reduce((sum, r) => sum + ((r.politician_count as number) || 0), 0)
      : 45;
 
    // Latest year ballot activity
    const latestBallot: CSVRow = annualBallots.length > 0
      ? annualBallots[annualBallots.length - 1]
      : {};
 
    // Coalition stability from resilience scores (Tidö = M, KD, L, SD)
    const tidoParties = ['M', 'KD', 'L', 'SD'];
    const tidoResilience = resilience.filter(r => tidoParties.includes(r.party as string));
    const avgResilience = tidoResilience.length > 0
      ? Math.round(tidoResilience.reduce((s, r) => s + ((r.avg_resilience_score as number) || 0), 0) / tidoResilience.length)
      : 72;
 
    return {
      title: 'Swedish Riksdag Overview Dashboard',
      description: 'Live intelligence from CIA PostgreSQL database exports',
      lastUpdated: new Date().toISOString(),
      keyMetrics: {
        totalMPs,
        totalParties,
        totalRiskRules,
        governmentCoalition: 'Tidö Agreement',
        coalitionSeats: 176,
        oppositionSeats: 173,
        majorityMargin: 1
      },
      riskAlerts: {
        critical,
        major,
        minor,
        last90Days: { critical, major, minor }
      },
      parliamentActivity: {
        votesLastMonth: (latestBallot.total_votes as number) || 0,
        documentsProcessed: (latestBallot.unique_ballots as number) || 0,
        motionsSubmitted: 0,
        committeeMeetings: 0
      },
      coalitionStability: {
        stabilityScore: avgResilience,
        riskLevel: avgResilience >= 70 ? 'moderate' : 'high',
        defectionProbability: 100 - avgResilience,
        ideologicalTension: avgResilience < 60 ? 'high' : 'moderate'
      },
      dataQuality: {
        completeness: 98.5,
        lastDataSync: new Date().toISOString(),
        coverage: '50+ years (1971-2026)'
      },
      _source: 'csv'
    };
  }
 
  /**
   * Build election analysis from CSV sources.
   * Replaces election-analysis.json.
   */
  async loadElectionAnalysis(): Promise<ElectionAnalysis> {
    const [forecastRows, scenarioRows] = await Promise.all([
      this.loadCSV(CIADataLoader.CSV_SOURCES.electionForecast.local),
      this.loadCSV(CIADataLoader.CSV_SOURCES.coalitionScenarios.local)
    ]);
 
    const toFiniteNumber = (value: unknown): number | undefined => {
      if (typeof value === 'number' && Number.isFinite(value)) return value;
      Iif (typeof value === 'string' && value.trim() !== '') {
        const num = Number(value);
        if (Number.isFinite(num)) return num;
      }
      return undefined;
    };
 
    const toBoolean = (value: unknown): boolean | undefined => {
      Iif (typeof value === 'boolean') return value;
      Eif (typeof value === 'string') {
        const normalized = value.trim().toLowerCase();
        if (normalized === 'true') return true;
        if (normalized === 'false') return false;
      }
      return undefined;
    };
 
    const parties = forecastRows.flatMap(r => {
      const name = String(r.name ?? '').trim();
      const currentSeats = toFiniteNumber(r.currentSeats);
      const predictedSeats = toFiniteNumber(r.predictedSeats);
      const change = toFiniteNumber(r.change);
      const voteShare = toFiniteNumber(r.voteShare);
 
      if (!name || currentSeats === undefined || predictedSeats === undefined || change === undefined || voteShare === undefined) {
        return [];
      }
 
      const confidenceMin = toFiniteNumber(r.confidenceMin);
      const confidenceMax = toFiniteNumber(r.confidenceMax);
 
      return [{
        name,
        currentSeats,
        predictedSeats,
        change,
        voteShare,
        confidenceInterval:
          confidenceMin !== undefined && confidenceMax !== undefined
            ? { min: confidenceMin, max: confidenceMax }
            : undefined
      }];
    });
 
    const coalitionScenarios = scenarioRows.flatMap(r => {
      const name = String(r.name ?? '').trim();
      const probability = toFiniteNumber(r.probability);
      const totalSeats = toFiniteNumber(r.totalSeats);
      const majority = toBoolean(r.majority);
      const riskLevel = String(r.riskLevel ?? '').trim();
      const composition = String(r.composition ?? '')
        .split(',')
        .map(s => s.trim())
        .filter(Boolean);
 
      if (
        !name ||
        probability === undefined ||
        totalSeats === undefined ||
        majority === undefined ||
        !riskLevel ||
        composition.length === 0
      ) {
        return [];
      }
 
      return [{
        name,
        probability,
        composition,
        totalSeats,
        majority,
        riskLevel
      }];
    });
 
    return {
      forecast: { parties },
      coalitionScenarios,
      keyFactors: [
        'Economic conditions',
        'Immigration policy',
        'Climate change priorities',
        'Healthcare reform',
        'NATO membership impact'
      ],
      electionDate: '2026-09-13'
    };
  }
 
  /**
   * Build party performance from CSV sources.
   * Replaces party-performance.json.
   */
  async loadPartyPerformance(): Promise<PartyPerformance> {
    const [performance, metrics, momentum] = await Promise.all([
      this.loadCSV(CIADataLoader.CSV_SOURCES.partyPerformance.local),
      this.loadCSV(CIADataLoader.CSV_SOURCES.partyMetrics.local),
      this.loadCSV(CIADataLoader.CSV_SOURCES.partyMomentum.local)
    ]);
 
    // Only include real riksdag parties
    const riksdagParties = CIADataLoader.RIKSDAG_PARTIES;
    const activePerformance = performance.filter(p => riksdagParties.includes(p.party as string));
 
    // Build a lookup from the detailed metrics
    const metricsMap: Record<string, CSVRow> = {};
    metrics.forEach(m => {
      if (riksdagParties.includes(m.party as string)) {
        metricsMap[m.party as string] = m;
      }
    });
 
    // Get latest momentum per party
    const latestMomentum: Record<string, CSVRow> = {};
    momentum
      .filter(m => riksdagParties.includes(m.party as string))
      .forEach(m => {
        const party = m.party as string;
        if (
          !latestMomentum[party] ||
          (m.year as number) > (latestMomentum[party].year as number) ||
          ((m.year as number) === (latestMomentum[party].year as number) &&
            (m.quarter as number) > (latestMomentum[party].quarter as number))
        ) {
          latestMomentum[party] = m;
        }
      });
 
    // Known seat counts (from 2022 election results)
    const seatMap: Record<string, number> = {
      S: 107, SD: 73, M: 68, C: 24, V: 24, KD: 19, L: 16, MP: 18
    };
 
    const parties: PartyEntry[] = activePerformance.map(p => {
      const party = p.party as string;
      const m = metricsMap[party] || {};
      const mom = latestMomentum[party] || {};
 
      return {
        id: party,
        partyName: (p.party_name as string) || party,
        shortName: party,
        metrics: {
          seats: seatMap[party] || 0,
          voteShare: 0,
          memberCount: (p.active_members as number) || 0,
          documentsAuthored: (p.documents_last_year as number) || 0,
          motionsSubmitted: (p.motions_last_year as number) || 0,
          successRate: (m.avg_win_rate as number) || 0
        },
        voting: {
          totalVotes: (m.total_votes_last_year as number) || 0,
          cohesionScore: (m.avg_participation_rate as number) || 0,
          rebellionRate: (m.avg_rebel_rate as number) || 0
        },
        trends: {
          supportTrend: ((mom.trend_direction as string) || 'stable').toLowerCase(),
          activityTrend: ((mom.stability_classification as string) || 'stable').toLowerCase(),
          performanceLevel: (m.performance_level as string) || (p.performance_level as string) || ''
        },
        _source: 'csv'
      };
    });
 
    // Sort by seats descending
    parties.sort((a, b) => (b.metrics.seats || 0) - (a.metrics.seats || 0));
 
    return {
      title: 'Party Performance Dashboard',
      description: 'Live party data from CIA PostgreSQL database exports',
      lastUpdated: new Date().toISOString(),
      parties,
      _source: 'csv'
    };
  }
 
  /**
   * Build top 10 influential MPs from CSV sources.
   * Replaces top10-influential-mps.json.
   */
  async loadTop10Influential(): Promise<Top10Influential> {
    const [influence, riskSummary] = await Promise.all([
      this.loadCSV(CIADataLoader.CSV_SOURCES.influenceMetrics.local),
      this.loadCSV(CIADataLoader.CSV_SOURCES.riskSummary.local)
    ]);
 
    // Build risk lookup by person_id
    const riskMap: Record<string, CSVRow> = {};
    riskSummary.forEach(r => {
      riskMap[r.person_id as string] = r;
    });
 
    // Sort by network_connections descending, take top 10
    const sorted = [...influence]
      .filter(mp => (mp.network_connections as number) > 0)
      .sort((a, b) => ((b.network_connections as number) || 0) - ((a.network_connections as number) || 0))
      .slice(0, 10);
 
    const rankings: MPRanking[] = sorted.map((mp, idx) => {
      const risk = riskMap[mp.person_id as string] || {};
      return {
        rank: idx + 1,
        id: String(mp.person_id),
        firstName: (mp.first_name as string) || '',
        lastName: (mp.last_name as string) || '',
        party: (mp.party as string) || '',
        role: (mp.influence_classification as string)
          ? (mp.influence_classification as string)
              .replace(/_/g, ' ')
              .toLowerCase()
              .replace(/\b\w/g, (c: string) => c.toUpperCase())
          : '',
        influenceScore: (mp.network_connections as number) || 0,
        networkConnections: (mp.network_connections as number) || 0,
        brokerClassification: (mp.broker_classification as string) || '',
        riskLevel: (risk.risk_level as string) || '',
        riskScore: (risk.risk_score as number) || 0,
        _source: 'csv'
      };
    });
 
    return {
      title: 'Top 10 Most Influential MPs',
      description: 'Network analysis from CIA politician influence metrics view',
      lastUpdated: new Date().toISOString(),
      methodology: 'Ranked by network_connections from view_riksdagen_politician_influence_metrics',
      rankings,
      _source: 'csv'
    };
  }
 
  /**
   * Build committee network from CSV sources.
   * Replaces committee-network.json.
   * Filters out INACTIVE committees and deduplicates by name.
   */
  async loadCommitteeNetwork(): Promise<CommitteeNetwork> {
    const [productivity, activity] = await Promise.all([
      this.loadCSV(CIADataLoader.CSV_SOURCES.committeeProductivity.local),
      this.loadCSV(CIADataLoader.CSV_SOURCES.committeeActivity.local)
    ]);
 
    // Build activity lookup by org code
    const activityMap: Record<string, number> = {};
    activity.forEach(a => {
      activityMap[a.org as string] = (a.document_count as number) || 0;
    });
 
    // Use class-level committee name-to-org-code mapping
    const nameToOrgCode = CIADataLoader.COMMITTEE_ORG_CODES;
 
    // Deduplicate committees by name, keeping the entry with the most data
    const bestByName: Record<string, CSVRow> = {};
    productivity.forEach(c => {
      const name = c.committee_name as string;
      if (!name) return;
      const existing = bestByName[name];
      if (!existing || (c.total_documents as number) > (existing.total_documents as number) ||
          ((c.total_documents as number) === (existing.total_documents as number) &&
           (c.total_members as number) > (existing.total_members as number))) {
        bestByName[name] = c;
      }
    });
 
    // Normalize committee metrics first so filtering and rendering use one consistent source.
    const committees: CommitteeEntry[] = Object.values(bestByName)
      .map(c => {
        const name = c.committee_name as string;
        const code = nameToOrgCode[name] || name.substring(0, 3).toUpperCase();
        const totalDocuments = (c.total_documents as number) || 0;
        const activityDocs = activityMap[code] || 0;
        const documentsProcessed = Math.max(totalDocuments, activityDocs);
        const productivityLevel = (c.productivity_level as string) || '';
        return {
          id: code,
          name,
          memberCount: (c.total_members as number) || 0,
          influenceScore: c.docs_per_member
            ? Math.round((c.docs_per_member as number) * 100)
            : 0,
          documentsProcessed,
          productivityLevel,
          meetingsPerYear:
            documentsProcessed > 0
              ? Math.round(
                  documentsProcessed / CIADataLoader.COMMITTEE_DOCS_PER_MEETING_ESTIMATE
                )
              : 0,
          keyIssues: [productivityLevel || 'N/A'],
          _source: 'csv'
        };
      })
      .filter(c => {
        // Keep real committees only; skip generic node and inactive rows with no measured output.
        return (
          c.name !== 'Riksdagen' &&
          c.memberCount > 0 &&
          (c.productivityLevel !== 'INACTIVE' || c.documentsProcessed > 0)
        );
      })
      .sort((a, b) => b.documentsProcessed - a.documentsProcessed);
 
    // Build simple network graph from committees
    const nodes: NetworkNode[] = committees.map(c => ({
      id: c.id,
      name: c.name,
      size: c.influenceScore
    }));
 
    // Create edges between committees that share similar productivity levels
    const edges: NetworkEdge[] = [];
    for (let i = 0; i < committees.length; i++) {
      for (let j = i + 1; j < committees.length && edges.length < 10; j++) {
        if (
          committees[i].productivityLevel === committees[j].productivityLevel &&
          committees[i].productivityLevel !== 'INACTIVE'
        ) {
          edges.push({
            source: committees[i].id,
            target: committees[j].id,
            weight: Math.min(committees[i].documentsProcessed, committees[j].documentsProcessed),
            type: 'productivity_similarity'
          });
        }
      }
    }
 
    return {
      title: 'Committee Network Analysis',
      description: 'Committee data from CIA committee productivity view',
      lastUpdated: new Date().toISOString(),
      committees,
      networkGraph: { nodes, edges },
      crossCommitteeMPs: [],
      _source: 'csv'
    };
  }
 
  /**
   * Build voting patterns from CSV sources.
   * Uses real coalition alignment data for the agreement matrix
   * and party effectiveness trends for win rates.
   */
  async loadVotingPatterns(): Promise<VotingPatterns> {
    const [coalitionAlignment, effectiveness, riskByParty] = await Promise.all([
      this.loadCSV(CIADataLoader.CSV_SOURCES.coalitionAlignment.local),
      this.loadCSV(CIADataLoader.CSV_SOURCES.partyEffectiveness.local),
      this.loadCSV(CIADataLoader.CSV_SOURCES.riskByParty.local)
    ]);
 
    const riksdagParties = CIADataLoader.RIKSDAG_PARTIES;
    const labels = riksdagParties;
    const partyNames = [
      'Social Democrats', 'Moderates', 'Sweden Democrats', 'Centre',
      'Left', 'Christian Democrats', 'Liberals', 'Green'
    ];
 
    // Build agreement matrix from real coalition alignment data
    const alignmentLookup: Record<string, number> = {};
    coalitionAlignment
      .filter(r => riksdagParties.includes(r.party1 as string) && riksdagParties.includes(r.party2 as string))
      .forEach(r => {
        const key1 = `${r.party1}:${r.party2}`;
        const key2 = `${r.party2}:${r.party1}`;
        const rate = Math.round(((r.alignment_rate as number) || 0) * 100);
        alignmentLookup[key1] = rate;
        alignmentLookup[key2] = rate;
      });
 
    // If real alignment data is available, use it; otherwise fall back to effectiveness-based
    const hasAlignmentData = Object.keys(alignmentLookup).length > 0;
 
    let agreementMatrix: number[][];
    if (hasAlignmentData) {
      agreementMatrix = labels.map(p1 =>
        labels.map(p2 => {
          if (p1 === p2) return 100;
          return alignmentLookup[`${p1}:${p2}`] ?? 50;
        })
      );
    } else {
      // Fallback: build from win rate similarity
      const latestWinRate: Record<string, CSVRow> = {};
      effectiveness
        .filter(e => riksdagParties.includes(e.party as string))
        .forEach(e => {
          const party = e.party as string;
          Eif (
            !latestWinRate[party] ||
            (e.year as number) > (latestWinRate[party].year as number) ||
            ((e.year as number) === (latestWinRate[party].year as number) &&
              (e.quarter as number) > (latestWinRate[party].quarter as number))
          ) {
            latestWinRate[party] = e;
          }
        });
      agreementMatrix = labels.map(p1 => {
        const wr1 = latestWinRate[p1] ? (latestWinRate[p1].avg_win_rate as number) : 50;
        return labels.map(p2 => {
          if (p1 === p2) return 100;
          const wr2 = latestWinRate[p2] ? (latestWinRate[p2].avg_win_rate as number) : 50;
          return Math.max(0, Math.round(100 - Math.abs(wr1 - wr2)));
        });
      });
    }
 
    // Rebellion tracking from risk data (HIGH risk ~ rebellious)
    const rebellionTracking: RebellionEntry[] = riksdagParties
      .map(party => {
        const partyRisks = riskByParty.filter(r => r.party === party);
        const highRisk = partyRisks.find(r => r.risk_level === 'HIGH');
        const total = partyRisks.reduce((s, r) => s + ((r.politician_count as number) || 0), 0);
        const highCount = highRisk ? (highRisk.politician_count as number) : 0;
        const rebellionRate = total > 0 ? Math.round((highCount / total) * 100 * 10) / 10 : 0;
        return {
          party,
          rebellionRate,
          trend: rebellionRate > 25 ? 'increasing' : rebellionRate > 15 ? 'stable' : 'decreasing'
        };
      })
      .filter(r => r.rebellionRate > 0);
 
    return {
      title: 'Voting Patterns Analysis',
      description: hasAlignmentData
        ? 'Real coalition alignment data from CIA voting analysis'
        : 'Derived from CIA party effectiveness trends and risk data',
      lastUpdated: new Date().toISOString(),
      analysisPeriod: '2022-2026',
      votingMatrix: { labels, partyNames, agreementMatrix },
      keyIssues: [],
      rebellionTracking,
      _source: 'csv'
    };
  }
 
  /**
   * Build ministry dashboard from CSV sources.
   */
  async loadMinistryDashboard(): Promise<MinistryDashboard> {
    const rows = await this.loadCSV(CIADataLoader.CSV_SOURCES.ministryEffectiveness.local);
 
    const ministries: MinistryEntry[] = rows
      .filter(r => r.ministry_name && (r.documents_produced as number) > 0)
      .map(r => ({
        name: r.ministry_name as string,
        effectiveness: (r.effectiveness_assessment as string) || '',
        documentsProduced: (r.documents_produced as number) || 0,
        governmentBills: (r.government_bills as number) || 0,
        year: (r.year as number) || 0,
        quarter: (r.quarter as number) || 0
      }))
      .sort((a, b) => b.documentsProduced - a.documentsProduced);
 
    return {
      title: 'Ministry Performance',
      description: 'Ministry effectiveness from CIA database exports',
      lastUpdated: new Date().toISOString(),
      ministries,
      _source: 'csv'
    };
  }
 
  /**
   * Build demographics dashboard from CSV sources.
   */
  async loadDemographics(): Promise<DemographicsDashboard> {
    const riksdagParties = CIADataLoader.RIKSDAG_PARTIES;
    const [genderRows, experienceRows] = await Promise.all([
      this.loadCSV(CIADataLoader.CSV_SOURCES.genderByParty.local),
      this.loadCSV(CIADataLoader.CSV_SOURCES.experienceByParty.local)
    ]);
 
    const genderByParty: GenderEntry[] = genderRows
      .filter(r => riksdagParties.includes(r.party as string))
      .map(r => ({
        party: r.party as string,
        gender: r.gender as string,
        count: (r.count as number) || 0
      }));
 
    const experienceByParty: ExperienceEntry[] = experienceRows
      .filter(r => riksdagParties.includes(r.party as string))
      .map(r => ({
        party: r.party as string,
        experienceLevel: (r.experience_level as string) || '',
        politicianCount: (r.politician_count as number) || 0
      }));
 
    return {
      title: 'Parliamentary Demographics',
      description: 'Gender and experience distribution from CIA database exports',
      lastUpdated: new Date().toISOString(),
      genderByParty,
      experienceByParty,
      _source: 'csv'
    };
  }
 
  /**
   * Build document activity dashboard from CSV sources.
   */
  async loadDocumentActivity(): Promise<DocumentActivityDashboard> {
    const [docTypeRows, decisionRows] = await Promise.all([
      this.loadCSV(CIADataLoader.CSV_SOURCES.annualDocTypes.local),
      this.loadCSV(CIADataLoader.CSV_SOURCES.decisionTrends.local)
    ]);
 
    const documentTypes: DocumentTypeEntry[] = docTypeRows
      .filter(r => (r.doc_count as number) > 0)
      .map(r => ({
        year: (r.year as number) || 0,
        documentType: (r.document_type as string) || '',
        docCount: (r.doc_count as number) || 0
      }));
 
    const decisionTrends: DecisionTrendEntry[] = decisionRows
      .filter(r => (r.decision_count as number) > 0)
      .map(r => ({
        year: (r.year as number) || 0,
        month: (r.month as number) || 0,
        decisionCount: (r.decision_count as number) || 0,
        approvedDecisions: (r.approved_decisions as number) || 0,
        rejectedDecisions: (r.rejected_decisions as number) || 0,
        approvalRate: (r.approval_rate as number) || 0
      }));
 
    return {
      title: 'Parliamentary Document Activity',
      description: 'Document production and decision trends from CIA database exports',
      lastUpdated: new Date().toISOString(),
      documentTypes,
      decisionTrends,
      _source: 'csv'
    };
  }
 
  /**
   * Build risk evolution dashboard from CSV sources.
   */
  async loadRiskEvolution(): Promise<RiskEvolutionDashboard> {
    const rows = await this.loadCSV(CIADataLoader.CSV_SOURCES.riskEvolution.local);
 
    const entries: RiskEvolutionEntry[] = rows
      .filter(r => (r.politician_count as number) > 0)
      .map(r => ({
        period: (r.assessment_period as string) || '',
        severity: (r.risk_severity as string) || '',
        politicianCount: (r.politician_count as number) || 0,
        avgRiskScore: (r.avg_risk_score as number) || 0
      }));
 
    return {
      title: 'Risk Score Evolution',
      description: 'Temporal risk score changes from CIA database exports',
      lastUpdated: new Date().toISOString(),
      entries,
      _source: 'csv'
    };
  }
 
  /**
   * Load all data in parallel.
   * @returns Object with all data
   */
  async loadAll(): Promise<CIADataPayload> {
    const [overview, election, partyPerf, top10, committees, votingPatterns, ministry, demographics, documentActivity, riskEvolution] =
      await Promise.all([
        this.loadOverviewDashboard(),
        this.loadElectionAnalysis(),
        this.loadPartyPerformance(),
        this.loadTop10Influential(),
        this.loadCommitteeNetwork(),
        this.loadVotingPatterns(),
        this.loadMinistryDashboard(),
        this.loadDemographics(),
        this.loadDocumentActivity(),
        this.loadRiskEvolution()
      ]);
 
    return {
      overview,
      election,
      partyPerf,
      top10,
      committees,
      votingPatterns,
      ministry,
      demographics,
      documentActivity,
      riskEvolution
    };
  }
}