summaryrefslogtreecommitdiff
path: root/lib/swp/api-client.ts
blob: e54a4c4d869d3284733917f0fed8aec94a9f2c13 (plain)
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
"use server";

/**
 * SWP API Client
 * 
 * ## 목적
 * 외부 SWP 시스템의 REST API를 호출하는 저수준 클라이언트입니다.
 * 
 * ## 설계 원칙
 * 1. **단순 API 호출만 담당** - 비즈니스 로직은 document-service.ts에서 처리
 * 2. **타입 안전성** - API 응답 타입을 명확히 정의
 * 3. **에러 핸들링** - API 실패 시 명확한 에러 메시지 제공
 * 4. **디버깅 지원** - 모든 API 호출을 debugLog로 추적
 * 
 * ## 주요 API
 * - `fetchGetVDRDocumentList`: 문서 마스터 조회 (필터링 지원)
 * - `fetchGetExternalInboxList`: 파일 정보 조회 (업로드된 파일 포함)
 * - `fetchGetRevTreeCompleteList`: 문서 리비전 트리 조회 (NEW)
 * - `fetchGetActivityFileList`: Rev-Activity-File 계층 구조 조회
 * - `callSaveInBoxList`: 파일 업로드 메타데이터 등록
 * - `callSaveInBoxListCancelStatus`: Standby 파일 취소
 * 
 * ## 사용 예시
 * ```typescript
 * // 문서 목록 조회
 * const documents = await fetchGetVDRDocumentList({
 *   proj_no: "SN2190",
 *   doc_gb: "V",
 *   vndrCd: "SE00100"
 * });
 * 
 * // 리비전 트리 조회
 * const tree = await fetchGetRevTreeCompleteList({
 *   proj_no: "SN2190",
 *   doc_no: "C168-SH-SBN08-XG-20118-01"
 * });
 * ```
 * 
 * @see lib/swp/document-service.ts - 비즈니스 로직 레이어
 * @see lib/swp/vendor-actions.ts - 서버 액션 (권한 체크)
 * @see lib/swp/README.md - 전체 시스템 문서
 * @see lib/swp/REVISION_TREE_USAGE.md - 리비전 트리 API 활용 가이드
 */

// ============================================================================
// SWP API 클라이언트
// ============================================================================

const SWP_BASE_URL = process.env.SWP_API_URL || "http://60.100.99.217/DDC";

// ============================================================================
// API 요청 타입 정의
// ============================================================================

/**
 * 문서 리스트 조회 필터
 */
export interface GetVDRDocumentListFilter {
  proj_no: string; // 필수
  doc_gb: "M" | "V"; // 필수 (M=MDR, V=VDR)
  ctgry?: string; // HULL or TOP
  pkgNo?: string;
  vndrCd?: string;
  pic_deptcd?: string;
  doc_type?: string;
  displn?: string;
  mat_cd?: string;
  proj_nm?: string;
  stage?: string;
  own_doc_no?: string;
  doc_title?: string;
  lang_gb?: string;
}

/**
 * 첨부파일 리스트 조회 필터
 */
export interface GetExternalInboxListFilter {
  projNo: string; // 필수
  pkgNo?: string;
  vndrCd?: string;
  stage?: string;
  owndocno?: string;
  doctitle?: string;
}

export interface SwpDocumentApiResponse {
  // 필수 필드
  DOC_NO: string;
  DOC_TITLE: string;
  PROJ_NO: string;
  CPY_CD: string;
  CPY_NM: string;
  PIC_NM: string;
  PIC_DEPTNM: string;
  SKL_CD: string;
  CRTER: string;
  CRTE_DTM: string;
  CHGR: string;
  CHG_DTM: string;

  // 선택적 필드 (null 가능)
  DOC_GB: string | null;
  DOC_TYPE: string | null;
  DOC_CLS: string | null; // Document Class (A, B, C 등)
  OWN_DOC_NO: string | null;
  SHI_DOC_NO: string | null;
  PROJ_NM: string | null;
  PKG_NO: string | null;
  MAT_CD: string | null;
  MAT_NM: string | null;
  DISPLN: string | null;
  CTGRY: string | null;
  VNDR_CD: string | null;
  PIC_DEPTCD: string | null;
  LTST_REV_NO: string | null;
  LTST_REV_SEQ: string | null;
  LTST_ACTV_STAT: string | null;
  STAGE: string | null;
  MOD_TYPE: string | null;
  ACT_TYPE_NM: string | null;
  USE_YN: string | null;
  REV_DTM: string | null;
}

