All files / scripts/data-transformers content-generators.ts

69.65% Statements 684/982
55.16% Branches 539/977
37.26% Functions 117/314
69.29% Lines 650/938

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 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765                                                                        16x 8x       4x                         186x         24x 24x 24x 7x 7x 1x 7x           24x 24x                   8x 8x 5x 5x     5x 5x 5x 5x 20x 20x     5x 5x   5x       105x   105x 105x 105x   105x     105x               268x   105x 24x   24x   24x 24x 24x     24x 24x       24x           24x 24x 24x 2x                           2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x   2x                   2x           105x 6x                               6x     6x 11x 11x   11x 11x       6x 11x 11x 11x 11x       11x 11x 11x 11x   11x 11x 11x 11x   11x         105x 3x                           3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x         105x 8x                           8x 8x 8x 8x 8x 8x 8x   8x 8x 8x 8x         8x           8x 8x 8x 8x 8x 8x         105x 4x   4x 5x     4x       105x 105x 105x       105x 105x 105x 105x   105x 36x 36x     36x 36x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   36x 36x     36x     36x 6x 6x 11x   6x 6x 6x 1x                           6x 6x     6x         36x 10x 10x 2x                           10x 10x     10x     36x     105x       85x   85x   85x 3x 3x       82x 82x 160x 160x 160x     82x     82x 82x     85x     85x     85x 160x     160x     160x               160x 160x 160x 160x     160x     160x 160x 160x 160x       160x 160x 160x   160x                         85x 85x 82x       82x 82x     160x 82x 82x     85x 85x       85x 85x     85x 160x 85x 75x 75x 10x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x   75x 75x     75x     82x     82x 85x 2x           2x 1x                           2x 2x 2x     2x 2x       82x 85x 2x           2x 1x                           2x 2x 2x     2x 2x       82x 85x 1x               1x                             1x 1x 1x     1x 1x 1x 1x 1x 1x 1x         82x       38x   38x   38x 2x 2x       36x 36x     38x     38x     38x 38x   38x 39x 6x     6x   39x   39x 56x 56x 56x     56x     56x 56x 56x 56x         56x 56x       56x 56x 56x   56x                       38x 38x     38x 56x 38x     38x 38x 56x 56x     38x 38x     38x     38x 39x 3x 38x 9x 9x 9x 2x                           9x 9x     9x     36x     36x 36x 36x       36x 36x     39x 36x 7x                           36x 36x     38x     38x 31x 31x 6x                           31x 31x     31x     36x     36x 36x     36x 36x                                             36x 36x     38x                                         36x 36x                                         36x       61x   61x   61x 2x 2x       59x 59x     61x     61x 61x 102x 102x 102x       78x 61x 19x 19x 19x     19x   19x       59x   59x 4x 4x   4x 4x 4x 4x 4x 4x   4x 6x 6x               59x   61x 57x 2x       57x 57x 96x 96x 96x 96x   57x   57x       34x 34x 72x 72x   72x 72x     72x         24x         59x   34x 34x 34x   34x 34x 34x 53x 53x 53x     53x     34x       59x 61x                               59x       59x 59x 13x     46x 46x       46x 46x 126x 126x 126x       46x 46x 6x 6x       46x 46x 59x     59x 34x       59x 9x 9x                             37x 5x 5x                               32x 32x       46x   46x     46x             46x 52x 52x 52x 52x 52x       52x     52x 126x 126x 126x       126x   126x 126x 126x 126x 28x   126x             126x 46x 2x 2x 5x 5x 5x   4x 2x 2x 2x         126x 46x 2x 2x 4x 4x   4x 2x 2x           2x 2x 2x 2x 2x   4x   2x         126x 46x 1x 1x 2x 2x   1x 1x   1x 1x 1x 1x                             1x 1x     1x 1x         46x 46x 46x       46x 46x     46x 52x 52x 52x     52x   46x 46x 59x 46x       46x 126x 46x 126x   46x 38x 38x   46x 28x           28x 28x       28x 28x         46x 30x 30x 7x       2x                   30x 30x     30x     46x   46x                                                   16x                                             218x 218x               15x 15x 15x       45x 15x 45x     15x 15x 15x 15x 15x 15x 15x 15x 15x   15x 15x   15x 15x               15x   15x 14x 14x 14x 14x     1x   1x 3x 3x 3x 3x 3x 3x 3x 3x   1x 1x               15x 15x   15x         15x 15x 15x               15x 15x 15x     15x 15x 15x 11x 3x       1x                   15x 15x 15x       15x 15x 15x 15x 15x     15x 15x                   19x   19x 19x     15x 15x 15x 15x   15x                   15x   15x 15x 15x     15x 1x                           1x     1x                             1x 1x     1x   1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x         15x                                                                                         15x                                                                                                                                                                             15x    
/**
 * @module data-transformers/content-generators
 * @description Article content generators that transform structured
 * parliamentary data into narrative HTML. Each generator handles a
 * specific article type: week-ahead, committee reports, propositions,
 * motions, and generic (weekly/monthly review, breaking).
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import { escapeHtml } from '../html-utils.js';
import type { Language } from '../types/language.js';
import type { ArticleContentData, WeekAheadData, RawDocument, MonthlyMetrics, RawCalendarEvent } from './types.js';
import { getPillarTransition } from '../editorial-pillars.js';
import {
  L,
  svSpan,
  sanitizeUrl,
  isHighPriority,
  formatDayName,
  getCommitteeName,
  generateEnhancedSummary,
  normalizePartyKey,
} from './helpers.js';
import { detectPolicyDomains, generatePolicySignificance, generateDeepPolicyAnalysis } from './policy-analysis.js';
import {
  groupMotionsByProposition,
  groupPropositionsByCommittee,
  generateOppositionStrategySection,
  renderMotionEntry,
  generateDocumentIntelligenceAnalysis,
  PROP_TITLE_SUFFIX_REGEX,
} from './document-analysis.js';
 
/** Per-language title-suffix templates for inverted-pyramid lede construction. */
const TITLE_SUFFIX_TEMPLATES: Readonly<Record<string, (t: string) => string>> = {
  sv: t => ` — inklusive "${t}"`,
  da: t => ` — herunder "${t}"`,
  no: t => ` — inkludert "${t}"`,
  fi: t => ` — mukaan lukien "${t}"`,
  de: t => ` — darunter "${t}"`,
  fr: t => ` — notamment "${t}"`,
  es: t => ` — incluyendo "${t}"`,
  nl: t => ` — inclusief "${t}"`,
  ar: t => ` — بما فيها "${t}"`,
  he: t => ` — כולל "${t}"`,
  ja: t => `、「${t}」を含む`,
  ko: t => `, "${t}" 포함`,
  zh: t => `,包括"${t}"`,
};
 
/** Extract meaningful keywords from text for cross-reference matching (min 2 chars, captures EU, KU, etc.; splits on whitespace, hyphens, and commas) */
function extractKeywords(text: string): string[] {
  return text.toLowerCase().split(/[\s,–-]+/u).filter(w => w.length >= 2);
}
 
/** Find documents related to a calendar event by organ match or keyword overlap (max 3) */
function findRelatedDocuments(event: RawCalendarEvent, documents: RawDocument[]): RawDocument[] {
  const eventOrgan = event.organ ?? '';
  const keywords = extractKeywords(event.rubrik ?? event.titel ?? event.title ?? '');
  return documents.filter(doc => {
    const docOrgan = doc.organ ?? doc.committee ?? '';
    if (eventOrgan && docOrgan && eventOrgan.toLowerCase() === docOrgan.toLowerCase()) return true;
    const docText = (doc.titel ?? doc.title ?? '').toLowerCase();
    return keywords.some(kw => docText.includes(kw));
  }).slice(0, 3);
}
 
/** Find written questions related to a calendar event by keyword overlap (max 3) */
function findRelatedQuestions(event: RawCalendarEvent, questions: RawDocument[]): RawDocument[] {
  const keywords = extractKeywords(event.rubrik ?? event.titel ?? event.title ?? '');
  return questions.filter(q => {
    const qText = (q.titel ?? q.title ?? '').toLowerCase();
    return keywords.some(kw => qText.includes(kw));
  }).slice(0, 3);
}
 
/** Extract targeted minister name from interpellation summary "till MINISTER" header line.
 *  Strips trailing topic clauses ("om X", "angående Y", etc.) and punctuation. */
function extractMinister(summary: string): string {
  // Use non-newline whitespace ([^\S\n]+) so we don't cross into the next line
  const m = summary.match(/\btill[^\S\n]+([^\n]+)/i);
  if (!m) return '';
  const raw = m[1].trim();
  Iif (!raw) return '';
 
  // Remove common trailing topic clauses and punctuation
  const lowerRaw = raw.toLowerCase();
  const stopPhrases = [' om ', ' angående ', ' rörande ', ' beträffande '];
  let end = raw.length;
  for (const phrase of stopPhrases) {
    const idx = lowerRaw.indexOf(phrase);
    Iif (idx !== -1 && idx < end) end = idx;
  }
  // Cut at terminating punctuation if it comes earlier
  const punctIdx = raw.search(/[?:;.,]/);
  Iif (punctIdx !== -1 && punctIdx < end) end = punctIdx;
 
  return raw.slice(0, end).trim();
}
 
