// L-9 架构文档同步: 005_architecture_data.json dependencyMatrix 添加 grades 依赖
import { readFileSync, writeFileSync } from 'node:fs';

const FILE = 'e:/Desktop/CICD/docs/architecture/005_architecture_data.json';
const src = readFileSync(FILE, 'utf8');

// 1) dependsOn: attendance 模块添加 "grades"
//    定位 attendance.dependsOn 数组的 "notifications" 行（属于 attendance 模块，而非其他模块）
//    通过上下文唯一性定位：parent, notifications 后接 ],\n      "uses": {
const depMarker = '"parent",\r\n        "notifications"\r\n      ],\r\n      "uses": {\r\n        "shared": [\r\n          "db",\r\n          "auth-guard.requirePermission",\r\n          "auth-guard.getAuthContext",\r\n          "db.schema.attendanceRecords",';
const depReplacement = '"parent",\r\n        "notifications",\r\n        "grades"\r\n      ],\r\n      "uses": {\r\n        "shared": [\r\n          "db",\r\n          "auth-guard.requirePermission",\r\n          "auth-guard.getAuthContext",\r\n          "db.schema.attendanceRecords",';

if (!src.includes(depMarker)) {
  // 尝试 LF
  const depMarkerLF = depMarker.replace(/\r\n/g, '\n');
  if (!src.includes(depMarkerLF)) {
    console.error('dependsOn marker not found');
    process.exit(1);
  }
  // 用 LF 版本替换
  const depReplacementLF = depReplacement.replace(/\r\n/g, '\n');
  const updated = src.replace(depMarkerLF, depReplacementLF);
  writeFileSync(FILE, updated, 'utf8');
  console.log('dependsOn updated (LF)');
} else {
  const updated = src.replace(depMarker, depReplacement);
  writeFileSync(FILE, updated, 'utf8');
  console.log('dependsOn updated (CRLF)');
}