export interface SwpFileApiResponse {
  // 필수 필드
  OWN_DOC_NO: string;
  REV_NO: string;
  STAGE: string;
  FILE_NM: string;
  FILE_SEQ: string;
  CRTER: string;
  CRTE_DTM: string;
  CHGR: string;
  CHG_DTM: string;

  // 선택적 필드 (null 가능)
  FILE_SZ: string | null;
  FLD_PATH: string | null;
  ACTV_NO: string | null;
  ACTV_SEQ: string | null;
  BOX_SEQ: string | null;
  OFDC_NO: string | null;
  PROJ_NO: string | null;
  PKG_NO: string | null;
  VNDR_CD: string | null;
  CPY_CD: string | null;
  STAT: string | null;
  STAT_NM: string | null;
  IDX: string | null;
  NOTE1: string | null; // DC Note
  NOTE2: string | null; // Eng Note
}

// ============================================================================
// 공통 API 호출 함수
// ============================================================================

async function callSwpApi<T>(
  endpoint: string,
  body: Record<string, unknown>,
  resultKey: string
): Promise<T[]> {
  const url = `${SWP_BASE_URL}/Services/WebService.svc/${endpoint}`;

  console.log(`[SWP API] 호출: ${endpoint}`, body);

  try {
    const response = await fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(body),
      signal: AbortSignal.timeout(30000), // 30초
    });

    if (!response.ok) {
      throw new Error(
        `SWP API 오류: ${response.status} ${response.statusText}`
      );
    }

    const data = await response.json();

    if (!data[resultKey]) {
      throw new Error(`API 응답 형식 오류: ${resultKey} 없음`);
    }

    console.log(`[SWP API] 성공: ${data[resultKey].length}개 조회`);

    return data[resultKey] as T[];
  } catch (error) {
    if (error instanceof Error) {
      if (error.name === "AbortError") {
        throw new Error(`API 타임아웃: 30초 초과`);
      }
      throw new Error(`${endpoint} 실패: ${error.message}`);
    }
    throw error;
  }
}

/**
 * SWP API 호출 (단일 값 반환용)
 * 배열이 아닌 단일 값(숫자, 문자열 등)을 반환하는 API용
 */
async function callSwpApiSingle<T>(
  endpoint: string,
  body: Record<string, unknown>,
  resultKey: string
): Promise<T> {
  const url = `${SWP_BASE_URL}/Services/WebService.svc/${endpoint}`;

  console.log(`[SWP API] 호출: ${endpoint}`, body);

  try {
    const response = await fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(body),
      signal: AbortSignal.timeout(30000), // 30초
    });

    if (!response.ok) {
      throw new Error(
        `SWP API 오류: ${response.status} ${response.statusText}`
      );
    }

    const data = await response.json();

    if (data[resultKey] === undefined) {
      throw new Error(`API 응답 형식 오류: ${resultKey} 없음`);
    }

    console.log(`[SWP API] 성공: ${endpoint}`, data[resultKey]);

    return data[resultKey] as T;
  } catch (error) {
    if (error instanceof Error) {
      if (error.name === "AbortError") {
        throw new Error(`API 타임아웃: 30초 초과`);
      }
      throw new Error(`${endpoint} 실패: ${error.message}`);
    }
    throw error;
  }
}

// ============================================================================
// 서버 액션: 문서 리스트 조회
// ============================================================================

/**
 * 문서 리스트 조회 (GetVDRDocumentList)
 * @param filter 조회 필터
 */
export async function fetchGetVDRDocumentList(
  filter: GetVDRDocumentListFilter
): Promise<SwpDocumentApiResponse[]> {
  // doc_gb 기본값 설정
  const body = {
    proj_no: filter.proj_no,
    doc_gb: filter.doc_gb || "V", // 기본값 V
    ctgry: filter.ctgry || "",
    pkgNo: filter.pkgNo || "",
    vndrCd: filter.vndrCd || "",
    pic_deptcd: filter.pic_deptcd || "",
    doc_type: filter.doc_type || "",
    displn: filter.displn || "",
    mat_cd: filter.mat_cd || "",
    proj_nm: filter.proj_nm || "",
    stage: filter.stage || "",
    own_doc_no: filter.own_doc_no || "",
    doc_title: filter.doc_title || "",
    lang_gb: filter.lang_gb || "",
  };

  return callSwpApi<SwpDocumentApiResponse>(
    "GetVDRDocumentList",
    body,
    "GetVDRDocumentListResult"
  );
}