export function generateWeekAheadContent(data: WeekAheadData, lang: Language | string): string {
  const { events, highlights, context } = data;
  // Cast to ArticleContentData to access documents field (passed via switch cast)
  const documents = (data as unknown as ArticleContentData).documents ?? [];
  const questions = data.questions ?? [];
  const interpellations = data.interpellations ?? [];
 
  let content = '';
 
  // Introduction section
  content += `
    <div class="context-box">
      <h3>${L(lang, 'whyMatters')}</h3>
      <p>${context || L(lang, 'whyMattersDefault')}</p>
    </div>
`;
 
  // Group events by significance
  const highPriority = events.filter(e => isHighPriority(e));
 
  if (highPriority.length > 0) {
    content += `\n    <h2>${L(lang, 'keyEvents')}</h2>\n`;
 
    highPriority.forEach(event => {
      // Derive dayName from event date if not present
      const dayName = event.dayName || (event.datum || event.from || event.start ? formatDayName(new Date(event.datum || event.from || event.start || ''), lang) : '');
      const eventTime = event.time || event.tid || 'Expected';
      const eventTitle = event.title || event.titel || 'Event';
 
      // Mark Swedish API titles for LLM translation post-processing
      const escapedEventTitle = escapeHtml(eventTitle);
      const titleHtml = (event.titel && !event.title)
        ? svSpan(escapedEventTitle, lang)
        : escapedEventTitle;
 
      content += `
    <h3>${dayName ? dayName + ' - ' : ''}${titleHtml}</h3>
    <p>${event.description || `${eventTime}: ${event.details || 'Parliamentary session scheduled.'}`}</p>
`;
 
      // Policy Context: cross-reference related documents and questions per event
      const relatedPolicyDocs = findRelatedDocuments(event, documents);
      const relatedPolicyQs = findRelatedQuestions(event, questions);
      if (relatedPolicyDocs.length > 0 || relatedPolicyQs.length > 0) {
        const policyContextLabel = lang === 'sv' ? 'Policysammanhang'
          : lang === 'de' ? 'Politischer Kontext'
          : lang === 'fr' ? 'Contexte politique'
          : lang === 'es' ? 'Contexto político'
          : lang === 'da' ? 'Politisk kontekst'
          : lang === 'no' ? 'Politisk kontekst'
          : lang === 'fi' ? 'Poliittinen konteksti'
          : lang === 'nl' ? 'Beleidscontext'
          : lang === 'ar' ? 'السياق السياسي'
          : lang === 'he' ? 'הקשר מדיניות'
          : lang === 'ja' ? '政策コンテキスト'
          : lang === 'ko' ? '정책 맥락'
          : lang === 'zh' ? '政策背景'
          : 'Policy Context';
        content += `    <div class="policy-context-box">\n`;
        content += `      <h4>${policyContextLabel}</h4>\n`;
        relatedPolicyDocs.forEach(doc => {
          const drec = doc as Record<string, string>;
          const docTitle = drec['titel'] ?? drec['title'] ?? 'Document';
          const dokId = drec['dok_id'] ?? '';
          const docUrl = dokId ? sanitizeUrl(`https://riksdagen.se/sv/dokument-och-lagar/dokument/${encodeURIComponent(dokId)}/`) : '';
          content += `      <div class="document-entry">\n`;
          content += `        <h5>${docUrl ? `<a href="${docUrl}" target="_blank" rel="noopener noreferrer">` : ''}${svSpan(escapeHtml(docTitle), lang)}${docUrl ? '</a>' : ''}</h5>\n`;
          const sig = generatePolicySignificance(doc, lang);
          Eif (sig) content += `        <p class="policy-significance">${escapeHtml(sig)}</p>\n`;
          content += `      </div>\n`;
        });
        relatedPolicyQs.forEach(q => {
          const qrec = q as Record<string, string>;
          const qTitle = qrec['titel'] ?? qrec['title'] ?? 'Question';
          const qParty = qrec['parti'] ? ` (${escapeHtml(qrec['parti'])})` : '';
          const qDokId = qrec['dok_id'] ?? '';
          const qUrl = qDokId ? sanitizeUrl(`https://riksdagen.se/sv/dokument-och-lagar/dokument/${encodeURIComponent(qDokId)}/`) : '';
          content += `      <div class="document-entry">\n`;
          content += `        <h5>${qUrl ? `<a href="${qUrl}" target="_blank" rel="noopener noreferrer">` : ''}${svSpan(escapeHtml(qTitle), lang)}${qUrl ? '</a>' : ''}${qParty}</h5>\n`;
          content += `      </div>\n`;
        });
        content += `    </div>\n`;
      }
    });
  }
 
  // Legislative Pipeline: show upcoming documents when calendar is sparse or empty
  if (documents.length > 0) {
    const sectionLabel = lang === 'sv'
      ? 'Kommande i den lagstiftande processen'
      : lang === 'de' ? 'Bevorstehende legislative Tagesordnung'
      : lang === 'fr' ? 'Agenda législatif à venir'
      : lang === 'es' ? 'Agenda legislativa próxima'
      : lang === 'da' ? 'Kommende lovgivningsmæssig dagsorden'
      : lang === 'no' ? 'Kommende lovgivningsmessig agenda'
      : lang === 'fi' ? 'Tuleva lainsäädäntöohjelma'
      : lang === 'nl' ? 'Komende wetgevende agenda'
      : lang === 'ar' ? 'جدول الأعمال التشريعي القادم'
      : lang === 'he' ? 'סדר היום החקיקתי הקרוב'
      : lang === 'ja' ? '今後の立法スケジュール'
      : lang === 'ko' ? '향후 입법 일정'
      : lang === 'zh' ? '未来立法议程'
      : 'Upcoming Legislative Agenda';
 
    content += `\n    <h2>${sectionLabel}</h2>\n`;
 
    // Show top documents — prioritise propositions and committee reports
    const priorityDocs = [
      ...documents.filter(d => (d as Record<string, string>).doktyp === 'prop' || (d as Record<string, string>).doktyp === 'proposition'),
      ...documents.filter(d => (d as Record<string, string>).doktyp === 'bet' || (d as Record<string, string>).doktyp === 'betankande'),
      ...documents.filter(d => {
        const t = (d as Record<string, string>).doktyp;
        return t !== 'prop' && t !== 'proposition' && t !== 'bet' && t !== 'betankande';
      }),
    ].slice(0, 15);
 
    priorityDocs.forEach(doc => {
      const rec = doc as Record<string, string>;
      const titleText = rec['titel'] || rec['title'] || rec['doktyp'] || 'Document';
      const escapedTitle = escapeHtml(titleText);
      const titleHtml = (rec['titel'] && !rec['title'])
        ? svSpan(escapedTitle, lang)
        : escapedTitle;
 
      const significance = generatePolicySignificance(doc, lang);
      const dokId = rec['dok_id'] ?? rec['id'] ?? '';
      const urlBase = 'https://riksdagen.se/sv/dokument-och-lagar/dokument/';
      const safeUrl = dokId ? sanitizeUrl(`${urlBase}${encodeURIComponent(dokId)}/`) : '';
 
      content += `\n    <div class="document-entry">\n`;
      content += `      <h4>${safeUrl ? `<a href="${safeUrl}" target="_blank" rel="noopener noreferrer">` : ''}${titleHtml}${safeUrl ? '</a>' : ''}</h4>\n`;
      Eif (significance) {
        content += `      <p class="policy-significance">${escapeHtml(significance)}</p>\n`;
      }
      content += `    </div>\n`;
    });
  }
 
  // Questions to Watch: upcoming written questions cross-referenced with debate topics
  if (questions.length > 0) {
    const questionsLabel = lang === 'sv' ? 'Frågor att bevaka'
      : lang === 'de' ? 'Zu beobachtende Anfragen'
      : lang === 'fr' ? 'Questions à surveiller'
      : lang === 'es' ? 'Preguntas a seguir'
      : lang === 'da' ? 'Spørgsmål at holde øje med'
      : lang === 'no' ? 'Spørsmål å følge med på'
      : lang === 'fi' ? 'Seurattavat kysymykset'
      : lang === 'nl' ? 'Te volgen vragen'
      : lang === 'ar' ? 'أسئلة تستحق المتابعة'
      : lang === 'he' ? 'שאלות לעקוב'
      : lang === 'ja' ? '注目の質問'
      : lang === 'ko' ? '주목할 질문'
      : lang === 'zh' ? '值得关注的问题'
      : 'Questions to Watch';
    content += `\n    <h2>${questionsLabel}</h2>\n`;
    questions.slice(0, 8).forEach(q => {
      const rec = q as Record<string, string>;
      const titleText = rec['titel'] || rec['title'] || 'Question';
      const party = rec['parti'] ? ` (${escapeHtml(rec['parti'])})` : '';
      const dok_id = rec['dok_id'] ?? '';
      const qUrl = dok_id ? sanitizeUrl(`https://riksdagen.se/sv/dokument-och-lagar/dokument/${encodeURIComponent(dok_id)}/`) : '';
      content += `    <div class="document-entry">\n`;
      content += `      <h4>${qUrl ? `<a href="${qUrl}" target="_blank" rel="noopener noreferrer">` : ''}${svSpan(escapeHtml(titleText), lang)}${qUrl ? '</a>' : ''}</h4>\n`;
      Eif (party) content += `      <p class="policy-significance">${escapeHtml(party)}</p>\n`;
      content += `    </div>\n`;
    });
  }
 
  // Interpellation Spotlight: formal interpellations enriched with minister response context
  if (interpellations.length > 0) {
    const interLabel = lang === 'sv' ? 'Interpellationer i fokus'
      : lang === 'de' ? 'Interpellationen im Fokus'
      : lang === 'fr' ? 'Interpellations en vedette'
      : lang === 'es' ? 'Interpelaciones destacadas'
      : lang === 'da' ? 'Forespørgsler i fokus'
      : lang === 'no' ? 'Interpellasjoner i fokus'
      : lang === 'fi' ? 'Välikysymykset valokeilassa'
      : lang === 'nl' ? 'Interpellaties in de spotlight'
      : lang === 'ar' ? 'أبرز الاستجوابات البرلمانية'
      : lang === 'he' ? 'בקשות הבהרה בזרקור'
      : lang === 'ja' ? '注目の質問主意書'
      : lang === 'ko' ? '주목할 대정부 질문'
      : lang === 'zh' ? '质询聚焦'
      : 'Interpellation Spotlight';
    content += `\n    <h2>${interLabel}</h2>\n`;
    interpellations.slice(0, 8).forEach(interp => {
      const rec = interp as Record<string, string>;
      const titleText = rec['titel'] || rec['title'] || 'Interpellation';
      const party = rec['parti'] ? ` (${escapeHtml(rec['parti'])})` : '';
      const dok_id = rec['dok_id'] ?? '';
      const iUrl = dok_id ? sanitizeUrl(`https://riksdagen.se/sv/dokument-och-lagar/dokument/${encodeURIComponent(dok_id)}/`) : '';
      // Extract minister and clean summary from the header lines
      const rawSummary = rec['summary'] ?? '';
      const ministerName = extractMinister(rawSummary);
      const tillMatch = rawSummary.match(/\btill\s+[^\n]+\n\s*/i);
      const contentStart = tillMatch
        ? rawSummary.indexOf(tillMatch[0]) + tillMatch[0].length
        : rawSummary.replace(/^Interpellation\s+\S+[^\n]*\n\s*/i, '').replace(/^\s*av\s+[^\n]+\n\s*/i, '').length === rawSummary.length
          ? 0
          : 0;
      const cleanedSummary = (tillMatch ? rawSummary.slice(contentStart) : rawSummary
        .replace(/^Interpellation\s+\S+[^\n]*\n\s*/i, '')
        .replace(/^\s*av\s+[^\n]+\n\s*/i, '')
        .replace(/^\s*till\s+[^\n]+\n\s*/i, ''))
        .trim()
        .slice(0, 200);
      content += `    <div class="document-entry">\n`;
      content += `      <h4>${iUrl ? `<a href="${iUrl}" target="_blank" rel="noopener noreferrer">` : ''}${svSpan(escapeHtml(titleText), lang)}${iUrl ? '</a>' : ''}</h4>\n`;
      Eif (party) content += `      <p class="policy-significance">${escapeHtml(party)}</p>\n`;
      if (ministerName) content += `      <p class="minister-target">→ ${svSpan(escapeHtml(ministerName), lang)}</p>\n`;
      if (cleanedSummary) content += `      <p>${svSpan(escapeHtml(cleanedSummary) + '…', lang)}</p>\n`;
      content += `    </div>\n`;
    });
  }
 
  // Additional context
  if (highlights && highlights.length > 0) {
    content += `\n    <h2>${L(lang, 'whatToWatch')}</h2>\n    <ul>\n`;
 
    highlights.forEach(highlight => {
      content += `      <li><strong>${highlight.title}:</strong> ${highlight.description}</li>\n`;
    });
 
    content += '    </ul>\n';
  }
 
  // Narrative bridge to analytical outlook (inter-pillar transition)
  const aheadTransition = getPillarTransition(lang, 'pulseToWatch');
  Eif (aheadTransition) {
    content += `    <p class="pillar-transition">${escapeHtml(aheadTransition)}</p>\n`;
  }
 
  // ── Key takeaways: synthesize all data sources for the week ──────────────
  const hasEventData = highPriority.length > 0;
  const hasDocData = documents.length > 0;
  const hasQData = questions.length > 0;
  const hasInterpData = interpellations.length > 0;
 
  if (hasEventData || hasDocData || hasQData || hasInterpData) {
    content += `\n    <h2>${L(lang, 'keyTakeaways')}</h2>\n`;
    content += `    <div class="context-box">\n      <ul>\n`;
 
    // Activity summary takeaway
    const itemCount = highPriority.length + documents.length + questions.length + interpellations.length;
    const activitySummaryTemplates: Record<string, (n: number) => string> = {
      sv: n => `Denna period innehåller ${n} ärenden som spänner över debatter, lagförslag, skriftliga frågor och interpellationer.`,
      da: n => `Denne periode omfatter ${n} emner på tværs af debatter, lovforslag, skriftlige spørgsmål og forespørgsler.`,
      no: n => `Denne perioden omfatter ${n} saker som spenner over debatter, lovforslag, skriftlige spørsmål og interpellasjoner.`,
      fi: n => `Tämä ajanjakso sisältää ${n} asiaa, jotka kattavat keskusteluja, lakiehdotuksia, kirjallisia kysymyksiä ja välikysymyksiä.`,
      de: n => `Dieser Zeitraum umfasst ${n} Themen in den Bereichen Debatten, Gesetzentwürfe, schriftliche Anfragen und Interpellationen.`,
      fr: n => `Cette période comprend ${n} sujets couvrant débats, propositions de loi, questions écrites et interpellations.`,
      es: n => `Este período incluye ${n} temas que abarcan debates, proyectos de ley, preguntas escritas e interpelaciones.`,
      nl: n => `Deze periode omvat ${n} onderwerpen over debatten, wetsvoorstellen, schriftelijke vragen en interpellaties.`,
      ar: n => `تشمل هذه الفترة ${n} بندًا تتراوح بين المناقشات ومشاريع القوانين والأسئلة المكتوبة والاستجوابات.`,
      he: n => `תקופה זו כוללת ${n} נושאים הכוללים דיונים, הצעות חוק, שאלות כתובות ואינטרפלציות.`,
      ja: n => `この期間には、討論・法案・書面質問・質問主意書を含む${n}件の議題があります。`,
      ko: n => `이 기간에는 토론, 법안, 서면 질문, 대정부 질문을 포괄하는 ${n}건의 의제가 있습니다.`,
      zh: n => `本期涵盖${n}个议题,横跨辩论、法案、书面质询和质询。`,
    };
    const actTpl = activitySummaryTemplates[lang as string];
    const activitySummary = actTpl
      ? actTpl(itemCount)
      : `This period features ${itemCount} items spanning debates, legislative proposals, written questions, and interpellations.`;
    content += `        <li>${escapeHtml(activitySummary)}</li>\n`;
 
    // Policy domain cross-analysis from documents
    if (documents.length > 0) {
      const weekDomains = new Set<string>();
      documents.forEach(doc => {
        detectPolicyDomains(doc, lang).forEach(d => weekDomains.add(d));
      });
      Eif (weekDomains.size > 0) {
        const domainList = Array.from(weekDomains).slice(0, 4).join(', ');
        const domainSummaryTemplates: Record<string, (d: string) => string> = {
          sv: d => `Den lagstiftande dagordningen berör ${d} — ett brett politiskt fokus denna period.`,
          da: d => `Den lovgivningsmæssige dagsorden berører ${d} — et bredt politisk fokus i denne periode.`,
          no: d => `Den lovgivningsmessige agendaen berører ${d} — et bredt politisk fokus denne perioden.`,
          fi: d => `Lainsäädäntöohjelma kattaa ${d} — laaja poliittinen painopiste tällä kaudella.`,
          de: d => `Die gesetzgeberische Tagesordnung berührt ${d} — ein breiter politischer Fokus in diesem Zeitraum.`,
          fr: d => `L'agenda législatif touche ${d} — un large spectre politique cette période.`,
          es: d => `La agenda legislativa toca ${d} — un amplio enfoque político en este período.`,
          nl: d => `De wetgevende agenda raakt ${d} — een breed politiek focus in deze periode.`,
          ar: d => `يغطي جدول الأعمال التشريعي ${d} — تركيز سياسي واسع في هذه الفترة.`,
          he: d => `סדר היום החקיקתי נוגע ב${d} — מוקד פוליטי רחב בתקופה זו.`,
          ja: d => `立法アジェンダは${d}に及び、この期間の幅広い政策的焦点を示しています。`,
          ko: d => `입법 안건은 ${d}에 걸쳐 있으며, 이 기간의 광범위한 정책 초점을 나타냅니다.`,
          zh: d => `立法议程涉及${d}——显示本期广泛的政策关注。`,
        };
        const domTpl = domainSummaryTemplates[lang as string];
        const domainSummary = domTpl
          ? domTpl(escapeHtml(domainList))
          : `The legislative agenda touches on ${escapeHtml(domainList)} — a broad policy focus this period.`;
        content += `        <li>${domainSummary}</li>\n`;
      }
    }
 
    // Parliamentary scrutiny indicator
    if (questions.length > 0 || interpellations.length > 0) {
      const scrutinyCount = questions.length + interpellations.length;
      const scrutinyTemplates: Record<string, (n: number) => string> = {
        sv: n => `${n} parlamentariska granskningsåtgärder (frågor och interpellationer) signalerar aktiv oppositionsövervakning.`,
        da: n => `${n} parlamentariske kontrolforanstaltninger signalerer aktiv oppositionsovervågning.`,
        no: n => `${n} parlamentariske kontrolltiltak signaliserer aktiv overvåking fra opposisjonen.`,
        fi: n => `${n} parlamentaarista valvontatoimenpidettä signaloi aktiivista oppositiovalvontaa.`,
        de: n => `${n} parlamentarische Kontrollmaßnahmen signalisieren aktive Oppositionsüberwachung.`,
        fr: n => `${n} mesures de contrôle parlementaire signalent une surveillance active de l'opposition.`,
        es: n => `${n} medidas de control parlamentario señalan una supervisión activa de la oposición.`,
        nl: n => `${n} parlementaire controlemaatregelen signaleren actief oppositietoezicht.`,
        ar: n => `${n} إجراءات رقابة برلمانية تشير إلى مراقبة نشطة من المعارضة.`,
        he: n => `${n} אמצעי פיקוח פרלמנטריים מסמנים מעקב פעיל של האופוזיציה.`,
        ja: n => `${n}件の議会監視措置は、野党による積極的な監視を示しています。`,
        ko: n => `${n}건의 의회 감시 조치는 야당의 적극적인 감시를 나타냅니다.`,
        zh: n => `${n}项议会监督措施表明反对派正在积极监督。`,
      };
      const scrTpl = scrutinyTemplates[lang as string];
      const scrutinySummary = scrTpl
        ? scrTpl(scrutinyCount)
        : `${scrutinyCount} parliamentary scrutiny measures (questions and interpellations) signal active opposition oversight.`;
      content += `        <li>${escapeHtml(scrutinySummary)}</li>\n`;
    }
 
    content += `      </ul>\n    </div>\n`;
  }
 
  return content;
}
 