// ============================================================================
// 서버 액션: 첨부파일 리스트 조회
// ============================================================================

/**
 * 첨부파일 리스트 조회 (GetExternalInboxList)
 * @param filter 조회 필터
 */
export async function fetchGetExternalInboxList(
  filter: GetExternalInboxListFilter
): Promise<SwpFileApiResponse[]> {
  const body = {
    projNo: filter.projNo,
    pkgNo: filter.pkgNo || "",
    vndrCd: filter.vndrCd || "",
    stage: filter.stage || "",
    owndocno: filter.owndocno || "",
    doctitle: filter.doctitle || "",
  };

  return callSwpApi<SwpFileApiResponse>(
    "GetExternalInboxList",
    body,
    "GetExternalInboxListResult"
  );
}

// ============================================================================
// 서버 액션: 프로젝트 데이터 일괄 조회
// ============================================================================

/**
 * 프로젝트의 문서 + 파일 리스트 동시 조회
 * @param projectNo 프로젝트 번호 (예: "SN2190")
 * @param docGb 문서 구분 (M=MDR, V=VDR, 기본값: V)
 */
export async function fetchSwpProjectData(
  projectNo: string,
  docGb: "M" | "V" = "V"
): Promise<{
  documents: SwpDocumentApiResponse[];
  files: SwpFileApiResponse[];
}> {
  console.log(`[SWP API] 프로젝트 ${projectNo} 데이터 조회 시작`);
  const startTime = Date.now();

  try {
    // 병렬 호출
    const [documents, files] = await Promise.all([
      fetchGetVDRDocumentList({
        proj_no: projectNo,
        doc_gb: docGb,
      }),
      fetchGetExternalInboxList({
        projNo: projectNo,
      }),
    ]);

    const duration = Date.now() - startTime;
    console.log(
      `[SWP API] 조회 완료: 문서 ${documents.length}개, 파일 ${files.length}개 (${duration}ms)`
    );

    return { documents, files };
  } catch (error) {
    console.error(`[SWP API] 조회 실패:`, error);
    throw error;
  }
}

// ============================================================================
// 파일 다운로드 URL 생성
// ============================================================================

/**
 * SWP 파일 다운로드 URL 생성
 */
export async function getSwpFileDownloadUrl(file: {
  FLD_PATH: string;
  FILE_NM: string;
}): Promise<string> {
  // FLD_PATH: "\SN2190\C00035\\20170217180135"
  // FILE_NM: "C168-SH-SBN08-XG-20118-01_04_IFC_20170216.pdf"

  const encodedPath = encodeURIComponent(file.FLD_PATH);
  const encodedName = encodeURIComponent(file.FILE_NM);

  return `${SWP_BASE_URL}/Files/${encodedPath}/${encodedName}`;
}

/**
 * SWP 파일 직접 다운로드 (Blob)
 */
export async function downloadSwpFile(file: {
  FLD_PATH: string;
  FILE_NM: string;
}): Promise<Blob> {
  const url = await getSwpFileDownloadUrl(file);

  const response = await fetch(url, {
    method: "GET",
    signal: AbortSignal.timeout(60000), // 1분
  });

  if (!response.ok) {
    throw new Error(`파일 다운로드 실패: ${response.status}`);
  }

  return response.blob();
}

// ============================================================================
// 통계 및 유틸리티
// ============================================================================

/**
 * API 응답 통계
 */
export interface SwpDataStats {
  projectNo: string;
  documentCount: number;
  fileCount: number;
  revisionCount: number;
  avgFilesPerDoc: number;
  stages: Record<string, number>;
  fileTypes: Record<string, number>;
  totalFileSize: number;
}