export function generateCommitteeContent(data: ArticleContentData, lang: Language | string): string {
  const reports = data.reports || [];
 
  let content = `<h2>${L(lang, 'latestReports')}</h2>\n`;
 
  if (reports.length === 0) {
    content += `<p>${L(lang, 'noReports')}</p>\n`;
    return content;
  }
 
  // Group reports by committee for thematic coherence
  const byCommittee: Record<string, RawDocument[]> = {};
  reports.forEach(report => {
    const committee = report.organ || report.committee || 'unknown';
    Eif (!byCommittee[committee]) byCommittee[committee] = [];
    byCommittee[committee].push(report);
  });
 
  const committeeCount = Object.keys(byCommittee).length;
 
  // Analytical lede: contextual overview of committee activity
  const breakdown = L(lang, 'committeeBreakdown') as string | ((n: number, c: number) => string);
  const breakdownText = typeof breakdown === 'function'
    ? breakdown(reports.length, committeeCount)
    : `${reports.length} committee reports across ${committeeCount} committees.`;
  content += `<p class="article-lede">${escapeHtml(String(breakdownText))}</p>\n`;
 
  // Thematic analysis section header
  content += `\n    <h2>${L(lang, 'thematicAnalysis')}</h2>\n`;
 
  // Generate content for each committee group with analysis
  Object.entries(byCommittee).forEach(([committeeCode, committeeReports]) => {
    const committeeName = getCommitteeName(committeeCode, lang);
 
    // Committee section header
    content += `\n    <h3>${escapeHtml(committeeName)}</h3>\n`;
 
    // Add committee context: how many reports from this committee
    Iif (committeeReports.length > 1) {
      const countContextFn = L(lang, 'committeeCountContext') as string | ((n: number) => string);
      const countContext = typeof countContextFn === 'function'
        ? countContextFn(committeeReports.length)
        : `${committeeReports.length} reports from this committee signal intensive legislative work within its portfolio.`;
      content += `    <p><em>${escapeHtml(String(countContext))}</em></p>\n`;
    }
 
    committeeReports.forEach(report => {
      const titleText = report.titel || report.title || '';
      const escapedTitle = escapeHtml(titleText);
      const titleHtml = (report.titel && !report.title)
        ? svSpan(escapedTitle, lang)
        : escapedTitle;
      const docName = escapeHtml(report.dokumentnamn || report.dok_id || titleText);
 
      // Use enriched summary or enhanced summary from metadata
      const summaryText = generateEnhancedSummary(report, 'report', lang);
      const isFromAPI = report.summary || report.notis;
      const reportDefaultVal = L(lang, 'reportDefault');
      const summaryHtml = (report.titel && !report.title && isFromAPI && summaryText !== reportDefaultVal)
        ? svSpan(escapeHtml(summaryText), lang)
        : escapeHtml(summaryText);
 
      const reportSigVal = L(lang, 'reportSignificance');
      const readFullVal = L(lang, 'readFullReport');
      const whatThisMeansVal = L(lang, 'whatThisMeans');
 
      content += `
    <div class="report-entry">
      <h4>${titleHtml}</h4>
      <p><strong>${L(lang, 'committee')}:</strong> ${escapeHtml(committeeName)}</p>
      <p>${escapeHtml(String(reportSigVal))} ${summaryHtml}</p>
      <p><strong>${escapeHtml(String(whatThisMeansVal))}:</strong> ${generateDeepPolicyAnalysis(report, lang, 'bet')}</p>
      <p><a href="${sanitizeUrl(report.url)}" class="document-link" rel="noopener noreferrer">${escapeHtml(String(readFullVal))}: ${docName}</a></p>
    </div>
`;
    });
  });
 
  // Narrative bridge from legislative content to analytical outlook (inter-pillar transition)
  const pulseTransition = getPillarTransition(lang, 'pulseToWatch');
  if (pulseTransition) {
    content += `    <p class="pillar-transition">${escapeHtml(pulseTransition)}</p>\n`;
  }
 
  // Key takeaways section
  content += `\n    <h2>${L(lang, 'keyTakeaways')}</h2>\n`;
  content += `    <div class="context-box">\n      <ul>\n`;
 
  // Generate analytical takeaways based on committees covered
  const committeeNames = Object.keys(byCommittee).map(c => getCommitteeName(c, lang));
  const activityFn = L(lang, 'committeeActivityTakeaway') as string | ((committees: string, extra: number) => string);
  const takeaway1 = typeof activityFn === 'function'
    ? activityFn(committeeNames.slice(0, 3).join(', '), committeeCount > 3 ? committeeCount - 3 : 0)
    : `Parliamentary committees have been active across ${committeeNames.slice(0, 3).join(', ')}.`;
  const momentumFn = L(lang, 'committeeMomentumTakeaway') as string | ((n: number) => string);
  const takeaway2 = typeof momentumFn === 'function'
    ? momentumFn(reports.length)
    : `A total of ${reports.length} reports demonstrates sustained legislative momentum.`;
 
  content += `        <li>${escapeHtml(takeaway1)}</li>\n`;
  content += `        <li>${escapeHtml(takeaway2)}</li>\n`;
 
  // Cross-committee domain analysis: identify which policy areas span multiple committees
  const allDomains = new Set<string>();
  reports.forEach(r => { detectPolicyDomains(r, lang).forEach(d => allDomains.add(d)); });
  if (allDomains.size > 0) {
    const domainList = Array.from(allDomains).slice(0, 3).join(', ');
    const crossAnalysisTemplates: Record<string, (d: string) => string> = {
      sv: (d) => `Betänkandena berör ${d} – ett mönster som tyder på breda lagstiftningsprioriteringar denna session.`,
      da: (d) => `Betænkningerne berører ${d} — et mønster, der signalerer brede lovgivningsprioriteringer.`,
      no: (d) => `Innstillingene berører ${d} — et mønster som signaliserer brede lovgivningsprioriteringer.`,
      fi: (d) => `Mietinnöt kattavat ${d} — laaja-alainen malli, joka osoittaa hallituksen lainsäädäntöprioriteetit.`,
      de: (d) => `Die Berichte betreffen ${d} — ein Muster, das die breiten Gesetzgebungsprioritäten signalisiert.`,
      fr: (d) => `Les rapports couvrent ${d} — un schéma indiquant les larges priorités législatives.`,
      es: (d) => `Los informes abarcan ${d} — un patrón que indica las amplias prioridades legislativas.`,
      nl: (d) => `De rapporten bestrijken ${d} — een patroon dat de brede wetgevende prioriteiten signaleert.`,
      ar: (d) => `تغطي التقارير ${d} — نمط يشير إلى أولويات تشريعية واسعة.`,
      he: (d) => `הדוחות מקיפים ${d} — תבנית המסמנת סדרי עדיפויות חקיקתיים רחבים.`,
      ja: (d) => `報告書は${d}に及び、幅広い立法優先事項を示しています。`,
      ko: (d) => `보고서는 ${d}에 걸쳐 있으며, 광범위한 입법 우선순위를 나타냅니다.`,
      zh: (d) => `报告涉及${d}——显示出广泛的立法优先事项。`,
    };
    const crossTpl = crossAnalysisTemplates[lang as string];
    const crossAnalysis = crossTpl
      ? crossTpl(escapeHtml(domainList))
      : `Reports span ${escapeHtml(domainList)} — a cross-committee pattern signalling the government's broad legislative priorities this session.`;
    content += `        <li>${crossAnalysis}</li>\n`;
  }
 
  content += `      </ul>\n    </div>\n`;
 
  // ── Optional: Voting Results section ─────────────────────────────────────
  const votes = (data.votes ?? []) as unknown[];
  if (votes.length > 0) {
    const votingSectionHeaders: Record<string, string> = {
      sv: 'Röstningsresultat', da: 'Afstemningsresultater', no: 'Voteringsresultater',
      fi: 'Äänestystulokset', de: 'Abstimmungsergebnisse', fr: 'Résultats du vote',
      es: 'Resultados de la votación', nl: 'Stemresultaten', ar: 'نتائج التصويت',
      he: 'תוצאות ההצבעה', ja: '投票結果', ko: '투표 결과', zh: '投票结果',
    };
    const votingCountTemplates: Record<string, (n: number) => string> = {
      sv: (n) => `${n} röstningsprotokoll visar hur partierna röstade i utskottsbeslut denna period.`,
      da: (n) => `${n} afstemningsprotokoller viser, hvordan partierne stemte om udvalgets beslutninger.`,
      no: (n) => `${n} voteringsprotokoll viser hvordan partiene stemte i komitévedtak.`,
      fi: (n) => `${n} äänestysrekisteriä osoittaa, miten puolueet äänestivät valiokunnan päätöksistä.`,
      de: (n) => `${n} Abstimmungsrekorde zeigen, wie die Parteien über Ausschussbeschlüsse abstimmten.`,
      fr: (n) => `${n} procès-verbaux de vote montrent comment les partis ont voté sur les décisions de commission.`,
      es: (n) => `${n} registros de votación muestran cómo votaron los partidos en las decisiones de la comisión.`,
      nl: (n) => `${n} stemregisters tonen hoe partijen stemden over commissiebeslissingen.`,
      ar: (n) => `${n} سجلات التصويت تظهر كيف صوتت الأحزاب على قرارات اللجنة.`,
      he: (n) => `${n} פרוטוקולי הצבעה מציגים כיצד הצביעו המפלגות על החלטות הוועדה.`,
      ja: (n) => `${n}件の投票記録が、委員会決定に対する各党の投票方法を示しています。`,
      ko: (n) => `${n}건의 투표 기록이 위원회 결정에 대한 각 정당의 투표 방식을 보여줍니다.`,
      zh: (n) => `${n}条投票记录显示各党派对委员会决定的投票情况。`,
    };
    const votingHeader = votingSectionHeaders[lang as string] ?? 'Voting Results';
    const votingCountFn = votingCountTemplates[lang as string];
    const votingCountText = votingCountFn
      ? votingCountFn(votes.length)
      : `${votes.length} voting records show how parties voted on committee decisions this period.`;
    content += `\n    <h2>${escapeHtml(votingHeader)}</h2>\n`;
    content += `    <p>${escapeHtml(votingCountText)}</p>\n`;
  }
 
  // ── Optional: Committee Debate section ───────────────────────────────────
  const speeches = (data.speeches ?? []) as unknown[];
  if (speeches.length > 0) {
    const debateSectionHeaders: Record<string, string> = {
      sv: 'Utskottsdebatt', da: 'Udvalgets debat', no: 'Komitédebatt',
      fi: 'Valiokunnan keskustelu', de: 'Ausschussdebatte', fr: 'Débat en commission',
      es: 'Debate en comisión', nl: 'Commissiedebat', ar: 'نقاش اللجنة',
      he: 'דיון בוועדה', ja: '委員会討論', ko: '위원회 토론', zh: '委员会讨论',
    };
    const debateCountTemplates: Record<string, (n: number) => string> = {
      sv: (n) => `${n} anföranden i kammaren belyser de viktigaste argumenten och partipositionerna i dessa frågor.`,
      da: (n) => `${n} parlamentariske taler belyser nøgleargumenter og partipositioner.`,
      no: (n) => `${n} parlamentariske innlegg belyser nøkkelargumenter og partiposisjoner.`,
      fi: (n) => `${n} parlamentaarista puheenvuoroa valaisee keskeisiä argumentteja ja puolueiden kantoja.`,
      de: (n) => `${n} parlamentarische Reden beleuchten Hauptargumente und Parteipositionen.`,
      fr: (n) => `${n} discours parlementaires éclairent les arguments clés et les positions des partis.`,
      es: (n) => `${n} discursos parlamentarios iluminan los principales argumentos y posiciones de los partidos.`,
      nl: (n) => `${n} parlementaire toespraken belichten de belangrijkste argumenten en partijposities.`,
      ar: (n) => `${n} خطاب برلماني يسلط الضوء على الحجج الرئيسية ومواقف الأحزاب.`,
      he: (n) => `${n} נאומים פרלמנטריים מאירים טיעונים מרכזיים ועמדות מפלגות.`,
      ja: (n) => `${n}件の議会演説が主要な論点と各党の立場を明らかにしています。`,
      ko: (n) => `${n}건의 의회 연설이 주요 논점과 각 정당의 입장을 보여줍니다.`,
      zh: (n) => `${n}篇议会演讲揭示了主要论点和各党派立场。`,
    };
    const debateHeader = debateSectionHeaders[lang as string] ?? 'Committee Debate';
    const debateCountFn = debateCountTemplates[lang as string];
    const debateCountText = debateCountFn
      ? debateCountFn(speeches.length)
      : `${speeches.length} parliamentary speeches highlight key arguments and party positions on these issues.`;
    content += `\n    <h2>${escapeHtml(debateHeader)}</h2>\n`;
    content += `    <p>${escapeHtml(debateCountText)}</p>\n`;
  }
 
  // ── Optional: Government Bill Linkage section ─────────────────────────────
  const propositions = (data.propositions ?? []) as RawDocument[];
  if (propositions.length > 0) {
    const billSectionHeaders: Record<string, string> = {
      sv: 'Koppling till regeringspropositioner', da: 'Tilknytning til regeringsforslag',
      no: 'Tilknytning til regjeringsproposisjoner', fi: 'Yhteys hallituksen esityksiin',
      de: 'Verknüpfung mit Regierungsvorlagen', fr: 'Lien avec les projets de loi gouvernementaux',
      es: 'Vinculación con proyectos de ley gubernamentales', nl: 'Koppeling aan regeringsvoorstellen',
      ar: 'الصلة بمشاريع قوانين الحكومة', he: 'קישור להצעות חוק ממשלתיות',
      ja: '政府法案との連携', ko: '정부 법안과의 연계', zh: '与政府法案的关联',
    };
    const billCountTemplates: Record<string, (n: number) => string> = {
      sv: (n) => `${n} regeringspropositioner är kopplade till dessa betänkanden och visar lagstiftningskedjan.`,
      da: (n) => `${n} regeringsforslag er knyttet til disse betænkninger og viser den lovgivningsmæssige kæde.`,
      no: (n) => `${n} regjeringsproposisjoner er knyttet til disse innstillingene og viser den legislative kjeden.`,
      fi: (n) => `${n} hallituksen esitystä liittyy näihin mietintöihin ja osoittaa lainsäädäntöketjun.`,
      de: (n) => `${n} Regierungsvorlagen sind mit diesen Berichten verknüpft und zeigen die Gesetzgebungskette.`,
      fr: (n) => `${n} projets de loi gouvernementaux sont liés à ces rapports, montrant la chaîne législative.`,
      es: (n) => `${n} proyectos de ley gubernamentales están vinculados a estos informes, mostrando la cadena legislativa.`,
      nl: (n) => `${n} regeringsvoorstellen zijn gekoppeld aan deze rapporten en tonen de wetgevingsketen.`,
      ar: (n) => `${n} مشاريع قوانين حكومية مرتبطة بهذه التقارير، مما يُظهر السلسلة التشريعية.`,
      he: (n) => `${n} הצעות חוק ממשלתיות קשורות לדוחות אלה, ומציגות את השרשרת החקיקתית.`,
      ja: (n) => `${n}件の政府法案がこれらの報告書に関連しており、立法プロセスの連鎖を示しています。`,
      ko: (n) => `${n}건의 정부 법안이 이 보고서들과 연계되어 입법 과정의 연결고리를 보여줍니다.`,
      zh: (n) => `${n}项政府法案与这些报告相关,展示了立法链条。`,
    };
    const billHeader = billSectionHeaders[lang as string] ?? 'Government Bill Linkage';
    const billCountFn = billCountTemplates[lang as string];
    const billCountText = billCountFn
      ? billCountFn(propositions.length)
      : `${propositions.length} government propositions are linked to these reports, tracing the full legislative chain.`;
    content += `\n    <h2>${escapeHtml(billHeader)}</h2>\n`;
    content += `    <p>${escapeHtml(billCountText)}</p>\n`;
    propositions.slice(0, 3).forEach(prop => { // display up to 3 linked propositions
      Iif (typeof prop !== 'object' || prop === null) return;
      const propTitle = escapeHtml((prop as RawDocument).titel || (prop as RawDocument).title || (prop as RawDocument).dokumentnamn || '');
      Eif (propTitle) {
        content += `    <p>→ ${propTitle}</p>\n`;
      }
    });
  }
 
  return content;
}
 