export async function analyzeSwpData(
  projectNo: string,
  documents: SwpDocumentApiResponse[],
  files: SwpFileApiResponse[]
): Promise<SwpDataStats> {
  // 리비전 카운트
  const revisionSet = new Set<string>();
  files.forEach((f) => revisionSet.add(`${f.OWN_DOC_NO}|${f.REV_NO}`));

  // 스테이지별 카운트
  const stages: Record<string, number> = {};
  files.forEach((f) => {
    stages[f.STAGE] = (stages[f.STAGE] || 0) + 1;
  });

  // 파일 타입별 카운트
  const fileTypes: Record<string, number> = {};
  files.forEach((f) => {
    const ext = f.FILE_NM.split(".").pop()?.toLowerCase() || "unknown";
    fileTypes[ext] = (fileTypes[ext] || 0) + 1;
  });

  // 총 파일 사이즈 (숫자로 변환 가능한 것만)
  const totalFileSize = files.reduce((sum, f) => {
    const size = f.FILE_SZ ? parseInt(f.FILE_SZ, 10) : 0;
    return sum + (isNaN(size) ? 0 : size);
  }, 0);

  return {
    projectNo,
    documentCount: documents.length,
    fileCount: files.length,
    revisionCount: revisionSet.size,
    avgFilesPerDoc:
      documents.length > 0 ? files.length / documents.length : 0,
    stages,
    fileTypes,
    totalFileSize,
  };
}

// ============================================================================
// 서버 액션: 리비전 트리 조회 (GetRevTreeCompleteList)
// ============================================================================

/**
 * 리비전 트리 조회 필터
 */
export interface GetRevTreeCompleteListFilter {
  proj_no: string; // 필수 (예: "SN2190")
  doc_no: string; // 필수 (예: "C168-SH-SBN08-XG-20118-01")
  stat?: string; // 상태 코드 필터 (선택)
  lang_gb?: string; // 언어 구분 (선택)
}

/**
 * 리비전 트리 노드 API 응답
 * 
 * 트리 구조:
 * - Root: 문서 자체
 * - Revision: 문서의 버전 (01, 02, 03...)
 * - Activity: 각 리비전에 속한 송수신 활동
 */
export interface RevTreeNodeApiResponse {
  // 노드 식별 정보
  SearchType: "Root" | "Revision" | "Activity"; // 노드 타입
  NodeName: string; // 노드 이름 (Activity의 경우 ACTV_NO)
  ParentName: string | null; // 부모 노드 이름
  SearchName: string; // 검색용 이름

  // Revision 정보 (SearchType이 "Revision"일 때)
  Stage: string; // IFA, IFC 등

  // Activity 정보 (SearchType이 "Activity"일 때)
  InOut: string; // "IN"=수신, "OUT"=송신
  StatusCode: string; // R00=Ready, S00=Ready, S30=Completed
  StatusName: string; // 상태명
  TransmittalNo: string; // 송장번호
  RefActivityNo: string; // 참조 Activity 번호

  // 메타 정보
  CreateDate: string;
  CreateEmpNo: string;
  ModifyDate: string;
  ModifyEmpNo: string;
}

/**
 * 리비전 트리 조회 (GetRevTreeCompleteList)
 * 
 * 문서의 전체 리비전 히스토리와 각 리비전별 Activity 목록을 트리 구조로 조회합니다.
 * 
 * @param filter 조회 필터
 * @returns 트리 구조의 노드 배열
 * 
 * @example
 * ```typescript
 * const tree = await fetchGetRevTreeCompleteList({
 *   proj_no: "SN2190",
 *   doc_no: "C168-SH-SBN08-XG-20118-01"
 * });
 * 
 * // Root 노드 찾기
 * const root = tree.find(n => n.SearchType === "Root");
 * 
 * // Revision 목록
 * const revisions = tree.filter(n => n.SearchType === "Revision");
 * 
 * // 특정 Revision의 Activity 목록
 * const rev04Activities = tree.filter(n => 
 *   n.SearchType === "Activity" && n.ParentName === "3" // Rev 04
 * );
 * ```
 */
export async function fetchGetRevTreeCompleteList(
  filter: GetRevTreeCompleteListFilter
): Promise<RevTreeNodeApiResponse[]> {
  const body = {
    proj_no: filter.proj_no,
    doc_no: filter.doc_no,
    stat: filter.stat || "",
    lang_gb: filter.lang_gb || "",
  };

  return callSwpApi<RevTreeNodeApiResponse>(
    "GetRevTreeCompleteList",
    body,
    "GetRevTreeCompleteListResult"
  );
}