export function generatePropositionsContent(data: ArticleContentData, lang: Language | string): string {
  const propositions = data.propositions || [];
 
  let content = `<h2>${L(lang, 'govProps')}</h2>\n`;
 
  if (propositions.length === 0) {
    content += `<p>${L(lang, 'noProps')}</p>\n`;
    return content;
  }
 
  // Analytical lede paragraph
  const breakdownFn = L(lang, 'propsBreakdown') as string | ((n: number) => string);
  const breakdownText = typeof breakdownFn === 'function'
    ? breakdownFn(propositions.length)
    : `${propositions.length} new government propositions submitted.`;
  content += `<p class="article-lede">${escapeHtml(String(breakdownText))}</p>\n`;
 
  // Legislative pipeline section
  content += `\n    <h2>${L(lang, 'legislativePipeline')}</h2>\n`;
 
  // Group propositions by committee; multi-committee → h3 committee + h4 prop, single → h3 prop
  const byCommitteeGroup = groupPropositionsByCommittee(propositions);
  const multiCommittee = byCommitteeGroup.size > 1;
 
  byCommitteeGroup.forEach((committeeProps, committeeKey) => {
    if (multiCommittee) {
      const committeeLabel = committeeKey
        ? escapeHtml(getCommitteeName(committeeKey, lang))
        : escapeHtml(String(L(lang, 'otherCommittee')));
      content += `    <h3>${committeeLabel}</h3>\n`;
    }
    const headingTag = multiCommittee ? 'h4' : 'h3';
 
    committeeProps.forEach(prop => {
      const titleText = prop.titel || prop.title || '';
      const escapedTitle = escapeHtml(titleText);
      const titleHtml = (prop.titel && !prop.title)
        ? svSpan(escapedTitle, lang)
        : escapedTitle;
      const docName = escapeHtml(prop.dokumentnamn || prop.dok_id || titleText);
 
      // Use enhanced summary based on metadata
      const summaryText = generateEnhancedSummary(prop, 'proposition', lang);
      const isFromAPI = prop.summary || prop.notis;
      const propDefaultVal = L(lang, 'propDefault');
      const summaryHtml = (prop.titel && !prop.title && isFromAPI && summaryText !== propDefaultVal)
        ? svSpan(escapeHtml(summaryText), lang)
        : escapeHtml(summaryText);
 
      // Show "Referred to" inline only in single-committee view (committee heading covers it otherwise)
      const referredCommittee = prop.organ || prop.committee;
      const referredLine = (!multiCommittee && referredCommittee)
        ? `<br><strong>${L(lang, 'referredTo')}:</strong> ${escapeHtml(getCommitteeName(referredCommittee, lang))}`
        : '';
 
      const propSigVal = L(lang, 'propSignificance');
      const readFullVal = L(lang, 'readFullProp');
      const whyItMattersVal = L(lang, 'whyItMatters');
 
      content += `
    <div class="proposition-entry">
      <${headingTag}>${titleHtml}</${headingTag}>
      <p>${escapeHtml(String(propSigVal))} ${summaryHtml}${referredLine}</p>
      <p><strong>${escapeHtml(String(whyItMattersVal))}:</strong> ${generateDeepPolicyAnalysis(prop, lang, 'prop')}</p>
      <p><a href="${sanitizeUrl(prop.url)}" class="document-link" rel="noopener noreferrer">${escapeHtml(String(readFullVal))}: ${docName}</a></p>
    </div>
`;
    });
  });
 
  // Policy implications section
  content += `\n    <h2>${L(lang, 'policyImplications')}</h2>\n`;
  content += `    <div class="context-box">\n`;
 
  // Count unique policy domains across all propositions for accurate "N policy domains" text
  const allPropDomains = new Set<string>();
  propositions.forEach(p => detectPolicyDomains(p, lang).forEach(d => allPropDomains.add(d)));
  const domainCount = allPropDomains.size;
 
  // Group by referred committee for government priority signal (separate from domain count)
  const byCommittee: Record<string, number> = {};
  propositions.forEach(p => {
    const c = p.organ || p.committee || 'unknown';
    byCommittee[c] = (byCommittee[c] || 0) + 1;
  });
 
  const implicationFn = L(lang, 'policyImplicationsContext') as string | ((propCount: number, domainCount: number) => string);
  const implication = typeof implicationFn === 'function'
    ? implicationFn(propositions.length, domainCount)
    : `These ${propositions.length} propositions touch on ${domainCount} policy domains.`;
  content += `      <p>${escapeHtml(String(implication))}</p>\n`;
 
  // Government priority signal: identify the committee receiving the most propositions
  const sortedCommittees = Object.entries(byCommittee)
    .filter(([c]) => c !== 'unknown')
    .sort(([, a], [, b]) => b - a);
  if (sortedCommittees.length > 0) {
    const [topCommittee, topCount] = sortedCommittees[0];
    const topName = getCommitteeName(topCommittee, lang);
    const priorityTemplates: Record<string, (n: string, c: number) => string> = {
      sv: (n, c) => `${n} tar emot ${c} av propositionerna – ett tecken på att detta är ett centralt prioriterat område för regeringen denna session.`,
      da: (n, c) => `${n} modtager ${c} af lovforslagene — et klart signal om regeringsprioritet.`,
      no: (n, c) => `${n} mottar ${c} av proposisjonene — et sterkt signal om regjeringsprioritet.`,
      fi: (n, c) => `${n} vastaanottaa ${c} esityksistä — vahva merkki hallituksen painopistealueesta.`,
      de: (n, c) => `${n} erhält ${c} der Vorlagen — ein starkes Signal für die Regierungspriorität in diesem Bereich.`,
      fr: (n, c) => `${n} reçoit ${c} des propositions — un signal fort de priorité gouvernementale.`,
      es: (n, c) => `${n} recibe ${c} de las proposiciones — una señal clara de prioridad gubernamental.`,
      nl: (n, c) => `${n} ontvangt ${c} van de voorstellen — een sterk signaal van overheidsprioriteit.`,
      ar: (n, c) => `${n} يستقبل ${c} من المقترحات — إشارة قوية لأولوية حكومية.`,
      he: (n, c) => `${n} מקבל ${c} מההצעות — אות חזק לעדיפות ממשלתית.`,
      ja: (n, c) => `${n}は${c}件の提案を受け取り、政府の重点分野であることを示しています。`,
      ko: (n, c) => `${n}이(가) ${c}건의 법안을 받아 정부 우선순위를 강하게 나타냅니다.`,
      zh: (n, c) => `${n}收到${c}项提案——强烈表明这是政府本期的优先领域。`,
    };
    const priorityTpl = priorityTemplates[lang as string];
    const priorityNote = priorityTpl
      ? priorityTpl(escapeHtml(topName), topCount)
      : `${escapeHtml(topName)} receives ${topCount} of the propositions — a strong signal of government priority in this policy area this session.`;
    content += `      <p>${priorityNote}</p>\n`;
  }
 
  content += `    </div>\n`;
 
  // Narrative bridge to analytical outlook (inter-pillar transition)
  const propTransition = getPillarTransition(lang, 'pulseToWatch');
  Eif (propTransition) {
    content += `    <p class="pillar-transition">${escapeHtml(propTransition)}</p>\n`;
  }
 
  // ── Key takeaways: synthesize propositions batch ──────────────────────────
  content += `\n    <h2>${L(lang, 'keyTakeaways')}</h2>\n`;
  content += `    <div class="context-box">\n      <ul>\n`;
 
  // Propositions batch overview
  const committeeCountProp = Object.keys(byCommittee).filter(c => c !== 'unknown').length;
  const propOverviewTemplates: Record<string, (p: number, c: number) => string> = {
    sv: (p, c) => `${p} propositioner har hänvisats till ${c} utskott, vilket visar bredden i regeringens lagstiftningsambitioner.`,
    da: (p, c) => `${p} lovforslag er henvist til ${c} udvalg, hvilket viser bredden i regeringens lovgivningsmæssige ambitioner.`,
    no: (p, c) => `${p} proposisjoner er henvist til ${c} komiteer, noe som viser bredden i regjeringens lovgivningsmessige ambisjoner.`,
    fi: (p, c) => `${p} esitystä on viitattu ${c} valiokuntaan, mikä kuvastaa hallituksen lainsäädännöllisten tavoitteiden laajuutta.`,
    de: (p, c) => `${p} Vorlagen wurden an ${c} Ausschüsse verwiesen, was die Breite der Gesetzgebungsambitionen der Regierung zeigt.`,
    fr: (p, c) => `${p} propositions ont été renvoyées à ${c} commissions, montrant l'ampleur des ambitions législatives du gouvernement.`,
    es: (p, c) => `${p} proposiciones han sido remitidas a ${c} comités, mostrando la amplitud de las ambiciones legislativas del gobierno.`,
    nl: (p, c) => `${p} voorstellen zijn verwezen naar ${c} commissies, wat de breedte van de wetgevende ambities van de regering toont.`,
    ar: (p, c) => `تمت إحالة ${p} مقترحات إلى ${c} لجان، مما يُظهر نطاق الطموحات التشريعية الحكومية.`,
    he: (p, c) => `${p} הצעות הופנו ל-${c} ועדות, המראות את רוחב השאיפות החקיקתיות של הממשלה.`,
    ja: (p, c) => `${p}件の法案が${c}の委員会に付託され、政府の幅広い立法野心を示しています。`,
    ko: (p, c) => `${p}건의 법안이 ${c}개 위원회에 회부되어 정부의 광범위한 입법 야심을 나타냅니다.`,
    zh: (p, c) => `${p}项提案已交付${c}个委员会审议,显示政府广泛的立法雄心。`,
  };
  const propOverTpl = propOverviewTemplates[lang as string];
  const propOverview = propOverTpl
    ? propOverTpl(propositions.length, committeeCountProp)
    : `${propositions.length} propositions have been referred to ${committeeCountProp} committees, showing the breadth of the government's legislative ambitions.`;
  content += `        <li>${escapeHtml(propOverview)}</li>\n`;
 
  // Policy domain cross-analysis
  if (allPropDomains.size > 0) {
    const domainListProp = Array.from(allPropDomains).slice(0, 3).join(', ');
    const propDomainTemplates: Record<string, (d: string) => string> = {
      sv: d => `Propositionerna berör ${d} — ett mönster som avslöjar regeringens politik­prioriteringar.`,
      da: d => `Lovforslagene berører ${d} — et mønster der afdækker regeringens politiske prioriteringer.`,
      no: d => `Proposisjonene berører ${d} — et mønster som avslører regjeringens politiske prioriteringer.`,
      fi: d => `Esitykset kattavat ${d} — malli, joka paljastaa hallituksen poliittiset prioriteetit.`,
      de: d => `Die Vorlagen betreffen ${d} — ein Muster, das die politischen Prioritäten der Regierung offenbart.`,
      fr: d => `Les propositions touchent ${d} — un schéma révélant les priorités politiques du gouvernement.`,
      es: d => `Las proposiciones abarcan ${d} — un patrón que revela las prioridades políticas del gobierno.`,
      nl: d => `De voorstellen raken ${d} — een patroon dat de politieke prioriteiten van de regering onthult.`,
      ar: d => `تمس المقترحات ${d} — نمط يكشف عن الأولويات السياسية للحكومة.`,
      he: d => `ההצעות נוגעות ב${d} — תבנית החושפת את סדרי העדיפויות הפוליטיים של הממשלה.`,
      ja: d => `法案は${d}に及び、政府の政策優先事項を明らかにしています。`,
      ko: d => `법안은 ${d}에 걸쳐 있으며, 정부의 정책 우선순위를 드러냅니다.`,
      zh: d => `提案涉及${d}——揭示了政府的政策优先事项。`,
    };
    const propDomTpl = propDomainTemplates[lang as string];
    const propDomainAnalysis = propDomTpl
      ? propDomTpl(escapeHtml(domainListProp))
      : `Propositions span ${escapeHtml(domainListProp)} — a pattern revealing the government's policy priorities.`;
    content += `        <li>${propDomainAnalysis}</li>\n`;
  }
 
  content += `      </ul>\n    </div>\n`;
 
  // Display limits for enrichment sections
  const MAX_DISPLAY_ITEMS = 3;
  const MAX_SPEECH_PREVIEW_LENGTH = 200;
 
  // ── Policy Substance section (from search_dokument_fulltext) ─────────────
  const fullTextResults = data.fullTextResults as Array<Record<string, unknown>> | undefined;
  Iif (fullTextResults && fullTextResults.length > 0) {
    const policySubstanceHeadings: Record<string, string> = {
      en: 'Policy Substance', sv: 'Politikinnehåll', da: 'Politisk indhold',
      no: 'Politisk innhold', fi: 'Politiikan sisältö', de: 'Politischer Inhalt',
      fr: 'Contenu politique', es: 'Contenido de la política', nl: 'Beleidsinhoud',
      ar: 'مضمون السياسة', he: 'תוכן המדיניות', ja: '政策の内容', ko: '정책 내용', zh: '政策内容',
    };
    const psHeading = policySubstanceHeadings[lang as string] ?? policySubstanceHeadings['en'];
    content += `\n    <h2>${escapeHtml(psHeading)}</h2>\n`;
    content += `    <div class="policy-substance">\n`;
    for (const doc of fullTextResults.slice(0, MAX_DISPLAY_ITEMS)) {
      const docTitle = escapeHtml(String(doc['titel'] ?? doc['title'] ?? ''));
      const docSummary = escapeHtml(String(doc['summary'] ?? doc['notis'] ?? ''));
      if (docTitle) {
        content += `      <div class="fulltext-result"><strong>${docTitle}</strong>`;
        if (docSummary) content += `<p>${docSummary}</p>`;
        content += `</div>\n`;
      }
    }
    content += `    </div>\n`;
  }
 
  // ── Department Impact section (from analyze_g0v_by_department) ───────────
  const departmentAnalysis = data.departmentAnalysis as Record<string, unknown> | undefined;
  const departments = departmentAnalysis
    ? ((departmentAnalysis['departments'] ?? departmentAnalysis['dokument'] ?? []) as Array<Record<string, unknown>>)
    : [];
  Iif (departments.length > 0) {
    const departmentImpactHeadings: Record<string, string> = {
      en: 'Department Impact', sv: 'Departementets påverkan', da: 'Ministerielt ansvar',
      no: 'Departementspåvirkning', fi: 'Ministeriön vaikutus', de: 'Ressortverantwortung',
      fr: 'Impact ministériel', es: 'Impacto ministerial', nl: 'Ministeriële impact',
      ar: 'تأثير الوزارة', he: 'השפעת המשרד', ja: '省庁への影響', ko: '부처 영향', zh: '部门影响',
    };
    const diHeading = departmentImpactHeadings[lang as string] ?? departmentImpactHeadings['en'];
    content += `\n    <h2>${escapeHtml(diHeading)}</h2>\n`;
    content += `    <div class="department-impact"><ul>\n`;
    for (const dept of departments.slice(0, MAX_DISPLAY_ITEMS)) {
      const deptName = escapeHtml(String(dept['departement'] ?? dept['name'] ?? dept['namn'] ?? ''));
      const deptCount = Number(dept['count'] ?? dept['antal'] ?? 0);
      if (deptName) {
        content += `      <li>${deptName}${deptCount > 0 ? ` (${deptCount})` : ''}</li>\n`;
      }
    }
    content += `    </ul></div>\n`;
  }
 
  // ── Parliamentary Debate section (from search_anforanden) ─────────────────
  const speechDebates = data.speechDebates as Array<Record<string, unknown>> | undefined;
  Iif (speechDebates && speechDebates.length > 0) {
    const parliamentaryDebateHeadings: Record<string, string> = {
      en: 'Parliamentary Debate', sv: 'Parlamentarisk debatt', da: 'Parlamentarisk debat',
      no: 'Parlamentarisk debatt', fi: 'Parlamentaarinen keskustelu', de: 'Parlamentarische Debatte',
      fr: 'Débat parlementaire', es: 'Debate parlamentario', nl: 'Parlementair debat',
      ar: 'النقاش البرلماني', he: 'דיון פרלמנטרי', ja: '議会討論', ko: '의회 토론', zh: '议会辩论',
    };
    const pdHeading = parliamentaryDebateHeadings[lang as string] ?? parliamentaryDebateHeadings['en'];
    content += `\n    <h2>${escapeHtml(pdHeading)}</h2>\n`;
    content += `    <div class="debate-context">\n`;
    for (const speech of speechDebates.slice(0, MAX_DISPLAY_ITEMS)) {
      const speaker = escapeHtml(String(speech['talare'] ?? speech['speaker'] ?? ''));
      const party = escapeHtml(String(speech['parti'] ?? speech['party'] ?? ''));
      const text = escapeHtml(String(speech['anforandetext'] ?? speech['text'] ?? '').substring(0, MAX_SPEECH_PREVIEW_LENGTH));
      if (speaker && text) {
        content += `      <blockquote><p>${text}…</p><footer>— ${speaker}${party ? ` (${party})` : ''}</footer></blockquote>\n`;
      }
    }
    content += `    </div>\n`;
  }
 
  return content;
}
 
export function generateMotionsContent(data: ArticleContentData, lang: Language | string): string {
  const motions = data.motions || [];
 
  let content = `<h2>${L(lang, 'oppMotions')}</h2>\n`;
 
  if (motions.length === 0) {
    content += `<p>${L(lang, 'noMotions')}</p>\n`;
    return content;
  }
 
  // Analytical lede paragraph
  const breakdownFn = L(lang, 'motionsBreakdown') as string | ((n: number) => string);
  const breakdownText = typeof breakdownFn === 'function'
    ? breakdownFn(motions.length)
    : `${motions.length} new opposition motions filed.`;
  content += `<p class="article-lede">${escapeHtml(String(breakdownText))}</p>\n`;
 
  // Group motions by party for strategic analysis
  const byParty: Record<string, RawDocument[]> = {};
  motions.forEach(motion => {
    const party = normalizePartyKey(motion.parti);
    if (!byParty[party]) byParty[party] = [];
    byParty[party].push(motion);
  });
 
  // Opposition strategy section with per-party analysis
  const partyCount = Object.keys(byParty).filter(p => p !== 'other').length;
  if (partyCount > 1) {
    content += `\n    <h2>${L(lang, 'oppositionStrategy')}</h2>\n`;
    const strategyFn = L(lang, 'oppositionStrategyContext') as string | ((n: number) => string);
    const strategyContext = typeof strategyFn === 'function'
      ? strategyFn(partyCount)
      : `Motions from ${partyCount} different parties reveal the breadth of opposition political criticism and alternative policy agendas.`;
    content += `    <p>${escapeHtml(String(strategyContext))}</p>\n`;
    // Per-party analysis with domain focus
    content += generateOppositionStrategySection(motions, lang);
  }
 
  // Group "med anledning av prop." motions by parent proposition to eliminate duplicate headings
  const { grouped: groupedByProp, independent: independentMotions } = groupMotionsByProposition(motions);
 
  if (groupedByProp.size > 0) {
    content += `\n    <h2>${L(lang, 'responsesToProp')}</h2>\n`;
    groupedByProp.forEach((propMotions, propRef) => {
      // Extract the descriptive title portion that follows the prop ID
      const firstTitle = propMotions[0]?.titel || propMotions[0]?.title || '';
      const suffixMatch = firstTitle.match(PROP_TITLE_SUFFIX_REGEX);
      const propTitle = suffixMatch?.[1]?.trim() || String(propRef);
      const safePropRef = escapeHtml(String(propRef));
      const safePropTitle = escapeHtml(propTitle);
      content += `    <h3>Prop. ${safePropRef}: ${svSpan(safePropTitle, lang)}</h3>\n`;
      // Individual motions inside a prop group use h4 to maintain h2→h3→h4 hierarchy
      propMotions.forEach(m => {
        const html = renderMotionEntry(m, lang);
        content += html.replace(/<h3(\b[^>]*)?>/g, '<h4$1>').replace(/<\/h3>/g, '</h4>');
      });
    });
  }
 
  // Motions to render with thematic analysis:
  // - when proposition groups exist: only independent motions (non-"med anledning av")
  // - when no proposition groups: all motions (preserves existing thematic behaviour)
  const thematicMotions = groupedByProp.size > 0 ? independentMotions : motions;
 
  if (thematicMotions.length > 0) {
    if (groupedByProp.size > 0) {
      content += `\n    <h2>${L(lang, 'independentMotions')}</h2>\n`;
    }
 
    // Group motions by primary policy theme for thematic analysis
    const byTheme: Record<string, RawDocument[]> = {};
    thematicMotions.forEach(motion => {
      const domains = detectPolicyDomains(motion, lang);
      const theme = domains[0] || String(L(lang, 'generalMatters'));
      if (!byTheme[theme]) byTheme[theme] = [];
      byTheme[theme].push(motion);
    });
    const themeCount = Object.keys(byTheme).length;
 
    if (themeCount > 1 && groupedByProp.size === 0) {
      // Suppress "Thematic Analysis" h2 when already under an "Independent Motions" h2
      // (groupedByProp.size === 0 means we are NOT in the split-section layout, so it is
      // safe to emit the additional h2 without creating two consecutive section headers)
      content += `\n    <h2>${L(lang, 'thematicAnalysis')}</h2>\n`;
      Object.entries(byTheme).forEach(([theme, themeMotions]) => {
        content += `\n    <h3>${escapeHtml(theme)} (${themeMotions.length})</h3>\n`;
        themeMotions.forEach(motion => {
          // Demote motion entry headings one level when inside a themed section
          const entryHtml = renderMotionEntry(motion, lang);
          const demotedHtml = entryHtml
            .replace(/<h3(\b[^>]*)?>/g, '<h4$1>')
            .replace(/<\/h3>/g, '</h4>');
          content += demotedHtml;
        });
      });
    } else {
      // Single theme, no detection, or alongside proposition groups: flat list
      thematicMotions.forEach(motion => { content += renderMotionEntry(motion, lang); });
    }
  }
 
  // Party activity breakdown
  if (partyCount > 0) {
    // Narrative bridge before cross-party analysis (inter-pillar transition)
    const watchTransition = getPillarTransition(lang, 'watchToOpposition');
    Eif (watchTransition) {
      content += `    <p class="pillar-transition">${escapeHtml(watchTransition)}</p>\n`;
    }
    content += `\n    <h2>${L(lang, 'coalitionDynamics')}</h2>\n`;
    content += `    <div class="context-box">\n      <ul>\n`;
    Object.entries(byParty).forEach(([party, partyMotions]) => {
      Eif (party !== 'other') {
        const detailFn = L(lang, 'partyMotionsFiled') as string | ((party: string, n: number) => string);
        const detail = typeof detailFn === 'function'
          ? detailFn(party, partyMotions.length)
          : `${party}: ${partyMotions.length} motions filed`;
        content += `        <li>${escapeHtml(String(detail))}</li>\n`;
      }
    });
    content += `      </ul>\n    </div>\n`;
  }
 
  // Government department engagement section (from analyze_g0v_by_department)
  const govDeptData = data.govDeptData ?? [];
  Iif (govDeptData.length > 0) {
    content += `\n    <h2>${L(lang, 'govEngagement')}</h2>\n`;
    content += `    <div class="context-box">\n      <ul>\n`;
    govDeptData.slice(0, 5).forEach(dept => {
      const deptName = escapeHtml(String(dept['name'] ?? dept['departement'] ?? dept['department'] ?? ''));
      const deptCount = dept['count'] ?? dept['total'] ?? dept['document_count'];
      if (deptName) {
        const hasDeptCount = deptCount !== null && deptCount !== undefined;
        content += hasDeptCount
          ? `        <li><strong>${deptName}</strong> (${escapeHtml(String(deptCount))})</li>\n`
          : `        <li><strong>${deptName}</strong></li>\n`;
      }
    });
    content += `      </ul>\n    </div>\n`;
  }
 
  return content;
}
 