// ============================================================================
// 서버 액션: Activity 및 파일 리스트 조회 (GetActivityFileList)
// ============================================================================

/**
 * Activity 파일 리스트 조회 필터
 */
export interface GetActivityFileListFilter {
  proj_no: string;
  doc_no: string;
  rev_seq?: string; // 선택적
}

/**
 * Activity 파일 API 응답
 */
export interface ActivityFileApiResponse {
  ACTV_NO: string;
  ACT_TYPE: string;
  DOC_NO: string;
  DOC_TITLE: string;
  REV_NO: string;
  REV_SEQ: string;
  STAGE: string;
  STAT: string; // R00=Receive, S30=Send, V00=Review
  FILE_TYPE: string; // "Receive", "Send", "Review"
  FILE_NM: string;
  FILE_SEQ: string;
  FILE_SZ: string;
  FILE_FMT: string;
  OWN_DOC_NO: string;
  TO_FROM: string; // 업체명
  OBJT_ID: string;
  DSC: string | null;
  BATCHUPLOAD_ID: string | null;
  TRNS_DTM: string | null;
  CRTER: string;
  CRTE_DTM: string;
}

/**
 * Activity 파일 리스트 조회 (GetActivityFileList)
 * @param filter 조회 필터
 */
export async function fetchGetActivityFileList(
  filter: GetActivityFileListFilter
): Promise<ActivityFileApiResponse[]> {
  const body = {
    proj_no: filter.proj_no,
    doc_no: filter.doc_no,
    rev_seq: filter.rev_seq || "",
  };

  return callSwpApi<ActivityFileApiResponse>(
    "GetActivityFileList",
    body,
    "GetActivityFileListResult"
  );
}

// ============================================================================
// 서버 액션: 파일 취소 (SaveInBoxListCancelStatus)
// ============================================================================

export interface CancelFileParams {
  boxSeq: string;
  actvSeq: string;
  chgr: string; // 취소 요청자 (evcp${userId})
}

/**
 * 파일 취소 API (SaveInBoxListCancelStatus)
 * @returns 취소된 파일 개수
 */
export async function callSaveInBoxListCancelStatus(
  params: CancelFileParams
): Promise<number> {
  const body = {
    boxSeq: params.boxSeq,
    actvSeq: params.actvSeq,
    chgr: params.chgr,
  };

  // 응답 키는 "SaveInBoxCancelStatusResult" (List 없음)
  const result = await callSwpApiSingle<number>(
    "SaveInBoxListCancelStatus",
    body,
    "SaveInBoxCancelStatusResult"
  );

  return result;
}

// ============================================================================
// 유틸리티: GetRevTreeCompleteList + GetActivityFileList 연동
// ============================================================================

/**
 * 리비전 트리 구조 파싱 결과
 */
export interface ParsedRevisionTree {
  docNo: string;
  revisions: Array<{
    revSeq: string; // "0", "1", "2", "3" (내부 시퀀스)
    revNo: string; // "01", "02", "03", "04" (표시용)
    stage: string; // "IFA", "IFC"
    activities: Array<{
      actvNo: string; // NodeName (예: "SHIK2017022008520078313")
      inOut: "IN" | "OUT";
      statusCode: string;
      statusName: string;
      transmittalNo: string;
      refActivityNo: string;
      createDate: string;
      createEmpNo: string;
    }>;
  }>;
}

/**
 * 리비전 트리를 구조화된 형태로 파싱
 * 
 * @param tree GetRevTreeCompleteList 응답
 * @returns 파싱된 리비전 트리
 */
export async function parseRevisionTree(
  tree: RevTreeNodeApiResponse[]
): Promise<ParsedRevisionTree> {
  const root = tree.find((n) => n.SearchType === "Root");
  if (!root) {
    throw new Error("Root 노드를 찾을 수 없습니다");
  }

  // Revision 목록
  const revisions = tree
    .filter((n) => n.SearchType === "Revision")
    .map((rev) => {
      // 해당 Revision의 Activity 목록
      const activities = tree
        .filter(
          (n) => n.SearchType === "Activity" && n.ParentName === rev.NodeName
        )
        .map((act) => ({
          actvNo: act.NodeName,
          inOut: act.InOut as "IN" | "OUT",
          statusCode: act.StatusCode,
          statusName: act.StatusName,
          transmittalNo: act.TransmittalNo,
          refActivityNo: act.RefActivityNo.trim(),
          createDate: act.CreateDate,
          createEmpNo: act.CreateEmpNo,
        }));

      return {
        revSeq: rev.NodeName, // "0", "1", "2", "3"
        revNo: rev.SearchName, // "01", "02", "03", "04"
        stage: rev.Stage,
        activities,
      };
    });

  return {
    docNo: root.NodeName,
    revisions,
  };
}