export function generateGenericContent(data: ArticleContentData, lang: Language | string): string {
  const docs = data.documents || [];
  if (docs.length === 0) {
    return `<p>${L(lang, 'genericContent')}</p>`;
  }
 
  const cia = data.ciaContext;
  let content = '';
 
  // ── Inverted-pyramid lede: lead with most significant document type ──────
  // Group by document type first to identify the most newsworthy lead
  const byType: Record<string, RawDocument[]> = {};
  docs.forEach(doc => {
    const docType = doc.doktyp || doc.documentType || 'other';
    if (!byType[docType]) byType[docType] = [];
    byType[docType].push(doc);
  });
 
  // Significance order: propositions → committee reports → government comms → motions → rest
  const typeOrder = ['prop', 'bet', 'skr', 'mot', 'other'];
  const sortedTypes = [...Object.keys(byType)].sort((a, b) => {
    const ai = typeOrder.indexOf(a); const bi = typeOrder.indexOf(b);
    return (ai === -1 ? 99 : ai) - (bi === -1 ? 99 : bi);
  });
 
  // Lead with the most significant type rather than a raw count
  const leadType = sortedTypes[0];
  const leadDocs = leadType ? (byType[leadType] ?? []) : [];
  const leadTitle = leadDocs[0] ? (leadDocs[0].titel || leadDocs[0].title || '') : '';
 
  // Per-language title suffix (e.g. " — including "Prop. 2025/26:42"")
  const titleSuffix: string = leadTitle
    ? (TITLE_SUFFIX_TEMPLATES[lang] ?? (t => ` — including "${t}"`))(leadTitle)
    : '';
 
  let ledeText: string;
  if (leadType === 'prop' && leadDocs.length > 0) {
    const n = leadDocs.length;
    ledeText = lang === 'sv'
      ? `Riksdagen behandlar ${n} proposition${n !== 1 ? 'er' : ''}${titleSuffix} under denna period.`
      : lang === 'da' ? `Folketinget behandler ${n} lovforslag${titleSuffix} i denne periode.`
      : lang === 'no' ? `Stortinget behandler ${n} lovproposisjon${n !== 1 ? 'er' : ''}${titleSuffix} i denne perioden.`
      : lang === 'fi' ? `Eduskunta käsittelee ${n} hallituksen esitystä${titleSuffix} tällä kaudella.`
      : lang === 'de' ? `Das Parlament berät ${n} Regierungsvorlag${n !== 1 ? 'en' : 'e'}${titleSuffix} in dieser Periode.`
      : lang === 'fr' ? `Le parlement examine ${n} proposition${n !== 1 ? 's' : ''} gouvernementale${n !== 1 ? 's' : ''}${titleSuffix} pendant cette période.`
      : lang === 'es' ? `El parlamento examina ${n} proposición${n !== 1 ? 'es' : ''} gubernamental${n !== 1 ? 'es' : ''}${titleSuffix} durante este período.`
      : lang === 'nl' ? `Het parlement bespreekt ${n} regeringsvoorstel${n !== 1 ? 'len' : ''}${titleSuffix} in deze periode.`
      : lang === 'ar' ? `يناقش البرلمان ${n} اقتراح${n !== 1 ? 'ات' : ''} حكومية${titleSuffix} خلال هذه الفترة.`
      : lang === 'he' ? `הפרלמנט דן ב-${n} הצעת חוק ממשלתית${n !== 1 ? 'ות' : ''}${titleSuffix} בתקופה זו.`
      : lang === 'ja' ? `議会はこの期間中に${n}本の政府提出法案を審議しています${titleSuffix}。`
      : lang === 'ko' ? `의회는 이 기간 동안 ${n}건의 정부 법안을 심의하고 있습니다${titleSuffix}.`
      : lang === 'zh' ? `议会正在审议本期${n}项政府提案${titleSuffix}。`
      : `Parliament is considering ${n} government proposition${n !== 1 ? 's' : ''}${titleSuffix} during this period.`;
  } else if (leadType === 'bet' && leadDocs.length > 0) {
    const n = leadDocs.length;
    ledeText = lang === 'sv'
      ? `Utskotten har lämnat ${n} betänkande${n !== 1 ? 'n' : ''}${titleSuffix} för riksdagens beslut.`
      : lang === 'da' ? `Udvalgene har afleveret ${n} betænkning${n !== 1 ? 'er' : ''}${titleSuffix} til parlamentarisk beslutning.`
      : lang === 'no' ? `Komiteene har levert ${n} innstilling${n !== 1 ? 'er' : ''}${titleSuffix} til parlamentarisk beslutning.`
      : lang === 'fi' ? `Valiokunnat ovat toimittaneet ${n} mietinnön${titleSuffix} parlamentin päätettäväksi.`
      : lang === 'de' ? `Die Ausschüsse haben ${n} Bericht${n !== 1 ? 'e' : ''}${titleSuffix} zur parlamentarischen Entscheidung vorgelegt.`
      : lang === 'fr' ? `Les commissions ont livré ${n} rapport${n !== 1 ? 's' : ''}${titleSuffix} pour décision parlementaire.`
      : lang === 'es' ? `Los comités han presentado ${n} informe${n !== 1 ? 's' : ''}${titleSuffix} para decisión parlamentaria.`
      : lang === 'nl' ? `De commissies hebben ${n} rapport${n !== 1 ? 'en' : ''}${titleSuffix} ingediend voor parlementaire beslissing.`
      : lang === 'ar' ? `قدمت اللجان ${n} تقرير${n !== 1 ? 'اً' : ''}${titleSuffix} للقرار البرلماني.`
      : lang === 'he' ? `הוועדות הגישו ${n} דוח${n !== 1 ? 'ות' : ''}${titleSuffix} להחלטה פרלמנטרית.`
      : lang === 'ja' ? `委員会は議会の決定のために${n}本の報告書を提出しました${titleSuffix}。`
      : lang === 'ko' ? `위원회들이 의회 결정을 위해 ${n}건의 보고서를 제출했습니다${titleSuffix}.`
      : lang === 'zh' ? `委员会已提交${n}份报告${titleSuffix}供议会决定。`
      : `Committees have delivered ${n} report${n !== 1 ? 's' : ''}${titleSuffix} for parliamentary decision.`;
  } else {
    const overviewFn = L(lang, 'genericOverview') as string | ((n: number) => string);
    ledeText = typeof overviewFn === 'function'
      ? overviewFn(docs.length)
      : `During this period, ${docs.length} documents were processed in parliament.`;
  }
  content += `<p class="article-lede">${escapeHtml(ledeText)}</p>\n`;
 
  content += `\n    <h2>${L(lang, 'thematicAnalysis')}</h2>\n`;
 
  // Per-language document type labels
  const docTypeLabels: Record<string, Record<string, string>> = {
    mot: { en: 'Motions', sv: 'Motioner', da: 'Forslag', no: 'Forslag', fi: 'Aloitteet', de: 'Anträge', fr: 'Motions', es: 'Mociones', nl: 'Moties', ar: 'اقتراحات', he: 'הצעות', ja: '動議', ko: '동의', zh: '动议' },
    prop: { en: 'Propositions', sv: 'Propositioner', da: 'Lovforslag', no: 'Proposisjoner', fi: 'Hallituksen esitykset', de: 'Regierungsvorlagen', fr: 'Propositions', es: 'Proposiciones', nl: 'Regeringsvoorstellen', ar: 'مقترحات حكومية', he: 'הצעות ממשלה', ja: '政府提案', ko: '정부 법안', zh: '政府提案' },
    bet: { en: 'Committee Reports', sv: 'Betänkanden', da: 'Betænkninger', no: 'Innstillinger', fi: 'Mietinnöt', de: 'Ausschussberichte', fr: 'Rapports de commission', es: 'Informes de comité', nl: 'Commissierapporten', ar: 'تقارير اللجان', he: 'דוחות ועדה', ja: '委員会報告', ko: '위원회 보고서', zh: '委员会报告' },
    skr: { en: 'Government Communications', sv: 'Skrivelser', da: 'Regeringsmeddelelser', no: 'Regjeringsmeldinger', fi: 'Hallituksen kirjeet', de: 'Regierungsschreiben', fr: 'Communications gouvernementales', es: 'Comunicaciones gubernamentales', nl: 'Regeringsmededelingen', ar: 'مراسلات حكومية', he: 'תקשורות ממשלתיות', ja: '政府通知', ko: '정부 서한', zh: '政府通知' },
  };
 
  for (const docType of sortedTypes) {
    const typeDocs = byType[docType] ?? [];
    const otherDocsVal = L(lang, 'otherDocuments');
    const otherDocsLabel = typeof otherDocsVal === 'string' ? otherDocsVal : 'Other documents';
    const langLabels = docTypeLabels[docType];
    const typeLabel = langLabels
      ? (langLabels[lang as string] ?? langLabels['sv'] ?? docType)
      : docType === 'other' ? otherDocsLabel : docType;
 
    content += `\n    <h3>${escapeHtml(typeLabel)} (${typeDocs.length})</h3>\n`;
 
    // ── Per-document deep analysis ───────────────────────────────────────
    for (const doc of typeDocs) {
      const titleText = doc.titel || doc.title || '';
      const escapedTitle = escapeHtml(titleText);
      const titleHtml = (doc.titel && !doc.title)
        ? svSpan(escapedTitle, lang)
        : escapedTitle;
 
      const analysis = generateDocumentIntelligenceAnalysis(doc, docType, cia, lang);
 
      content += `    <div class="document-entry">\n`;
      content += `      <h4>${titleHtml}</h4>\n`;
      content += `      <p>${analysis}</p>\n`;
      if (doc.url) {
        content += `      <p><a href="${sanitizeUrl(doc.url)}" class="document-link" rel="noopener noreferrer">${escapeHtml(doc.dokumentnamn || doc.dok_id || titleText)}</a></p>\n`;
      }
      content += `    </div>\n`;
    }
  }
 
  // ── Cross-type analytical sections (bring generic content closer to dedicated generators) ──
 
  // Opposition strategy when motions with multiple parties exist
  const motionDocs = docs.filter(d => (d.doktyp || d.documentType) === 'mot');
  if (motionDocs.length >= 2) {
    const byPartyGeneric: Record<string, RawDocument[]> = {};
    motionDocs.forEach(m => {
      const party = normalizePartyKey(m.parti);
      if (!byPartyGeneric[party]) byPartyGeneric[party] = [];
      byPartyGeneric[party].push(m);
    });
    const partyCountGeneric = Object.keys(byPartyGeneric).filter(p => p !== 'other').length;
    Eif (partyCountGeneric > 1) {
      content += `\n    <h2>${L(lang, 'oppositionStrategy')}</h2>\n`;
      content += generateOppositionStrategySection(motionDocs, lang);
    }
  }
 
  // Committee breakdown when committee reports exist
  const reportDocs = docs.filter(d => (d.doktyp || d.documentType) === 'bet');
  if (reportDocs.length >= 2) {
    const byCommitteeGeneric: Record<string, number> = {};
    reportDocs.forEach(r => {
      const c = r.organ || r.committee || 'unknown';
      byCommitteeGeneric[c] = (byCommitteeGeneric[c] || 0) + 1;
    });
    const knownCommittees = Object.entries(byCommitteeGeneric).filter(([c]) => c !== 'unknown');
    Eif (knownCommittees.length > 0) {
      const committeeSectionLabels: Record<string, string> = {
        en: 'Committee Activity', sv: 'Utskottsaktivitet', da: 'Udvalgsaktivitet',
        no: 'Komitéaktivitet', fi: 'Valiokuntatoiminta', de: 'Ausschusstätigkeit',
        fr: 'Activité des commissions', es: 'Actividad de comités', nl: 'Commissieactiviteit',
        ar: 'نشاط اللجان', he: 'פעילות ועדות', ja: '委員会活動', ko: '위원회 활동', zh: '委员会活动',
      };
      const committeeSectionLabel = committeeSectionLabels[lang as string] ?? 'Committee Activity';
      content += `\n    <h2>${escapeHtml(committeeSectionLabel)}</h2>\n`;
      content += `    <div class="context-box">\n      <ul>\n`;
      knownCommittees
        .sort(([, a], [, b]) => b - a)
        .forEach(([c, n]) => {
          content += `        <li>${escapeHtml(getCommitteeName(c, lang))}: ${n}</li>\n`;
        });
      content += `      </ul>\n    </div>\n`;
    }
  }
 
  // Government priority signal when multiple propositions target the same committee
  const propDocs = docs.filter(d => (d.doktyp || d.documentType) === 'prop');
  if (propDocs.length >= 2) {
    const byPropCommittee: Record<string, number> = {};
    propDocs.forEach(p => {
      const c = p.organ || p.committee || 'unknown';
      byPropCommittee[c] = (byPropCommittee[c] || 0) + 1;
    });
    const sortedPropCommittees = Object.entries(byPropCommittee)
      .filter(([c]) => c !== 'unknown')
      .sort(([, a], [, b]) => b - a);
    Eif (sortedPropCommittees.length > 0 && sortedPropCommittees[0][1] >= 2) {
      const [topC, topN] = sortedPropCommittees[0];
      const topCName = escapeHtml(getCommitteeName(topC, lang));
      const govPriorityTemplates: Record<string, (n: string, c: number) => string> = {
        sv: (n, c) => `${n} tar emot ${c} propositioner – detta signalerar ett prioriterat politikområde.`,
        da: (n, c) => `${n} modtager ${c} lovforslag — et klart signal om prioritet.`,
        no: (n, c) => `${n} mottar ${c} proposisjoner — et signal om regjeringsprioritet.`,
        fi: (n, c) => `${n} vastaanottaa ${c} esitystä — merkki hallituksen painopistealueesta.`,
        de: (n, c) => `${n} erhält ${c} Vorlagen — ein Signal für Regierungspriorität.`,
        fr: (n, c) => `${n} reçoit ${c} propositions — un signal de priorité gouvernementale.`,
        es: (n, c) => `${n} recibe ${c} proposiciones — señal de prioridad gubernamental.`,
        nl: (n, c) => `${n} ontvangt ${c} voorstellen — signaal van overheidsprioriteit.`,
        ar: (n, c) => `${n} يستقبل ${c} مقترحات — إشارة لأولوية حكومية.`,
        he: (n, c) => `${n} מקבל ${c} הצעות — אות לעדיפות ממשלתית.`,
        ja: (n, c) => `${n}は${c}件の提案を受け取り、政府の重点分野を示しています。`,
        ko: (n, c) => `${n}이(가) ${c}건의 법안을 받아 정부 우선순위를 나타냅니다.`,
        zh: (n, c) => `${n}收到${c}项提案——表明这是政府的优先领域。`,
      };
      const govTpl = govPriorityTemplates[lang as string];
      const govNote = govTpl
        ? govTpl(topCName, topN)
        : `${topCName} receives ${topN} propositions — signalling government priority in this policy area.`;
      content += `\n    <h2>${L(lang, 'policyImplications')}</h2>\n`;
      content += `    <p>${govNote}</p>\n`;
    }
  }
 
  // ── Narrative bridge to analytical outlook ───────────────────────────────
  const oppositionTransition = getPillarTransition(lang, 'oppositionToAhead');
  Eif (oppositionTransition) {
    content += `    <p class="pillar-transition">${escapeHtml(oppositionTransition)}</p>\n`;
  }
 
  // ── Key takeaways ────────────────────────────────────────────────────────
  content += `\n    <h2>${L(lang, 'keyTakeaways')}</h2>\n`;
  content += `    <div class="context-box">\n      <ul>\n`;
 
  // Document type distribution (localised labels in summary too)
  const typeDescriptions = sortedTypes.map(docType => {
    const typeDocs = byType[docType] ?? [];
    const langLabels2 = docTypeLabels[docType];
    const label = langLabels2
      ? (langLabels2[lang as string] ?? langLabels2['sv'] ?? docType).toLowerCase()
      : docType;
    return `${typeDocs.length} ${label}`;
  });
  const processedLabel = L(lang, 'processedThisPeriod');
  const processedSuffix = typeof processedLabel === 'string' ? processedLabel : 'processed this period';
  if (typeDescriptions.length > 0) {
    content += `        <li>${escapeHtml(typeDescriptions.join(', '))} ${escapeHtml(processedSuffix)}</li>\n`;
  }
 
  // Policy domains — show labels only to keep the bullet concise
  const allDomains = new Set<string>();
  const enrichedCount = docs.filter(d => d.contentFetched).length;
  docs.forEach(doc => {
    detectPolicyDomains(doc, lang).forEach(d => allDomains.add(d));
  });
  if (allDomains.size > 0) {
    const policyContextVal = L(lang, 'policyContext');
    content += `        <li>${escapeHtml(String(policyContextVal))}: ${escapeHtml(Array.from(allDomains).slice(0, 4).join('; '))}</li>\n`;
  }
  if (enrichedCount > 0) {
    const analysisDepthLabels: Record<string, string> = {
      en: 'Analysis depth', sv: 'Analysdjup', da: 'Analysedybde', no: 'Analysedybde',
      fi: 'Analyysisyvyys', de: 'Analysetiefe', fr: 'Profondeur d\'analyse',
      es: 'Profundidad del análisis', nl: 'Analysediepte', ar: 'عمق التحليل',
      he: 'עומק הניתוח', ja: '分析の深さ', ko: '분석 깊이', zh: '分析深度',
    };
    const depthLabel = analysisDepthLabels[lang as string] ?? 'Analysis depth';
    const ofLabels: Record<string, string> = {
      sv: 'av', da: 'af', no: 'av', fi: '/', de: 'von', fr: 'sur', es: 'de',
      nl: 'van', ar: 'من', he: 'מתוך', ja: '/', ko: '/', zh: '/',
    };
    const ofLabel = ofLabels[lang as string] ?? 'of';
    content += `        <li><strong>${escapeHtml(depthLabel)}:</strong> ${enrichedCount} ${ofLabel} ${docs.length}</li>\n`;
  }
 
  // ── SECONDARY: CIA context only when it changes interpretation ───────────
  // Razor-thin majority is actionable intelligence worth flagging once, in summary
  if (cia && cia.coalitionStability.majorityMargin <= 2) {
    const margin = cia.coalitionStability.majorityMargin;
    const ciaContextTemplates: Record<string, (m: number) => string> = {
      sv: m => `Historisk kontext: nuvarande ${m}-mandatsövertag innebär att en enda avhoppare kan ändra utfallet.`,
      da: m => `Historisk kontekst: det nuværende flertal på ${m} mandater betyder, at en enkelt afhopper kan vende resultatet.`,
      no: m => `Historisk kontekst: nåværende ${m}-mandats flertall betyr at en enkelt avhopper kan snu utfallet.`,
      fi: m => `Historiallinen konteksti: nykyinen ${m} paikan enemmistö tarkoittaa, että yksittäinen loikkari voi kääntää tuloksen.`,
      de: m => `Historischer Kontext: Die aktuelle ${m}-Sitze-Mehrheit bedeutet, dass ein einzelner Abweichler das Ergebnis kippen könnte.`,
      fr: m => `Contexte historique : la majorité actuelle de ${m} sièges signifie qu'une seule défection pourrait inverser le résultat.`,
      es: m => `Contexto histórico: la mayoría actual de ${m} escaños significa que una sola defección podría revertir el resultado.`,
      nl: m => `Historische context: de huidige meerderheid van ${m} zetels betekent dat één enkele overloper de uitkomst kan omkeren.`,
      ar: m => `السياق التاريخي: الأغلبية الحالية البالغة ${m} مقاعد تعني أن انشقاقاً واحداً يمكن أن يعكس النتائج.`,
      he: m => `הקשר היסטורי: הרוב הנוכחי של ${m} מושבים משמעו שעריקות אחת יכולה להפוך את התוצאה.`,
      ja: m => `歴史的背景:現在の${m}議席差は、1人の離反で結果が覆る可能性を意味します。`,
      ko: m => `역사적 맥락: 현재 ${m}석 차이는 단 한 명의 이탈로도 결과가 뒤집힐 수 있음을 의미합니다.`,
      zh: m => `历史背景:目前${m}席的多数意味着任何一位议员的倒戈都可能逆转结果。`,
    };
    const ciaTpl = ciaContextTemplates[lang as string];
    const ciaText = ciaTpl
      ? ciaTpl(margin)
      : `Historical context: the current ${margin}-seat majority means any single defection or absence could reverse outcomes this week.`;
    content += `        <li><small class="cia-context">${escapeHtml(ciaText)}</small></li>\n`;
  }
 
  content += `      </ul>\n    </div>\n`;
 
  return content;
}
 
/**
 * Per-language label maps for the monthly-review-specific sections.
 * Covers all 14 supported languages.
 */
const MONTHLY_LABELS: Readonly<{
  monthInNumbers: Record<string, string>;
  partyRankings: Record<string, string>;
  legislativeEfficiency: Record<string, string>;
  strategicOutlook: Record<string, string>;
  totalDocuments: Record<string, string>;
  reports: Record<string, string>;
  propositions: Record<string, string>;
  motions: Record<string, string>;
  speeches: Record<string, string>;
  efficiencyRate: Record<string, string>;
  trendVsPrevMonth: Record<string, string>;
  trendVs2MonthsAgo: Record<string, string>;
  activityRank: Record<string, string>;
  motionsFiled: Record<string, string>;
  speechesDelivered: Record<string, string>;
  coalitionStabilityOutlook: Record<string, string>;
  motionDenialContext: Record<string, string>;
  insufficientData: Record<string, string>;
}> = {
  monthInNumbers: { en: '📊 Month in Numbers', sv: '📊 Månaden i siffror', da: '📊 Måneden i tal', no: '📊 Måneden i tall', fi: '📊 Kuukausi numeroina', de: '📊 Der Monat in Zahlen', fr: '📊 Le mois en chiffres', es: '📊 El mes en cifras', nl: '📊 De maand in cijfers', ar: '📊 الشهر بالأرقام', he: '📊 החודש במספרים', ja: '📊 今月の統計', ko: '📊 이달의 통계', zh: '📊 本月数字' },
  partyRankings: { en: '🏆 Party Performance Rankings', sv: '🏆 Partiernas prestationsrankning', da: '🏆 Partiernes præstationsrangering', no: '🏆 Partienes prestasjonsranking', fi: '🏆 Puolueiden suoritusranking', de: '🏆 Partei-Leistungsranking', fr: '🏆 Classement des performances des partis', es: '🏆 Clasificación de rendimiento de partidos', nl: '🏆 Partijprestaties ranking', ar: '🏆 تصنيف أداء الأحزاب', he: '🏆 דירוג ביצועי מפלגות', ja: '🏆 政党パフォーマンスランキング', ko: '🏆 정당 성과 순위', zh: '🏆 政党绩效排名' },
  legislativeEfficiency: { en: '⚖️ Legislative Efficiency', sv: '⚖️ Lagstiftningseffektivitet', da: '⚖️ Lovgivningseffektivitet', no: '⚖️ Lovgivningseffektivitet', fi: '⚖️ Lainsäädäntötehokkuus', de: '⚖️ Gesetzgebungseffizienz', fr: '⚖️ Efficacité législative', es: '⚖️ Eficiencia legislativa', nl: '⚖️ Wetgevingsefficiëntie', ar: '⚖️ الكفاءة التشريعية', he: '⚖️ יעילות חקיקתית', ja: '⚖️ 立法効率', ko: '⚖️ 입법 효율성', zh: '⚖️ 立法效率' },
  strategicOutlook: { en: '🔭 Strategic Outlook', sv: '🔭 Strategisk utsikt', da: '🔭 Strategisk udsigt', no: '🔭 Strategisk utsikt', fi: '🔭 Strateginen näkymä', de: '🔭 Strategischer Ausblick', fr: '🔭 Perspectives stratégiques', es: '🔭 Perspectiva estratégica', nl: '🔭 Strategisch vooruitzicht', ar: '🔭 التوقعات الاستراتيجية', he: '🔭 סיכוי אסטרטגי', ja: '🔭 戦略的展望', ko: '🔭 전략적 전망', zh: '🔭 战略展望' },
  totalDocuments: { en: 'Total documents', sv: 'Totalt antal dokument', da: 'Dokumenter i alt', no: 'Totalt antall dokumenter', fi: 'Asiakirjoja yhteensä', de: 'Dokumente gesamt', fr: 'Total des documents', es: 'Total de documentos', nl: 'Totaal documenten', ar: 'إجمالي الوثائق', he: 'סך כל המסמכים', ja: '総文書数', ko: '총 문서 수', zh: '文件总数' },
  reports: { en: 'Committee reports', sv: 'Betänkanden', da: 'Betænkninger', no: 'Innstillinger', fi: 'Mietinnöt', de: 'Ausschussberichte', fr: 'Rapports de commission', es: 'Informes de comité', nl: 'Commissierapporten', ar: 'تقارير اللجان', he: 'דוחות ועדה', ja: '委員会報告', ko: '위원회 보고서', zh: '委员会报告' },
  propositions: { en: 'Government propositions', sv: 'Propositioner', da: 'Lovforslag', no: 'Proposisjoner', fi: 'Hallituksen esitykset', de: 'Regierungsvorlagen', fr: 'Propositions gouvernementales', es: 'Proposiciones gubernamentales', nl: 'Regeringsvoorstellen', ar: 'المقترحات الحكومية', he: 'הצעות ממשלה', ja: '政府提案', ko: '정부 법안', zh: '政府提案' },
  motions: { en: 'Opposition motions', sv: 'Motioner', da: 'Forslag', no: 'Forslag', fi: 'Aloitteet', de: 'Anträge', fr: "Motions de l'opposition", es: 'Mociones de la oposición', nl: 'Oppositiemoties', ar: 'مقترحات المعارضة', he: 'הצעות האופוזיציה', ja: '反対動議', ko: '야당 동의', zh: '反对党动议' },
  speeches: { en: 'Chamber speeches', sv: 'Anföranden', da: 'Taler', no: 'Taler', fi: 'Puheet', de: 'Parlamentsreden', fr: 'Discours parlementaires', es: 'Discursos parlamentarios', nl: 'Parlementaire toespraken', ar: 'الخطب البرلمانية', he: 'נאומי המליאה', ja: '議会演説', ko: '의회 연설', zh: '议会演讲' },
  efficiencyRate: { en: 'Committee reports per proposition', sv: 'Betänkanden per proposition', da: 'Betænkninger per lovforslag', no: 'Innstillinger per proposisjon', fi: 'Mietinnöt per esitys', de: 'Ausschussberichte pro Vorlage', fr: 'Rapports par proposition', es: 'Informes por proposición', nl: 'Commissierapporten per voorstel', ar: 'تقارير اللجان لكل اقتراح', he: 'דוחות ועדה לכל הצעה', ja: '提案当たり委員会報告数', ko: '제안당 위원회 보고서', zh: '每项提案的委员会报告数' },
  trendVsPrevMonth: { en: 'vs. previous month', sv: 'jmf föregående månad', da: 'ift. forrige måned', no: 'vs. forrige måned', fi: 'vs. edellinen kuukausi', de: 'vs. Vormonat', fr: 'vs. mois précédent', es: 'vs. mes anterior', nl: 'vs. vorige maand', ar: 'مقارنة بالشهر السابق', he: 'לעומת החודש הקודם', ja: '前月比', ko: '전월 대비', zh: '对比上个月' },
  trendVs2MonthsAgo: { en: '3-month rolling avg', sv: '3 månaders glidande snitt', da: '3-måneders glidende gennemsnit', no: '3 måneders glidende gjennomsnitt', fi: '3 kuukauden liukuva keskiarvo', de: '3-Monats-Gleitdurchschnitt', fr: 'Moyenne mobile sur 3 mois', es: 'Promedio móvil de 3 meses', nl: '3-maands voortschrijdend gemiddelde', ar: 'متوسط متحرك 3 أشهر', he: 'ממוצע נע 3 חודשים', ja: '3ヶ月移動平均', ko: '3개월 이동 평균', zh: '3个月滚动平均' },
  activityRank: { en: 'Activity rank', sv: 'Aktivitetsrankning', da: 'Aktivitetsrangering', no: 'Aktivitetsrangering', fi: 'Aktiivisuusranking', de: 'Aktivitätsrang', fr: "Rang d'activité", es: 'Rango de actividad', nl: 'Activiteitsrang', ar: 'تصنيف النشاط', he: 'דירוג פעילות', ja: '活動ランク', ko: '활동 순위', zh: '活动排名' },
  motionsFiled: { en: 'motions', sv: 'motioner', da: 'forslag', no: 'forslag', fi: 'aloitetta', de: 'Anträge', fr: 'motions', es: 'mociones', nl: 'moties', ar: 'مقترحات', he: 'הצעות', ja: '動議', ko: '동의', zh: '动议' },
  speechesDelivered: { en: 'speeches', sv: 'anföranden', da: 'taler', no: 'taler', fi: 'puhetta', de: 'Reden', fr: 'discours', es: 'discursos', nl: 'toespraken', ar: 'خطب', he: 'נאומים', ja: '演説', ko: '연설', zh: '演讲' },
  coalitionStabilityOutlook: { en: 'Coalition stability outlook', sv: 'Koalitionsstabilitetsutsikt', da: 'Koalitionsstabilitetsudsigt', no: 'Koalisjonsstabilitetsutsikt', fi: 'Koalitiostabiliteettiarvio', de: 'Koalitionsstabilitätsausblick', fr: 'Perspectives de stabilité de la coalition', es: 'Perspectiva de estabilidad de la coalición', nl: 'Coalitie stabiliteitsoverzicht', ar: 'توقعات استقرار الائتلاف', he: 'תחזית יציבות הקואליציה', ja: '連立安定性の展望', ko: '연립 안정성 전망', zh: '联合政府稳定性展望' },
  motionDenialContext: { en: 'Opposition motions historically pass at a low rate — parliamentary majority dynamics shape legislative outcomes.', sv: 'Oppositionsmotioner har historiskt sett låg bifallsfrekvens — parlamentariska majoritetsförhållanden styr lagstiftningsutfallen.', da: 'Oppositionsforslag har historisk set lav vedtagelsesfrekvens — parlamentariske flertalsdynamikker former lovgivningsresultaterne.', no: 'Opposisjonsforslag har historisk lav vedtaksrate — parlamentariske flertallsdynamikker former lovgivningsutfall.', fi: 'Oppositioaloitteilla on historiallisesti matala hyväksymisaste — parlamentin enemmistödynamiikka muokkaa lainsäädäntötuloksia.', de: 'Oppositionsanträge haben historisch geringe Erfolgsquoten — Mehrheitsverhältnisse im Parlament prägen die Gesetzgebungsergebnisse.', fr: "Les motions de l'opposition ont historiquement un faible taux d'adoption — la dynamique des majorités parlementaires façonne les résultats législatifs.", es: 'Las mociones de la oposición tienen históricamente bajas tasas de aprobación — la dinámica de la mayoría parlamentaria da forma a los resultados legislativos.', nl: 'Oppositiemoties hebben historisch gezien lage doorvoerpercentages — meerderheidsparlementsynamiek bepaalt wetgevingsresultaten.', ar: 'تتمتع مقترحات المعارضة تاريخياً بمعدلات مرور منخفضة — تشكّل ديناميكيات أغلبية البرلمان نتائج التشريع.', he: 'להצעות האופוזיציה יש היסטורית שיעורי מעבר נמוכים — דינמיקת הרוב הפרלמנטרית מעצבת תוצאות חקיקה.', ja: '野党動議は歴史的に低い通過率を持つ — 議会の多数決ダイナミクスが立法結果を形成する。', ko: '야당 동의는 역사적으로 낮은 통과율을 보임 — 의회 다수결 역학이 입법 결과를 형성함.', zh: '反对党动议历史上通过率较低——议会多数动态决定立法结果。' },
  insufficientData: { en: 'Insufficient party activity data for this period.', sv: 'Otillräckliga partiaktivitetsdata för denna period.', da: 'Utilstrækkelige partiaktivitetsdata for denne periode.', no: 'Utilstrekkelige partiaktivitetsdata for denne perioden.', fi: 'Puoluetoimintatiedot ovat riittämättömät tälle ajanjaksolle.', de: 'Unzureichende Parteiak­tivitätsdaten für diesen Zeitraum.', fr: "Données d'activité des partis insuffisantes pour cette période.", es: 'Datos de actividad de partido insuficientes para este período.', nl: 'Onvoldoende partijactiviteitsgegevens voor deze periode.', ar: 'بيانات نشاط الحزب غير كافية لهذه الفترة.', he: 'נתוני פעילות מפלגתית לא מספיקים לתקופה זו.', ja: 'この期間の政党活動データが不十分です。', ko: '이 기간에 대한 정당 활동 데이터가 불충분합니다.', zh: '本期政党活动数据不足。' },
};
 
/** Return the label for a language key, falling back to English. */
function ml(lang: Language | string, key: keyof typeof MONTHLY_LABELS): string {
  const map = MONTHLY_LABELS[key] as Record<string, string>;
  return map[lang as string] ?? map['en'] ?? key;
}
 
/**
 * Generate the "Month in Numbers" statistical summary section.
 * Renders document type counts, speech count, and month-over-month trend.
 */
function generateMonthInNumbers(metrics: MonthlyMetrics, lang: Language | string): string {
  const prevDiff = metrics.totalDocuments - metrics.previousMonthDocCount;
  const prevSign = prevDiff > 0 ? '+' : '';
  const availableMonthCounts = [
    metrics.totalDocuments,
    metrics.previousMonthDocCount,
    metrics.twoMonthsAgoDocCount,
  ].filter(count => count > 0);
  const rollingAvg = availableMonthCounts.length > 0
    ? Math.round(availableMonthCounts.reduce((a, b) => a + b, 0) / availableMonthCounts.length)
    : metrics.totalDocuments;
 
  let html = `\n    <h2>${escapeHtml(ml(lang, 'monthInNumbers'))}</h2>\n`;
  html += `    <div class="context-box">\n      <ul>\n`;
  html += `        <li><strong>${escapeHtml(ml(lang, 'totalDocuments'))}:</strong> ${escapeHtml(String(metrics.totalDocuments))}</li>\n`;
  html += `        <li><strong>${escapeHtml(ml(lang, 'reports'))}:</strong> ${escapeHtml(String(metrics.reportCount))}</li>\n`;
  html += `        <li><strong>${escapeHtml(ml(lang, 'propositions'))}:</strong> ${escapeHtml(String(metrics.propositionCount))}</li>\n`;
  html += `        <li><strong>${escapeHtml(ml(lang, 'motions'))}:</strong> ${escapeHtml(String(metrics.motionCount))}</li>\n`;
  html += `        <li><strong>${escapeHtml(ml(lang, 'speeches'))}:</strong> ${escapeHtml(String(metrics.speechCount))}</li>\n`;
  Eif (metrics.previousMonthDocCount > 0) {
    html += `        <li><em>${escapeHtml(ml(lang, 'trendVsPrevMonth'))}:</em> ${escapeHtml(prevSign)}${escapeHtml(String(prevDiff))}</li>\n`;
  }
  Eif (metrics.twoMonthsAgoDocCount > 0 || metrics.previousMonthDocCount > 0) {
    html += `        <li><em>${escapeHtml(ml(lang, 'trendVs2MonthsAgo'))}:</em> ${escapeHtml(String(rollingAvg))}</li>\n`;
  }
  html += `      </ul>\n    </div>\n`;
  return html;
}
 
/**
 * Generate the "Party Performance Rankings" section.
 * Ranks parties by combined legislative and debate activity.
 */
function generatePartyRankings(metrics: MonthlyMetrics, lang: Language | string): string {
  let html = `\n    <h2>${escapeHtml(ml(lang, 'partyRankings'))}</h2>\n`;
 
  if (!metrics.partyRankings || metrics.partyRankings.length === 0) {
    html += `    <div class="context-box">\n`;
    html += `      <p>${escapeHtml(ml(lang, 'insufficientData'))}</p>\n`;
    html += `    </div>\n`;
    return html;
  }
 
  html += `    <div class="context-box">\n      <ol>\n`;
  // Show top 8 parties — matches the 8 Swedish parliamentary parties and keeps the list scannable
  metrics.partyRankings.slice(0, 8).forEach((entry, idx) => {
    const motionLabel = ml(lang, 'motionsFiled');
    const speechLabel = ml(lang, 'speechesDelivered');
    const rankLabel = ml(lang, 'activityRank');
    html += `        <li><strong>${escapeHtml(entry.party)}</strong> — `;
    html += `${escapeHtml(String(entry.motionCount))} ${escapeHtml(motionLabel)}, `;
    html += `${escapeHtml(String(entry.speechCount))} ${escapeHtml(speechLabel)}`;
    if (idx === 0) html += ` (${escapeHtml(rankLabel)} #1)`;
    html += `</li>\n`;
  });
  html += `      </ol>\n    </div>\n`;
  return html;
}
 
/**
 * Generate the "Legislative Efficiency" metrics section.
 * Shows committee throughput rate and opposition motion context.
 */
function generateLegislativeEfficiency(metrics: MonthlyMetrics, lang: Language | string): string {
  let html = `\n    <h2>${escapeHtml(ml(lang, 'legislativeEfficiency'))}</h2>\n`;
  html += `    <div class="context-box">\n      <ul>\n`;
 
  Iif (metrics.propositionCount > 0) {
    const ratio = metrics.legislativeEfficiencyRate.toFixed(2);
    html += `        <li><strong>${escapeHtml(ml(lang, 'efficiencyRate'))}:</strong> ${escapeHtml(ratio)} (${escapeHtml(String(metrics.reportCount))} / ${escapeHtml(String(metrics.propositionCount))})</li>\n`;
  }
 
  html += `        <li><small>${escapeHtml(ml(lang, 'motionDenialContext'))}</small></li>\n`;
  html += `      </ul>\n    </div>\n`;
  return html;
}
 
/**
 * Generate the "Strategic Outlook" section.
 * Connects current-month trends to coalition stability and upcoming dynamics.
 */