/**
 * 특정 문서의 모든 리비전과 파일 정보를 조회
 * 
 * GetRevTreeCompleteList로 트리 구조를 가져온 후,
 * 각 Activity의 NodeName(ACTV_NO)을 사용하여 GetActivityFileList를 호출합니다.
 * 
 * ⚠️ 주의: Activity가 많을 경우 API 호출이 많아질 수 있습니다.
 * 
 * @param projNo 프로젝트 번호
 * @param docNo 문서 번호
 * @returns 리비전 트리 + 각 Activity의 파일 목록
 * 
 * @example
 * ```typescript
 * const fullData = await fetchDocumentRevisionTreeWithFiles(
 *   "SN2190",
 *   "C168-SH-SBN08-XG-20118-01"
 * );
 * 
 * console.log(`문서: ${fullData.docNo}`);
 * fullData.revisions.forEach(rev => {
 *   console.log(`\nRevision ${rev.revNo} (${rev.stage})`);
 *   rev.activities.forEach(act => {
 *     console.log(`  Activity: ${act.actvNo} (${act.inOut})`);
 *     console.log(`    파일 ${act.files.length}개`);
 *   });
 * });
 * ```
 */
export async function fetchDocumentRevisionTreeWithFiles(
  projNo: string,
  docNo: string
): Promise<{
  docNo: string;
  revisions: Array<{
    revSeq: string;
    revNo: string;
    stage: string;
    activities: Array<{
      actvNo: string;
      inOut: "IN" | "OUT";
      statusCode: string;
      statusName: string;
      transmittalNo: string;
      files: ActivityFileApiResponse[];
    }>;
  }>;
}> {
  console.log(`[SWP API] 문서 ${docNo} 전체 히스토리 조회 시작`);

  // 1단계: 리비전 트리 조회
  const tree = await fetchGetRevTreeCompleteList({
    proj_no: projNo,
    doc_no: docNo,
  });

  const parsed = await parseRevisionTree(tree);

  // 2단계: 각 Activity의 파일 목록 조회
  // Activity가 많을 경우 병렬 처리 고려 (단, API 부하 주의)
  const revisionsWithFiles = await Promise.all(
    parsed.revisions.map(async (rev) => {
      const activitiesWithFiles = await Promise.all(
        rev.activities.map(async (act) => {
          try {
            // GetActivityFileList는 rev_seq가 필요할 수 있음
            const files = await fetchGetActivityFileList({
              proj_no: projNo,
              doc_no: docNo,
              rev_seq: rev.revSeq,
            });

            // Activity 번호로 필터링
            const activityFiles = files.filter(
              (f) => f.ACTV_NO === act.actvNo
            );

            return {
              ...act,
              files: activityFiles,
            };
          } catch (error) {
            console.error(
              `Activity ${act.actvNo} 파일 조회 실패:`,
              error
            );
            return {
              ...act,
              files: [],
            };
          }
        })
      );

      return {
        ...rev,
        activities: activitiesWithFiles,
      };
    })
  );

  console.log(`[SWP API] 조회 완료: ${revisionsWithFiles.length}개 리비전`);

  return {
    docNo: parsed.docNo,
    revisions: revisionsWithFiles,
  };
}

/**
 * 특정 Revision의 Activity 목록만 조회 (경량 버전)
 * 
 * @param projNo 프로젝트 번호
 * @param docNo 문서 번호
 * @param revNo Revision 번호 (예: "04")
 * @returns 해당 Revision의 Activity 목록
 */
export async function fetchRevisionActivities(
  projNo: string,
  docNo: string,
  revNo: string
): Promise<ParsedRevisionTree["revisions"][0] | null> {
  const tree = await fetchGetRevTreeCompleteList({
    proj_no: projNo,
    doc_no: docNo,
  });

  const parsed = await parseRevisionTree(tree);

  return parsed.revisions.find((r) => r.revNo === revNo) || null;
}