function generateStrategicOutlook(metrics: MonthlyMetrics, data: ArticleContentData, lang: Language | string): string {
  const cia = data.ciaContext;
  let html = `\n    <h2>${escapeHtml(ml(lang, 'strategicOutlook'))}</h2>\n`;
  html += `    <div class="context-box">\n      <ul>\n`;
 
  // Activity trajectory based on month-over-month comparison
  Eif (metrics.previousMonthDocCount > 0) {
    const isIncreasing = metrics.totalDocuments > metrics.previousMonthDocCount;
    const trajectoryTemplates: Record<string, (inc: boolean) => string> = {
      en: inc => inc ? 'Legislative activity is accelerating — document volume is up month-over-month.' : 'Legislative activity is decelerating — document volume is down month-over-month.',
      sv: inc => inc ? 'Riksdagsaktiviteten accelererar — dokumentvolymen ökar månad för månad.' : 'Riksdagsaktiviteten avtar — dokumentvolymen minskar månad för månad.',
      da: inc => inc ? 'Den lovgivende aktivitet accelererer — dokumentmængden stiger måned for måned.' : 'Den lovgivende aktivitet aftager — dokumentmængden falder måned for måned.',
      no: inc => inc ? 'Den lovgivende aktiviteten akselererer — dokumentvolumet øker måned for måned.' : 'Den lovgivende aktiviteten avtar — dokumentvolumet synker måned for måned.',
      fi: inc => inc ? 'Lainsäädäntöaktiivisuus kiihtyy — asiakirjojen määrä kasvaa kuukaudesta toiseen.' : 'Lainsäädäntöaktiivisuus hidastuu — asiakirjojen määrä laskee kuukaudesta toiseen.',
      de: inc => inc ? 'Die Gesetzgebungsaktivität nimmt zu — das Dokumentenvolumen steigt von Monat zu Monat.' : 'Die Gesetzgebungsaktivität nimmt ab — das Dokumentenvolumen sinkt von Monat zu Monat.',
      fr: inc => inc ? "L'activité législative s'accélère — le volume documentaire augmente d'un mois sur l'autre." : "L'activité législative ralentit — le volume documentaire diminue d'un mois sur l'autre.",
      es: inc => inc ? 'La actividad legislativa está acelerando — el volumen de documentos aumenta mes a mes.' : 'La actividad legislativa está desacelerando — el volumen de documentos disminuye mes a mes.',
      nl: inc => inc ? 'De wetgevende activiteit neemt toe — het documentvolume stijgt maand over maand.' : 'De wetgevende activiteit neemt af — het documentvolume daalt maand over maand.',
      ar: inc => inc ? 'النشاط التشريعي يتسارع — حجم الوثائق يزداد شهراً بعد شهر.' : 'النشاط التشريعي يتباطأ — حجم الوثائق يتراجع شهراً بعد شهر.',
      he: inc => inc ? 'הפעילות החקיקתית מתאיצה — נפח המסמכים עולה מחודש לחודש.' : 'הפעילות החקיקתית מואטת — נפח המסמכים יורד מחודש לחודש.',
      ja: inc => inc ? '立法活動が加速しています — 文書量が前月比で増加しています。' : '立法活動が減速しています — 文書量が前月比で減少しています。',
      ko: inc => inc ? '입법 활동이 가속화되고 있습니다 — 문서량이 전월 대비 증가하고 있습니다.' : '입법 활동이 감속되고 있습니다 — 문서량이 전월 대비 감소하고 있습니다.',
      zh: inc => inc ? '立法活动正在加速——文件数量环比增加。' : '立法活动正在减速——文件数量环比减少。',
    };
    const tpl = trajectoryTemplates[lang as string];
    const trajectoryText = tpl ? tpl(isIncreasing) : (trajectoryTemplates['en'] ?? (inc => inc ? 'Legislative activity is accelerating.' : 'Legislative activity is decelerating.'))(isIncreasing);
    html += `        <li>${escapeHtml(trajectoryText)}</li>\n`;
  }
 
  // Coalition stability signal from CIA context
  Eif (cia) {
    const stabilityLabel = ml(lang, 'coalitionStabilityOutlook');
    const stabilityScore = cia.coalitionStability.stabilityScore;
    const riskLevel = cia.coalitionStability.riskLevel;
    html += `        <li><strong>${escapeHtml(stabilityLabel)}:</strong> ${escapeHtml(String(stabilityScore))}/100 (${escapeHtml(riskLevel)})</li>\n`;
  }
 
  html += `      </ul>\n    </div>\n`;
  return html;
}
 
/**
 * Generate a rich monthly review article with "Month in Numbers", party rankings,
 * legislative efficiency metrics, and a strategic outlook section.
 * Falls back to generic content when monthlyMetrics is absent.
 */
export function generateMonthlyReviewContent(data: ArticleContentData, lang: Language | string): string {
  // Base document analysis (same as weekly review / generic)
  let content = generateGenericContent(data, lang);
 
  const metrics = data.monthlyMetrics;
  if (!metrics) return content;
 
  // Append monthly-specific sections
  content += generateMonthInNumbers(metrics, lang);
  content += generatePartyRankings(metrics, lang);
  content += generateLegislativeEfficiency(metrics, lang);
  content += generateStrategicOutlook(metrics, data, lang);
 
  return content;
}
 
/**
 * Generate Month-Ahead article content with strategic legislative forecasting.
 * Extends week-ahead calendar coverage with committee pipeline tracking,
 * propositions in pipeline, and motion trend analysis.
 */
export function generateMonthAheadContent(data: ArticleContentData, lang: Language | string): string {
  // Base calendar/event content (handles events + upcoming documents)
  let content = generateWeekAheadContent(data as WeekAheadData, lang);
 
  const propositions = data.propositions ?? [];
  const reports = data.reports ?? [];
  const motions = data.motions ?? [];
 
  // ── Strategic Legislative Outlook ────────────────────────────────────────
  if (propositions.length > 0) {
    const outlookLabel = lang === 'sv' ? 'Strategisk lagstiftningsutsikt'
      : lang === 'de' ? 'Strategischer Gesetzgebungsausblick'
      : lang === 'fr' ? 'Perspectives législatives stratégiques'
      : lang === 'es' ? 'Perspectiva legislativa estratégica'
      : lang === 'da' ? 'Strategisk lovgivningsmæssigt udsyn'
      : lang === 'no' ? 'Strategisk lovgivningsmessig utsikt'
      : lang === 'fi' ? 'Strateginen lainsäädäntönäkymä'
      : lang === 'nl' ? 'Strategisch wetgevingsoverzicht'
      : lang === 'ar' ? 'التوقعات التشريعية الاستراتيجية'
      : lang === 'he' ? 'תחזית חקיקתית אסטרטגית'
      : lang === 'ja' ? '戦略的立法見通し'
      : lang === 'ko' ? '전략적 입법 전망'
      : lang === 'zh' ? '战略立法展望'
      : 'Strategic Legislative Outlook';
    content += `\n    <h2>${outlookLabel}</h2>\n`;
 
    // Lede: how many propositions are in the pipeline
    const propLedeTemplates: Record<string, (n: number) => string> = {
      sv: n => `${n} propositioner befinner sig i den lagstiftande processen denna månad.`,
      da: n => `${n} lovforslag befinder sig i den lovgivningsmæssige proces denne måned.`,
      no: n => `${n} proposisjoner er i den lovgivningsmessige prosessen denne måneden.`,
      fi: n => `${n} hallituksen esitystä on lainsäädäntöprosessissa tässä kuussa.`,
      de: n => `${n} Regierungsvorlagen befinden sich diesen Monat im Gesetzgebungsprozess.`,
      fr: n => `${n} propositions gouvernementales sont en cours de traitement législatif ce mois-ci.`,
      es: n => `${n} proposiciones gubernamentales se encuentran en el proceso legislativo este mes.`,
      nl: n => `${n} regeringsvoorstellen bevinden zich deze maand in het wetgevingsproces.`,
      ar: n => `${n} مقترحات حكومية في المسار التشريعي هذا الشهر.`,
      he: n => `${n} הצעות ממשלה נמצאות בתהליך החקיקתי החודש.`,
      ja: n => `今月は${n}件の政府提案が立法プロセスにあります。`,
      ko: n => `이달 ${n}건의 정부 법안이 입법 프로세스에 있습니다.`,
      zh: n => `本月${n}项政府提案处于立法审议过程中。`,
    };
    const propLedeTpl = propLedeTemplates[lang as string];
    const propLede = propLedeTpl
      ? propLedeTpl(propositions.length)
      : `${propositions.length} government propositions are in the legislative pipeline this month.`;
    content += `    <p class="article-lede">${escapeHtml(propLede)}</p>\n`;
 
    propositions.slice(0, 8).forEach(prop => {  // 8 propositions: readable summary without overwhelming
      const rec = prop as Record<string, string>;
      const titleText = rec['titel'] || rec['title'] || rec['doktyp'] || 'Proposition';
      const escapedTitle = escapeHtml(titleText);
      const titleHtml = (rec['titel'] && !rec['title']) ? svSpan(escapedTitle, lang) : escapedTitle;
      const significance = generatePolicySignificance(prop, lang);
      const dokId = rec['dok_id'] ?? rec['id'] ?? '';
      const urlBase = 'https://riksdagen.se/sv/dokument-och-lagar/dokument/';
      const safeUrl = dokId ? sanitizeUrl(`${urlBase}${encodeURIComponent(dokId)}/`) : '';
      content += `    <div class="document-entry">\n`;
      content += `      <h4>${safeUrl ? `<a href="${safeUrl}" target="_blank" rel="noopener noreferrer">` : ''}${titleHtml}${safeUrl ? '</a>' : ''}</h4>\n`;
      Eif (significance) {
        content += `      <p class="policy-significance">${escapeHtml(significance)}</p>\n`;
      }
      content += `    </div>\n`;
    });
  }
 
  // ── Committee Pipeline ────────────────────────────────────────────────────
  Iif (reports.length > 0) {
    const pipelineLabel = lang === 'sv' ? 'Utskottspipeline'
      : lang === 'de' ? 'Ausschusspipeline'
      : lang === 'fr' ? 'Pipeline des commissions'
      : lang === 'es' ? 'Proceso en comité'
      : lang === 'da' ? 'Udvalgspipeline'
      : lang === 'no' ? 'Komitépipeline'
      : lang === 'fi' ? 'Valiokuntaputkisto'
      : lang === 'nl' ? 'Commissiepijplijn'
      : lang === 'ar' ? 'مسار اللجان'
      : lang === 'he' ? 'צינור הוועדות'
      : lang === 'ja' ? '委員会パイプライン'
      : lang === 'ko' ? '위원회 파이프라인'
      : lang === 'zh' ? '委员会审议流程'
      : 'Committee Pipeline';
    content += `\n    <h2>${pipelineLabel}</h2>\n`;
 
    // Group reports by committee
    const byCommittee: Record<string, RawDocument[]> = {};
    reports.forEach(r => {
      const rec2 = r as Record<string, string>;
      const key = rec2.organ ?? rec2.committee ?? 'unknown';
      if (!byCommittee[key]) byCommittee[key] = [];
      byCommittee[key].push(r);
    });
 
    Object.entries(byCommittee).slice(0, 5).forEach(([committeeCode, committeeReports]) => { // up to 5 committees
      const committeeName = getCommitteeName(committeeCode, lang);
      content += `    <h3>${escapeHtml(committeeName)}</h3>\n`;
      committeeReports.slice(0, 3).forEach(report => {  // 3 reports per committee keeps the section scannable
        const rec = report as Record<string, string>;
        const titleText = rec['titel'] || rec['title'] || 'Report';
        const escapedTitle = escapeHtml(titleText);
        const titleHtml = (rec['titel'] && !rec['title']) ? svSpan(escapedTitle, lang) : escapedTitle;
        const dokId = rec['dok_id'] ?? '';
        const urlBase = 'https://riksdagen.se/sv/dokument-och-lagar/dokument/';
        const safeUrl = dokId ? sanitizeUrl(`${urlBase}${encodeURIComponent(dokId)}/`) : '';
        content += `    <div class="document-entry">\n`;
        content += `      <h4>${safeUrl ? `<a href="${safeUrl}" target="_blank" rel="noopener noreferrer">` : ''}${titleHtml}${safeUrl ? '</a>' : ''}</h4>\n`;
        content += `    </div>\n`;
      });
    });
  }
 
  // ── Policy Trends ─────────────────────────────────────────────────────────
  Iif (motions.length > 0) {
    const trendsLabel = lang === 'sv' ? 'Politiska trender'
      : lang === 'de' ? 'Politische Trends'
      : lang === 'fr' ? 'Tendances politiques'
      : lang === 'es' ? 'Tendencias políticas'
      : lang === 'da' ? 'Politiske tendenser'
      : lang === 'no' ? 'Politiske trender'
      : lang === 'fi' ? 'Poliittiset trendit'
      : lang === 'nl' ? 'Politieke trends'
      : lang === 'ar' ? 'الاتجاهات السياسية'
      : lang === 'he' ? 'מגמות מדיניות'
      : lang === 'ja' ? '政策トレンド'
      : lang === 'ko' ? '정책 트렌드'
      : lang === 'zh' ? '政策趋势'
      : 'Policy Trends';
    content += `\n    <h2>${trendsLabel}</h2>\n`;
 
    // Identify active policy domains from motions
    const allTrendDomains = new Set<string>();
    motions.forEach(m => detectPolicyDomains(m, lang).forEach(d => allTrendDomains.add(d)));
 
    // Party activity breakdown
    const byPartyTrend: Record<string, number> = {};
    motions.forEach(m => {
      const party = normalizePartyKey((m as Record<string, string>).parti);
      byPartyTrend[party] = (byPartyTrend[party] || 0) + 1;
    });
 
    if (allTrendDomains.size > 0) {
      const domainList = Array.from(allTrendDomains).slice(0, 5).join(', ');  // top 5 domains for readability
      const domainIntroTemplates: Record<string, (d: string, n: number) => string> = {
        sv: (d, n) => `${n} motioner identifierar aktiva policydomäner: ${d}.`,
        da: (d, n) => `${n} motioner identificerer aktive politikdomæner: ${d}.`,
        no: (d, n) => `${n} forslag identifiserer aktive politikkdomener: ${d}.`,
        fi: (d, n) => `${n} aloitetta tunnistaa aktiiviset politiikka-alueet: ${d}.`,
        de: (d, n) => `${n} Anträge identifizieren aktive Politikbereiche: ${d}.`,
        fr: (d, n) => `${n} motions identifient des domaines politiques actifs: ${d}.`,
        es: (d, n) => `${n} mociones identifican áreas de política activas: ${d}.`,
        nl: (d, n) => `${n} moties identificeren actieve beleidsgebieden: ${d}.`,
        ar: (d, n) => `${n} اقتراحات تُحدد مجالات السياسة النشطة: ${d}.`,
        he: (d, n) => `${n} הצעות מזהות תחומי מדיניות פעילים: ${d}.`,
        ja: (d, n) => `${n}件の動議が活発な政策領域を特定しています: ${d}。`,
        ko: (d, n) => `${n}건의 동의가 활발한 정책 분야를 식별합니다: ${d}.`,
        zh: (d, n) => `${n}项动议确定了活跃的政策领域:${d}。`,
      };
      const domTpl = domainIntroTemplates[lang as string];
      const domainIntroRaw = domTpl
        ? domTpl(domainList, motions.length)
        : `${motions.length} motions identify active policy domains: ${domainList}.`;
      const domainIntro = escapeHtml(domainIntroRaw);
      content += `    <p>${domainIntro}</p>\n`;
    }
 
    // Top parties by motion volume
    const topParties = Object.entries(byPartyTrend)
      .filter(([k]) => k !== 'unknown' && k !== 'other')
      .sort((a, b) => b[1] - a[1])
      .slice(0, 4);  // 4 parties: covers the typical Swedish governing+major-opposition parties
 
    if (topParties.length > 0) {
      content += `    <ul>\n`;
      topParties.forEach(([party, count]) => {
        const partyMotionTemplates: Record<string, (p: string, n: number) => string> = {
          sv: (p, n) => `${p}: ${n} motioner inlämnade`,
          da: (p, n) => `${p}: ${n} motioner indgivet`,
          no: (p, n) => `${p}: ${n} forslag innsendt`,
          fi: (p, n) => `${p}: ${n} aloitetta jätetty`,
          de: (p, n) => `${p}: ${n} Anträge eingereicht`,
          fr: (p, n) => `${p}: ${n} motions déposées`,
          es: (p, n) => `${p}: ${n} mociones presentadas`,
          nl: (p, n) => `${p}: ${n} moties ingediend`,
          ar: (p, n) => `${p}: ${n} اقتراحات مقدمة`,
          he: (p, n) => `${p}: ${n} הצעות הוגשו`,
          ja: (p, n) => `${p}: ${n}件の動議を提出`,
          ko: (p, n) => `${p}: ${n}건의 동의 제출`,
          zh: (p, n) => `${p}: ${n}项动议提交`,
        };
        const partyTpl = partyMotionTemplates[lang as string];
        const partyEntry = partyTpl
          ? partyTpl(escapeHtml(party), count)
          : `${escapeHtml(party)}: ${count} motions submitted`;
        content += `      <li>${partyEntry}</li>\n`;
      });
      content += `    </ul>\n`;
    }
  }
 
  return content;
}