summaryrefslogtreecommitdiff
path: root/lib/forms-plant/services.ts
blob: 99e7c35b59b39846cd14022ff5ae30cd5ce6e2fc (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
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
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
// lib/forms/services.ts
"use server";

import { headers } from "next/headers";
import path from "path";
import fs from "fs/promises";
import { v4 as uuidv4 } from "uuid";
import db from "@/db/db";
import {
  formEntries,
  formMetas,
  forms,
  tagClassAttributes,
  tagClasses,
  tags,
  tagSubfieldOptions,
  tagSubfields,
  tagTypeClassFormMappings,
  tagTypes,
  vendorDataReportTemps,
  VendorDataReportTemps,
} from "@/db/schema/vendorData";
import { eq, and, desc, sql, DrizzleError, inArray, or, type SQL, type InferSelectModel } from "drizzle-orm";
import { unstable_cache } from "next/cache";
import { revalidateTag } from "next/cache";
import { getErrorMessage } from "../handle-error";
import { DataTableColumnJSON } from "@/components/form-data/form-data-table-columns";
import { contractItems, contracts, items, projects } from "@/db/schema";
import { getSEDPToken } from "../sedp/sedp-token";
import { decryptWithServerAction } from "@/components/drm/drmUtils";
import { deleteFile, saveFile } from "@/lib/file-stroage";


export type FormInfo = InferSelectModel<typeof forms>;

export async function getFormsByContractItemId(
  contractItemId: number | null,
  mode: "ENG" | "IM" | "ALL" = "ALL"
): Promise<{ forms: FormInfo[] }> {
  // 유효성 검사
  if (!contractItemId || contractItemId <= 0) {
    console.warn(`Invalid contractItemId: ${contractItemId}`);
    return { forms: [] };
  }

  // 고유 캐시 키 (모드 포함)
  const cacheKey = `forms-${contractItemId}-${mode}`;

  try {
    // return unstable_cache(
    //   async () => {
    //     console.log(
    //       `[Forms Service] Fetching forms for contractItemId: ${contractItemId}, mode: ${mode}`
    //     );

    try {
      // 쿼리 생성
      let query = db.select().from(forms).where(eq(forms.contractItemId, contractItemId));

      // 모드에 따른 추가 필터
      if (mode === "ENG") {
        query = db.select().from(forms).where(
          and(
            eq(forms.contractItemId, contractItemId),
            eq(forms.eng, true)
          )
        );
      } else if (mode === "IM") {
        query = db.select().from(forms).where(
          and(
            eq(forms.contractItemId, contractItemId),
            eq(forms.im, true)
          )
        );
      }

      // 쿼리 실행
      const formRecords = await query;

      console.log(
        `[Forms Service] Found ${formRecords.length} forms for contractItemId: ${contractItemId}, mode: ${mode}`
      );

      return { forms: formRecords };
    } catch (error) {
      getErrorMessage(
        `Database error for contractItemId ${contractItemId}, mode: ${mode}: ${error}`
      );
      throw error; // 캐시 함수에서 에러를 던져 캐싱이 발생하지 않도록 함
    }
    //   },
    //   [cacheKey],
    //   {
    //     // 캐시 시간 단축
    //     revalidate: 60, // 1분으로 줄임
    //     tags: [cacheKey],
    //   }
    // )();
  } catch (error) {
    getErrorMessage(
      `Cache operation failed for contractItemId ${contractItemId}, mode: ${mode}: ${error}`
    );

    // 캐시 문제 시 직접 쿼리 시도
    try {
      console.log(
        `[Forms Service] Fallback: Direct query for contractItemId: ${contractItemId}, mode: ${mode}`
      );

      // 쿼리 생성
      let query = db.select().from(forms).where(eq(forms.contractItemId, contractItemId));

      // 모드에 따른 추가 필터
      if (mode === "ENG") {
        query = db.select().from(forms).where(
          and(
            eq(forms.contractItemId, contractItemId),
            eq(forms.eng, true)
          )
        );
      } else if (mode === "IM") {
        query = db.select().from(forms).where(
          and(
            eq(forms.contractItemId, contractItemId),
            eq(forms.im, true)
          )
        );
      }

      // 쿼리 실행
      const formRecords = await query;

      return { forms: formRecords };
    } catch (dbError) {
      getErrorMessage(
        `Fallback query failed for contractItemId ${contractItemId}, mode: ${mode}: ${dbError}`
      );
      return { forms: [] };
    }
  }
}

/**
 * 폼 캐시를 갱신하는 서버 액션
 */
export async function revalidateForms(contractItemId: number) {
  if (!contractItemId) return;

  const cacheKey = `forms-${contractItemId}`;
  console.log(`[Forms Service] Invalidating cache for ${cacheKey}`);

  try {
    revalidateTag(cacheKey);
    console.log(`[Forms Service] Cache invalidated for ${cacheKey}`);
  } catch (error) {
    getErrorMessage(`Failed to invalidate cache for ${cacheKey}: ${error}`);
  }
}

export interface EditableFieldsInfo {
  tagNo: string;
  editableFields: string[]; // 편집 가능한 필드 키 목록
}

// TAG별 편집 가능 필드 조회 함수
export async function getEditableFieldsByTag(
  contractItemId: number,
  projectId: number
): Promise<Map<string, string[]>> {
  try {
    // 1. 해당 contractItemId의 모든 태그 조회
    const tagList = await db
      .select({
        tagNo: tags.tagNo,
        tagClass: tags.class
      })
      .from(tags)
      .where(eq(tags.contractItemId, contractItemId));

    const editableFieldsMap = new Map<string, string[]>();

    // 2. 각 태그별로 편집 가능 필드 계산
    for (const tag of tagList) {
      try {
        // 2-1. tagClasses에서 해당 class(label)와 projectId로 tagClass 찾기
        const tagClassResult = await db
          .select({ id: tagClasses.id })
          .from(tagClasses)
          .where(
            and(
              eq(tagClasses.label, tag.tagClass),
              eq(tagClasses.projectId, projectId)
            )
          )
          .limit(1);

        if (tagClassResult.length === 0) {
          console.warn(`No tagClass found for class: ${tag.tagClass}, projectId: ${projectId}`);
          editableFieldsMap.set(tag.tagNo, []); // 편집 불가능
          continue;
        }

        // 2-2. tagClassAttributes에서 편집 가능한 필드 목록 조회
        const editableAttributes = await db
          .select({ attId: tagClassAttributes.attId })
          .from(tagClassAttributes)
          .where(eq(tagClassAttributes.tagClassId, tagClassResult[0].id))
          .orderBy(tagClassAttributes.seq);

        // 2-3. attId 목록 저장
        const editableFields = editableAttributes.map(attr => attr.attId);
        editableFieldsMap.set(tag.tagNo, editableFields);

      } catch (error) {
        console.error(`Error processing tag ${tag.tagNo}:`, error);
        editableFieldsMap.set(tag.tagNo, []); // 에러 시 편집 불가능
      }
    }

    return editableFieldsMap;
  } catch (error) {
    console.error('Error getting editable fields by tag:', error);
    return new Map();
  }
}
/**
 * "가장 최신 1개 row"를 가져오고,
 * data가 배열이면 그 배열을 반환,
 * 그리고 이 로직 전체를 unstable_cache로 감싸 캐싱.
 */
export async function getFormData(formCode: string, contractItemId: number) {
  try {

    // 기존 로직으로 projectId, columns, data 가져오기
    const contractItemResult = await db
      .select({
        projectId: projects.id
      })
      .from(contractItems)
      .innerJoin(contracts, eq(contractItems.contractId, contracts.id))
      .innerJoin(projects, eq(contracts.projectId, projects.id))
      .where(eq(contractItems.id, contractItemId))
      .limit(1);

    if (contractItemResult.length === 0) {
      console.warn(`[getFormData] No contract item found with ID: ${contractItemId}`);
      return { columns: null, data: [], editableFieldsMap: new Map() };
    }

    const projectId = contractItemResult[0].projectId;

    const metaRows = await db
      .select()
      .from(formMetas)
      .where(
        and(
          eq(formMetas.formCode, formCode),
          eq(formMetas.projectId, projectId)
        )
      )
      .orderBy(desc(formMetas.updatedAt))
      .limit(1);

    const meta = metaRows[0] ?? null;
    if (!meta) {
      console.warn(`[getFormData] No form meta found for formCode: ${formCode} and projectId: ${projectId}`);
      return { columns: null, data: [], editableFieldsMap: new Map() };
    }

    const entryRows = await db
      .select()
      .from(formEntries)
      .where(
        and(
          eq(formEntries.formCode, formCode),
          eq(formEntries.contractItemId, contractItemId)
        )
      )
      .orderBy(desc(formEntries.updatedAt))
      .limit(1);

    const entry = entryRows[0] ?? null;

    let columns = meta.columns as DataTableColumnJSON[];
    const excludeKeys = ['BF_TAG_NO', 'TAG_TYPE_ID', 'PIC_NO'];
    columns = columns.filter(col => !excludeKeys.includes(col.key));



    columns.forEach((col) => {
      if (!col.displayLabel) {
        if (col.uom) {
          col.displayLabel = `${col.label} (${col.uom})`;
        } else {
          col.displayLabel = col.label;
        }
      }
    });

    columns.push({
      key: "status",
      label: "status",
      displayLabel: "Status",
      type: "STRING"
    })

    let data: Array<Record<string, any>> = [];
    if (entry) {
      if (Array.isArray(entry.data)) {
        data = entry.data;

        data.sort((a, b) => {
          const statusA = a.status || '';
          const statusB = b.status || '';
          return statusB.localeCompare(statusA)
        })

      } else {
        console.warn("formEntries data was not an array. Using empty array.");
      }
    }

    // *** 새로 추가: 편집 가능 필드 정보 계산 ***
    const editableFieldsMap = await getEditableFieldsByTag(contractItemId, projectId);

    return { columns, data, editableFieldsMap };


  } catch (cacheError) {
    console.error(`[getFormData] Cache operation failed:`, cacheError);

    // Fallback logic (기존과 동일하게 editableFieldsMap 추가)
    try {
      console.log(`[getFormData] Fallback DB query for (${formCode}, ${contractItemId})`);

      const contractItemResult = await db
        .select({
          projectId: projects.id
        })
        .from(contractItems)
        .innerJoin(contracts, eq(contractItems.contractId, contracts.id))
        .innerJoin(projects, eq(contracts.projectId, projects.id))
        .where(eq(contractItems.id, contractItemId))
        .limit(1);

      if (contractItemResult.length === 0) {
        console.warn(`[getFormData] Fallback: No contract item found with ID: ${contractItemId}`);
        return { columns: null, data: [], editableFieldsMap: new Map() };
      }

      const projectId = contractItemResult[0].projectId;

      const metaRows = await db
        .select()
        .from(formMetas)
        .where(
          and(
            eq(formMetas.formCode, formCode),
            eq(formMetas.projectId, projectId)
          )
        )
        .orderBy(desc(formMetas.updatedAt))
        .limit(1);

      const meta = metaRows[0] ?? null;
      if (!meta) {
        console.warn(`[getFormData] Fallback: No form meta found for formCode: ${formCode} and projectId: ${projectId}`);
        return { columns: null, data: [], editableFieldsMap: new Map() };
      }

      const entryRows = await db
        .select()
        .from(formEntries)
        .where(
          and(
            eq(formEntries.formCode, formCode),
            eq(formEntries.contractItemId, contractItemId)
          )
        )
        .orderBy(desc(formEntries.updatedAt))
        .limit(1);

      const entry = entryRows[0] ?? null;

      let columns = meta.columns as DataTableColumnJSON[];
      const excludeKeys = ['BF_TAG_NO', 'TAG_TYPE_ID', 'PIC_NO'];
      columns = columns.filter(col => !excludeKeys.includes(col.key));

      columns.forEach((col) => {
        if (!col.displayLabel) {
          if (col.uom) {
            col.displayLabel = `${col.label} (${col.uom})`;
          } else {
            col.displayLabel = col.label;
          }
        }
      });

      let data: Array<Record<string, any>> = [];
      if (entry) {
        if (Array.isArray(entry.data)) {
          data = entry.data;
        } else {
          console.warn("formEntries data was not an array. Using empty array (fallback).");
        }
      }

      // Fallback에서도 편집 가능 필드 정보 계산
      const editableFieldsMap = await getEditableFieldsByTag(contractItemId, projectId);

      return { columns, data, projectId, editableFieldsMap };
    } catch (dbError) {
      console.error(`[getFormData] Fallback DB query failed:`, dbError);
      return { columns: null, data: [], editableFieldsMap: new Map() };
    }
  }
}
/**1
 * contractId와 formCode(itemCode)를 사용하여 contractItemId를 찾는 서버 액션
 * 
 * @param contractId - 계약 ID
 * @param formCode - 폼 코드 (itemCode와 동일)
 * @returns 찾은 contractItemId 또는 null
 */
export async function findContractItemId(contractId: number, formCode: string): Promise<number | null> {
  try {
    console.log(`[findContractItemId] 계약 ID ${contractId}와 formCode ${formCode}에 대한 contractItem 조회 시작`);

    // 1. forms 테이블에서 formCode에 해당하는 모든 레코드 조회
    const formsResult = await db
      .select({
        contractItemId: forms.contractItemId
      })
      .from(forms)
      .where(eq(forms.formCode, formCode));

    if (formsResult.length === 0) {
      console.warn(`[findContractItemId] formCode ${formCode}에 해당하는 form을 찾을 수 없습니다.`);
      return null;
    }

    // 모든 contractItemId 추출
    const contractItemIds = formsResult.map(form => form.contractItemId);
    console.log(`[findContractItemId] formCode ${formCode}에 해당하는 ${contractItemIds.length}개의 contractItemId 발견`);

    // 2. contractItems 테이블에서 추출한 contractItemId 중에서 
    // contractId가 일치하는 항목 찾기
    const contractItemResult = await db
      .select({
        id: contractItems.id
      })
      .from(contractItems)
      .where(
        and(
          inArray(contractItems.id, contractItemIds),
          eq(contractItems.contractId, contractId)
        )
      )
      .limit(1);

    if (contractItemResult.length === 0) {
      console.warn(`[findContractItemId] 계약 ID ${contractId}와 일치하는 contractItemId를 찾을 수 없습니다.`);
      return null;
    }

    const contractItemId = contractItemResult[0].id;
    console.log(`[findContractItemId] 계약 아이템 ID ${contractItemId} 발견`);

    return contractItemId;
  } catch (error) {
    console.error(`[findContractItemId] contractItem 조회 중 오류 발생:`, error);
    return null;
  }
}

export async function getPackageCodeById(contractItemId: number): Promise<string | null> {
  try {

    // 1. forms 테이블에서 formCode에 해당하는 모든 레코드 조회
    const contractItemsResult = await db
      .select({
        itemId: contractItems.itemId
      })
      .from(contractItems)
      .where(eq(contractItems.id, contractItemId))
      .limit(1)
      ;

    if (contractItemsResult.length === 0) {
      console.warn(`[contractItemId]에 해당하는 item을 찾을 수 없습니다.`);
      return null;
    }

    const itemId = contractItemsResult[0].itemId

    const packageCodeResult = await db
      .select({
        packageCode: items.packageCode
      })
      .from(items)
      .where(eq(items.id, itemId))
      .limit(1);

    if (packageCodeResult.length === 0) {
      console.warn(`${itemId}와 일치하는 패키지 코드를 찾을 수 없습니다.`);
      return null;
    }

    const packageCode = packageCodeResult[0].packageCode;

    return packageCode;
  } catch (error) {
    console.error(`패키지 코드 조회 중 오류 발생:`, error);
    return null;
  }
}


export async function syncMissingTags(
  contractItemId: number,
  formCode: string
) {
  // (1) Ensure there's a row in `forms` matching (contractItemId, formCode).
  const [formRow] = await db
    .select()
    .from(forms)
    .where(
      and(
        eq(forms.contractItemId, contractItemId),
        eq(forms.formCode, formCode)
      )
    )
    .limit(1);

  if (!formRow) {
    throw new Error(
      `Form not found for contractItemId=${contractItemId}, formCode=${formCode}`
    );
  }

  // (2) Get all mappings from `tagTypeClassFormMappings` for this formCode.
  const formMappings = await db
    .select()
    .from(tagTypeClassFormMappings)
    .where(eq(tagTypeClassFormMappings.formCode, formCode));

  // If no mappings are found, there's nothing to sync.
  if (formMappings.length === 0) {
    console.log(`No mappings found for formCode=${formCode}`);
    return { createdCount: 0, updatedCount: 0, deletedCount: 0 };
  }

  // Build a dynamic OR clause to match (tagType, class) pairs from the mappings.
  const orConditions = formMappings.map((m) =>
    and(eq(tags.tagType, m.tagTypeLabel), eq(tags.class, m.classLabel))
  );

  // (3) Fetch all matching `tags` for the contractItemId + any of the (tagType, class) pairs.
  const tagRows = await db
    .select()
    .from(tags)
    .where(and(eq(tags.contractItemId, contractItemId), or(...orConditions)));

  // (4) Fetch (or create) a single `formEntries` row for (contractItemId, formCode).
  let [entry] = await db
    .select()
    .from(formEntries)
    .where(
      and(
        eq(formEntries.contractItemId, contractItemId),
        eq(formEntries.formCode, formCode)
      )
    )
    .limit(1);

  if (!entry) {
    const [inserted] = await db
      .insert(formEntries)
      .values({
        contractItemId,
        formCode,
        data: [], // Initialize with empty array
      })
      .returning();
    entry = inserted;
  }

  // entry.data는 [{ TAG_NO: string, TAG_DESC?: string }, ...] 형태라고 가정
  const existingData = entry.data as Array<{
    TAG_NO: string;
    TAG_DESC?: string;
  }>;

  // Create a Set of valid tagNumbers from tagRows for efficient lookup
  const validTagNumbers = new Set(tagRows.map((tag) => tag.tagNo));

  // Copy existing data to work with
  let updatedData: Array<{
    TAG_NO: string;
    TAG_DESC?: string;
  }> = [];

  let createdCount = 0;
  let updatedCount = 0;
  let deletedCount = 0;

  // First, filter out items that should be deleted (not in validTagNumbers)
  for (const item of existingData) {
    if (validTagNumbers.has(item.TAG_NO)) {
      updatedData.push(item);
    } else {
      deletedCount++;
    }
  }

  // (5) For each tagRow, if it's missing in updatedData, push it in.
  //     이미 있는 경우에도 description이 달라지면 업데이트할 수 있음.
  for (const tagRow of tagRows) {
    const { tagNo, description } = tagRow;

    // 5-1. 기존 데이터에서 TAG_NO 매칭
    const existingIndex = updatedData.findIndex(
      (item) => item.TAG_NO === tagNo
    );

    // 5-2. 없다면 새로 추가
    if (existingIndex === -1) {
      updatedData.push({
        TAG_NO: tagNo,
        TAG_DESC: description ?? "",
      });
      createdCount++;
    } else {
      // 5-3. 이미 있으면, description이 다를 때만 업데이트(선택 사항)
      const existingItem = updatedData[existingIndex];
      if (existingItem.TAG_DESC !== description) {
        updatedData[existingIndex] = {
          ...existingItem,
          TAG_DESC: description ?? "",
        };
        updatedCount++;
      }
    }
  }

  // (6) 실제로 추가되거나 수정되거나 삭제된 게 있다면 DB에 반영
  if (createdCount > 0 || updatedCount > 0 || deletedCount > 0) {
    await db
      .update(formEntries)
      .set({ data: updatedData })
      .where(eq(formEntries.id, entry.id));
  }

  // 캐시 무효화 등 후처리
  revalidateTag(`form-data-${formCode}-${contractItemId}`);

  return { createdCount, updatedCount, deletedCount };
}

/**
 * updateFormDataInDB:
 * (formCode, contractItemId)에 해당하는 "단 하나의" formEntries row를 가져와,
 * data: [{ TAG_NO, ...}, ...] 배열에서 TAG_NO 매칭되는 항목을 업데이트
 * 업데이트 후, revalidateTag()로 캐시 무효화.
 */
export interface UpdateResponse {
  success: boolean;
  message: string;
  data?: {
    updatedCount?: number;
    failedCount?: number;
    updatedTags?: string[];
    notFoundTags?: string[];
    updateTimestamp?: string;
    error?: any;
    invalidRows?: any[];
    TAG_NO?: string;
    updatedFields?: string[];
  };
}

export async function updateFormDataInDB(
  formCode: string,
  contractItemId: number,
  newData: Record<string, any>
): Promise<UpdateResponse> {
  try {
    // 1) tagNumber로 식별
    const TAG_NO = newData.TAG_NO;
    if (!TAG_NO) {
      return {
        success: false,
        message: "tagNumber는 필수 항목입니다.",
      };
    }

    // 2) row 찾기 (단 하나)
    const entries = await db
      .select()
      .from(formEntries)
      .where(
        and(
          eq(formEntries.formCode, formCode),
          eq(formEntries.contractItemId, contractItemId)
        )
      )
      .limit(1);

    if (!entries || entries.length === 0) {
      return {
        success: false,
        message: `폼 데이터를 찾을 수 없습니다. (formCode=${formCode}, contractItemId=${contractItemId})`,
      };
    }

    const entry = entries[0];

    // 3) data가 배열인지 확인
    if (!entry.data) {
      return {
        success: false,
        message: "폼 데이터가 없습니다.",
      };
    }

    const dataArray = entry.data as Array<Record<string, any>>;
    if (!Array.isArray(dataArray)) {
      return {
        success: false,
        message: "폼 데이터가 올바른 형식이 아닙니다. 배열 형식이어야 합니다.",
      };
    }

    // 4) TAG_NO = newData.TAG_NO 항목 찾기
    const idx = dataArray.findIndex((item) => item.TAG_NO === TAG_NO);
    if (idx < 0) {
      return {
        success: false,
        message: `태그 번호 "${TAG_NO}"를 가진 항목을 찾을 수 없습니다.`,
      };
    }

    // 5) 병합 (status 필드 추가)
    const oldItem = dataArray[idx];
    const updatedItem = {
      ...oldItem,
      ...newData,
      TAG_NO: oldItem.TAG_NO, // TAG_NO 변경 불가 시 유지
      status: "Updated" // Excel에서 가져온 데이터임을 표시
    };

    const updatedArray = [...dataArray];
    updatedArray[idx] = updatedItem;

    // 6) DB UPDATE
    try {
      await db
        .update(formEntries)
        .set({
          data: updatedArray,
          updatedAt: new Date(), // 업데이트 시간도 갱신
        })
        .where(eq(formEntries.id, entry.id));
    } catch (dbError) {
      console.error("Database update error:", dbError);

      if (dbError instanceof DrizzleError) {
        return {
          success: false,
          message: `데이터베이스 업데이트 오류: ${dbError.message}`,
        };
      }

      return {
        success: false,
        message: "데이터베이스 업데이트 중 오류가 발생했습니다.",
      };
    }

    // 7) Cache 무효화
    try {
      // 캐시 태그를 form-data-${formCode}-${contractItemId} 형태로 가정
      const cacheTag = `form-data-${formCode}-${contractItemId}`;
      console.log(cacheTag, "update")
      revalidateTag(cacheTag);
    } catch (cacheError) {
      console.warn("Cache revalidation warning:", cacheError);
      // 캐시 무효화는 실패해도 업데이트 자체는 성공했으므로 경고만 로그로 남김
    }

    return {
      success: true,
      message: "데이터가 성공적으로 업데이트되었습니다.",
      data: {
        TAG_NO,
        updatedFields: Object.keys(newData).filter(
          (key) => key !== "TAG_NO"
        ),
      },
    };
  } catch (error) {
    // 예상치 못한 오류 처리
    console.error("Unexpected error in updateFormDataInDB:", error);
    return {
      success: false,
      message:
        error instanceof Error
          ? `예상치 못한 오류가 발생했습니다: ${error.message}`
          : "알 수 없는 오류가 발생했습니다.",
    };
  }
}

export async function updateFormDataBatchInDB(
  formCode: string,
  contractItemId: number,
  newDataArray: Record<string, any>[]
): Promise<UpdateResponse> {
  try {
    // 입력 유효성 검사
    if (!newDataArray || newDataArray.length === 0) {
      return {
        success: false,
        message: "업데이트할 데이터가 없습니다.",
      };
    }

    // TAG_NO 유효성 검사
    const invalidRows = newDataArray.filter(row => !row.TAG_NO);
    if (invalidRows.length > 0) {
      return {
        success: false,
        message: `${invalidRows.length}개 행에 TAG_NO가 없습니다.`,
        data: { invalidRows }
      };
    }

    // 1) DB에서 현재 데이터 가져오기
    const entries = await db
      .select()
      .from(formEntries)
      .where(
        and(
          eq(formEntries.formCode, formCode),
          eq(formEntries.contractItemId, contractItemId)
        )
      )
      .limit(1);

    if (!entries || entries.length === 0) {
      return {
        success: false,
        message: `폼 데이터를 찾을 수 없습니다. (formCode=${formCode}, contractItemId=${contractItemId})`,
      };
    }

    const entry = entries[0];

    // 데이터 형식 검증
    if (!entry.data) {
      return {
        success: false,
        message: "폼 데이터가 없습니다.",
      };
    }

    const dataArray = entry.data as Array<Record<string, any>>;
    if (!Array.isArray(dataArray)) {
      return {
        success: false,
        message: "폼 데이터가 올바른 형식이 아닙니다. 배열 형식이어야 합니다.",
      };
    }

    // 2) 모든 변경사항을 한번에 적용
    const updatedArray = [...dataArray];
    const updatedTags: string[] = [];
    const notFoundTags: string[] = [];
    const updateTimestamp = new Date().toISOString();
    
    // 각 import row에 대해 업데이트 수행
    for (const newData of newDataArray) {
      const TAG_NO = newData.TAG_NO;
      const idx = updatedArray.findIndex(item => item.TAG_NO === TAG_NO);
      
      if (idx >= 0) {
        // 기존 데이터와 병합
        const oldItem = updatedArray[idx];
        updatedArray[idx] = {
          ...oldItem,
          ...newData,
          TAG_NO: oldItem.TAG_NO, // TAG_NO는 변경 불가
          TAG_DESC: oldItem.TAG_DESC, // TAG_DESC도 보존
          status: "Updated", // Excel import 표시
          lastUpdated: updateTimestamp // 업데이트 시각 추가
        };
        updatedTags.push(TAG_NO);
      } else {
        // TAG를 찾을 수 없는 경우
        notFoundTags.push(TAG_NO);
      }
    }

    // 하나도 업데이트할 항목이 없는 경우
    if (updatedTags.length === 0) {
      return {
        success: false,
        message: `업데이트할 수 있는 TAG를 찾을 수 없습니다. 모든 ${notFoundTags.length}개 TAG가 데이터베이스에 없습니다.`,
        data: {
          updatedCount: 0,
          failedCount: notFoundTags.length,
          notFoundTags
        }
      };
    }

    // 3) DB에 한 번만 저장
    try {
      await db
        .update(formEntries)
        .set({
          data: updatedArray,
          updatedAt: new Date(),
        })
        .where(eq(formEntries.id, entry.id));
        
    } catch (dbError) {
      console.error("Database update error:", dbError);

      if (dbError instanceof DrizzleError) {
        return {
          success: false,
          message: `데이터베이스 업데이트 오류: ${dbError.message}`,
          data: {
            updatedCount: 0,
            failedCount: newDataArray.length,
            error: dbError
          }
        };
      }

      return {
        success: false,
        message: "데이터베이스 업데이트 중 오류가 발생했습니다.",
        data: {
          updatedCount: 0,
          failedCount: newDataArray.length
        }
      };
    }

    // 4) 캐시 무효화
    try {
      const cacheTag = `form-data-${formCode}-${contractItemId}`;
      console.log(`Cache invalidated: ${cacheTag}`);
      revalidateTag(cacheTag);
    } catch (cacheError) {
      // 캐시 무효화 실패는 경고만
      console.warn("Cache revalidation warning:", cacheError);
    }

    // 5) 성공 응답
    const message = notFoundTags.length > 0
      ? `${updatedTags.length}개 항목이 업데이트되었습니다. (${notFoundTags.length}개 TAG는 찾을 수 없음)`
      : `${updatedTags.length}개 항목이 성공적으로 업데이트되었습니다.`;

    return {
      success: true,
      message: message,
      data: {
        updatedCount: updatedTags.length,
        updatedTags,
        notFoundTags: notFoundTags.length > 0 ? notFoundTags : undefined,
        failedCount: notFoundTags.length,
        updateTimestamp
      },
    };
    
  } catch (error) {
    // 예상치 못한 오류 처리
    console.error("Unexpected error in updateFormDataBatchInDB:", error);
    
    return {
      success: false,
      message: error instanceof Error
        ? `예상치 못한 오류가 발생했습니다: ${error.message}`
        : "알 수 없는 오류가 발생했습니다.",
      data: {
        updatedCount: 0,
        failedCount: newDataArray.length,
        error: error
      }
    };
  }
}

// FormColumn Type (동일)
export interface FormColumn {
  key: string;
  type: string;
  label: string;
  options?: string[];
}

interface MetadataResult {
  formName: string;
  formCode: string;
  columns: FormColumn[];
}

/**
 * 서버 액션:
 * 주어진 formCode에 해당하는 form_metas 레코드 1개를 찾아서
 * { formName, formCode, columns } 형태로 반환.
 * 없으면 null.
 */
export async function fetchFormMetadata(
  formCode: string,
  projectId: number
): Promise<MetadataResult | null> {
  try {
    // 기존 방식: select().from().where()
    const rows = await db
      .select()
      .from(formMetas)
      .where(and(eq(formMetas.formCode, formCode), eq(formMetas.projectId, projectId)))
      .limit(1);

    // rows는 배열
    const metaData = rows[0];
    if (!metaData) return null;

    return {
      formCode: metaData.formCode,
      formName: metaData.formName,
      columns: metaData.columns as FormColumn[],
    };
  } catch (err) {
    console.error("Error in fetchFormMetadata:", err);
    return null;
  }
}

type GetReportFileList = (
  packageId: string,
  formCode: string
) => Promise<{
  formId: number;
}>;

export const getFormId: GetReportFileList = async (packageId, formCode) => {
  const result: { formId: number } = {
    formId: 0,
  };
  try {
    const [targetForm] = await db
      .select()
      .from(forms)
      .where(
        and(
          eq(forms.formCode, formCode),
          eq(forms.contractItemId, Number(packageId))
        )
      );

    if (!targetForm) {
      throw new Error("Not Found Target Form");
    }

    const { id: formId } = targetForm;

    result.formId = formId;
  } catch (err) {
  } finally {
    return result;
  }
};

type getReportTempList = (
  packageId: number,
  formId: number
) => Promise<VendorDataReportTemps[]>;

export const getReportTempList: getReportTempList = async (
  packageId,
  formId
) => {
  let result: VendorDataReportTemps[] = [];

  try {
    result = await db
      .select()
      .from(vendorDataReportTemps)
      .where(
        and(
          eq(vendorDataReportTemps.contractItemId, packageId),
          eq(vendorDataReportTemps.formId, formId)
        )
      );
  } catch (err) {
  } finally {
    return result;
  }
};

export async function uploadReportTemp(
  packageId: number,
  formId: number,
  formData: FormData
) {
  const file = formData.get("file") as File | null;
  const customFileName = formData.get("customFileName") as string;
  const uploaderType = (formData.get("uploaderType") as string) || "vendor";

  if (!["vendor", "client", "shi"].includes(uploaderType)) {
    throw new Error(
      `Invalid uploaderType: ${uploaderType}. Must be one of: vendor, client, shi`
    );
  }
  if (file && file.size > 0) {

    const saveResult = await saveFile({ file, directory: "vendorFormData", originalName: customFileName });
    if (!saveResult.success) {
      return { success: false, error: saveResult.error };
    }

    return db.transaction(async (tx) => {
      // 파일 정보를 테이블에 저장
      await tx
        .insert(vendorDataReportTemps)
        .values({
          contractItemId: packageId,
          formId: formId,
          fileName: customFileName,
          filePath: saveResult.publicPath!,
        })
        .returning();
    });
  }
}

export const getOrigin = async (): Promise<string> => {
  const headersList = await headers();
  const host = headersList.get("host");
  const proto = headersList.get("x-forwarded-proto") || "http"; // 기본값은 http
  const origin = `${proto}://${host}`;

  return origin;
};


type deleteReportTempFile = (id: number) => Promise<{
  result: boolean;
  error?: any;
}>;

export const deleteReportTempFile: deleteReportTempFile = async (id) => {
  try {
    return db.transaction(async (tx) => {
      const [targetTempFile] = await tx
        .select()
        .from(vendorDataReportTemps)
        .where(eq(vendorDataReportTemps.id, id));

      if (!targetTempFile) {
        throw new Error("해당 Template File을 찾을 수 없습니다.");
      }

      await tx
        .delete(vendorDataReportTemps)
        .where(eq(vendorDataReportTemps.id, id));

      const { filePath } = targetTempFile;

      await deleteFile(filePath);

      return { result: true };
    });
  } catch (err) {
    return { result: false, error: (err as Error).message };
  }
};


/**
 * Get tag type mappings specific to a form
 * @param formCode The form code to filter mappings
 * @param projectId The project ID
 * @returns Array of tag type-class mappings for the form
 */
export async function getFormTagTypeMappings(formCode: string, projectId: number) {

  try {
    const mappings = await db.query.tagTypeClassFormMappings.findMany({
      where: and(
        eq(tagTypeClassFormMappings.formCode, formCode),
        eq(tagTypeClassFormMappings.projectId, projectId)
      )
    });

    return mappings;
  } catch (error) {
    console.error("Error fetching form tag type mappings:", error);
    throw new Error("Failed to load form tag type mappings");
  }
}

/**
 * Get tag type by its description
 * @param description The tag type description (used as tagTypeLabel in mappings)
 * @param projectId The project ID
 * @returns The tag type object
 */
export async function getTagTypeByDescription(description: string, projectId: number) {
  try {
    const tagType = await db.query.tagTypes.findFirst({
      where: and(
        eq(tagTypes.description, description),
        eq(tagTypes.projectId, projectId)
      )
    });

    return tagType;
  } catch (error) {
    console.error("Error fetching tag type by description:", error);
    throw new Error("Failed to load tag type");
  }
}

/**
 * Get subfields for a specific tag type
 * @param tagTypeCode The tag type code
 * @param projectId The project ID
 * @returns Object containing subfields with their options
 */
export async function getSubfieldsByTagTypeForForm(tagTypeCode: string, projectId: number) {
  try {
    const subfields = await db.query.tagSubfields.findMany({
      where: and(
        eq(tagSubfields.tagTypeCode, tagTypeCode),
        eq(tagSubfields.projectId, projectId)
      ),
      orderBy: tagSubfields.sortOrder
    });

    const subfieldsWithOptions = await Promise.all(
      subfields.map(async (subfield) => {
        const options = await db.query.tagSubfieldOptions.findMany({
          where: and(
            eq(tagSubfieldOptions.attributesId, subfield.attributesId),
            eq(tagSubfieldOptions.projectId, projectId)
          )
        });

        return {
          name: subfield.attributesId,
          label: subfield.attributesDescription,
          type: options.length > 0 ? "select" : "text",
          options: options.map(opt => ({ value: opt.code, label: opt.label })),
          expression: subfield.expression || undefined,
          delimiter: subfield.delimiter || undefined
        };
      })
    );

    return { subFields: subfieldsWithOptions };
  } catch (error) {
    console.error("Error fetching subfields for form:", error);
    throw new Error("Failed to load subfields");
  }
}

interface GenericData {
  [key: string]: any;
}

interface SEDPAttribute {
  NAME: string;
  VALUE: any;
  UOM: string;
  UOM_ID?: string;
  CLS_ID?:string;
}

interface SEDPDataItem {
  TAG_NO: string;
  TAG_DESC: string;
  CLS_ID: string;
  ATTRIBUTES: SEDPAttribute[];
  SCOPE: string;
  TOOLID: string;
  ITM_NO: string;
  OP_DELETE: boolean;
  MAIN_YN: boolean;
  LAST_REV_YN: boolean;
  CRTER_NO: string;
  CHGER_NO: string;
  TYPE: string;
  PROJ_NO: string;
  REV_NO: string;
  CRTE_DTM?: string;
  CHGE_DTM?: string;
  _id?: string;
}

async function transformDataToSEDPFormat(
  tableData: GenericData[],
  columnsJSON: DataTableColumnJSON[],
  formCode: string,
  objectCode: string,
  projectNo: string,
  contractItemId: number, // Add contractItemId parameter
  designerNo: string = "253213"
): Promise<SEDPDataItem[]> {
  // Create a map for quick column lookup
  const columnsMap = new Map<string, DataTableColumnJSON>();
  columnsJSON.forEach(col => {
    columnsMap.set(col.key, col);
  });

  // Current timestamp for CRTE_DTM and CHGE_DTM
  const currentTimestamp = new Date().toISOString();

  // Define the API base URL
  const SEDP_API_BASE_URL = process.env.SEDP_API_BASE_URL || 'http://sedpwebapi.ship.samsung.co.kr/api';

  // Get the token
  const apiKey = await getSEDPToken();

  // Cache for UOM factors to avoid duplicate API calls
  const uomFactorCache = new Map<string, number>();

  // Cache for packageCode to avoid duplicate DB queries for same tag
  const packageCodeCache = new Map<string, string>();

  // Cache for tagClass code to avoid duplicate DB queries for same tag
  const tagClassCodeCache = new Map<string, string>();

  // Transform each row
  const transformedItems = [];

  for (const row of tableData) {

    const cotractItem = await db.query.contractItems.findFirst({
      where: 
        eq(contractItems.id, contractItemId),
    });

    const item = await db.query.items.findFirst({
      where: 
        eq(items.id, cotractItem.itemId),
    });

    // Get packageCode for this specific tag
    let packageCode = item.packageCode; // fallback to formCode
    let tagClassCode = ""; // for CLS_ID

    if (row.TAG_NO && contractItemId) {
      // Check cache first
      const cacheKey = `${contractItemId}-${row.TAG_NO}`;

      if (packageCodeCache.has(cacheKey)) {
        packageCode = packageCodeCache.get(cacheKey)!;
      } else {
        try {
          // Query to get packageCode for this specific tag
          const tagResult = await db.query.tags.findFirst({
            where: and(
              eq(tags.contractItemId, contractItemId),
              eq(tags.tagNo, row.TAG_NO)
            )
          });

          if (tagResult) {
            // Get tagClass code if tagClassId exists
            if (tagResult.tagClassId) {
              // Check tagClass cache first
              if (tagClassCodeCache.has(cacheKey)) {
                tagClassCode = tagClassCodeCache.get(cacheKey)!;
              } else {
                const tagClassResult = await db.query.tagClasses.findFirst({
                  where: eq(tagClasses.id, tagResult.tagClassId)
                });

                if (tagClassResult) {
                  tagClassCode = tagClassResult.code;
                  console.log(`Found tagClass code for tag ${row.TAG_NO}: ${tagClassCode}`);
                } else {
                  console.warn(`No tagClass found for tagClassId: ${tagResult.tagClassId}`);
                }

                // Cache the tagClass code result
                tagClassCodeCache.set(cacheKey, tagClassCode);
              }
            }

            // Get the contract item
            const contractItemResult = await db.query.contractItems.findFirst({
              where: eq(contractItems.id, tagResult.contractItemId)
            });

            if (contractItemResult) {
              // Get the first item with this itemId
              const itemResult = await db.query.items.findFirst({
                where: eq(items.id, contractItemResult.itemId)
              });

              if (itemResult && itemResult.packageCode) {
                packageCode = itemResult.packageCode;
                console.log(`Found packageCode for tag ${row.TAG_NO}: ${packageCode}`);
              } else {
                console.warn(`No item found for contractItem.itemId: ${contractItemResult.itemId}, using fallback`);
              }
            } else {
              console.warn(`No contractItem found for tag ${row.TAG_NO}, using fallback`);
            }
          } else {
            console.warn(`No tag found for contractItemId: ${contractItemId}, tagNo: ${row.TAG_NO}, using fallback`);
          }

          // Cache the result (even if it's the fallback value)
          packageCodeCache.set(cacheKey, packageCode);
        } catch (error) {
          console.error(`Error fetching packageCode for tag ${row.TAG_NO}:`, error);
          // Use fallback value and cache it
          packageCodeCache.set(cacheKey, packageCode);
        }
      }

      // Get tagClass code if not already retrieved above
      if (!tagClassCode && tagClassCodeCache.has(cacheKey)) {
        tagClassCode = tagClassCodeCache.get(cacheKey)!;
      } else if (!tagClassCode) {
        try {
          const tagResult = await db.query.tags.findFirst({
            where: and(
              eq(tags.contractItemId, contractItemId),
              eq(tags.tagNo, row.TAG_NO)
            )
          });

          if (tagResult && tagResult.tagClassId) {
            const tagClassResult = await db.query.tagClasses.findFirst({
              where: eq(tagClasses.id, tagResult.tagClassId)
            });

            if (tagClassResult) {
              tagClassCode = tagClassResult.code;
              console.log(`Found tagClass code for tag ${row.TAG_NO}: ${tagClassCode}`);
            }
          }

          // Cache the tagClass code result
          tagClassCodeCache.set(cacheKey, tagClassCode);
        } catch (error) {
          console.error(`Error fetching tagClass code for tag ${row.TAG_NO}:`, error);
          // Cache empty string as fallback
          tagClassCodeCache.set(cacheKey, "");
        }
      }
    }

    // Create base SEDP item with required fields
    const sedpItem: SEDPDataItem = {
      TAG_NO: row.TAG_NO || "",
      TAG_DESC: row.TAG_DESC || "",
      ATTRIBUTES: [],
      // SCOPE: objectCode,
      SCOPE: packageCode,
      TOOLID: "eVCP", // Changed from VDCS
      ITM_NO: row.TAG_NO || "",
      OP_DELETE: false,
      MAIN_YN: true,
      LAST_REV_YN: true,
      CRTER_NO: designerNo,
      CHGER_NO: designerNo,
      TYPE: formCode, // Use packageCode instead of formCode
      CLS_ID: tagClassCode, // Add CLS_ID with tagClass code
      PROJ_NO: projectNo,
      REV_NO: "00",
      CRTE_DTM: currentTimestamp,
      CHGE_DTM: currentTimestamp,
      _id: ""
    };

    // Convert all other fields (except TAG_NO and TAG_DESC) to ATTRIBUTES
    for (const key in row) {
      if (key !== "TAG_NO" && key !== "TAG_DESC") {
        const column = columnsMap.get(key);
        let value = row[key];

        // Only process non-empty values
        if (value !== undefined && value !== null && value !== "") {
          // Check if we need to apply UOM conversion
          if (column?.uomId) {
            // First check cache to avoid duplicate API calls
            let factor = uomFactorCache.get(column.uomId);

            // If not in cache, make API call to get the factor
            if (factor === undefined) {
              try {
                const response = await fetch(
                  `${SEDP_API_BASE_URL}/UOM/GetByID`,
                  {
                    method: 'POST',
                    headers: {
                      'Content-Type': 'application/json',
                      'accept': '*/*',
                      'ApiKey': apiKey,
                      'ProjectNo': projectNo
                    },
                    body: JSON.stringify({
                      'ProjectNo': projectNo,
                      'UOMID': column.uomId,
                      'ContainDeleted': false
                    })
                  }
                );

                if (response.ok) {
                  const uomData = await response.json();
                  if (uomData && uomData.FACTOR !== undefined && uomData.FACTOR !== null) {
                    factor = Number(uomData.FACTOR);
                    // Store in cache for future use (type assertion to ensure it's a number)
                    uomFactorCache.set(column.uomId, factor);
                  }
                } else {
                  console.warn(`Failed to get UOM data for ${column.uomId}: ${response.statusText}`);
                }
              } catch (error) {
                console.error(`Error fetching UOM data for ${column.uomId}:`, error);
              }
            }

            // Apply the factor if we got one
            // if (factor !== undefined && typeof value === 'number') {
            //   value = value * factor;
            // }
          }

          const attribute: SEDPAttribute = {
            NAME: key,
            VALUE: String(value),  // 모든 값을 문자열로 변환
            UOM: column?.uom || "",
            CLS_ID: tagClassCode || "",
          };

          // Add UOM_ID if present in column definition
          if (column?.uomId) {
            attribute.UOM_ID = column.uomId;
          }

          sedpItem.ATTRIBUTES.push(attribute);
        }
      }
    }

    transformedItems.push(sedpItem);
  }

  return transformedItems;
}

// Server Action wrapper (async)
export async function transformFormDataToSEDP(
  tableData: GenericData[],
  columnsJSON: DataTableColumnJSON[],
  formCode: string,
  objectCode: string,
  projectNo: string,
  contractItemId: number, // Add contractItemId parameter
  designerNo: string = "253213"
): Promise<SEDPDataItem[]> {
  return transformDataToSEDPFormat(
    tableData,
    columnsJSON,
    formCode,
    objectCode,
    projectNo,
    contractItemId, // Pass contractItemId
    designerNo
  );
}
/**
 * Get project code by project ID
 */
export async function getProjectCodeById(projectId: number): Promise<string> {
  const projectRecord = await db
    .select({ code: projects.code })
    .from(projects)
    .where(eq(projects.id, projectId))
    .limit(1);

  if (!projectRecord || projectRecord.length === 0) {
    throw new Error(`Project not found with ID: ${projectId}`);
  }

  return projectRecord[0].code;
}

export async function getProjectById(projectId: number): Promise<{ code: string; type: string;  }> {
  const projectRecord = await db
    .select({ code: projects.code , type:projects.type})
    .from(projects)
    .where(eq(projects.id, projectId))
    .limit(1);

  if (!projectRecord || projectRecord.length === 0) {
    throw new Error(`Project not found with ID: ${projectId}`);
  }

  return projectRecord[0];
}


/**
 * Send data to SEDP
 */
export async function sendDataToSEDP(
  projectCode: string,
  sedpData: SEDPDataItem[]
): Promise<any> {
  try {
    // Get the token
    const apiKey = await getSEDPToken();

    // Define the API base URL
    const SEDP_API_BASE_URL = process.env.SEDP_API_BASE_URL || 'http://sedpwebapi.ship.samsung.co.kr/api';

    console.log("Sending data to SEDP:", JSON.stringify(sedpData, null, 2));

    // Make the API call
    const response = await fetch(
      `${SEDP_API_BASE_URL}/AdapterData/Overwrite`,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'accept': '*/*',
          'ApiKey': apiKey,
          'ProjectNo': projectCode
        },
        body: JSON.stringify(sedpData)
      }
    );

    if (!response.ok) {
      const errorText = await response.text();
      throw new Error(`SEDP API request failed: ${response.status} ${response.statusText} - ${errorText}`);
    }

    const data = await response.json();
    return data;
  } catch (error: any) {
    console.error('Error calling SEDP API:', error);
    throw new Error(`Failed to send data to SEDP API: ${error.message || 'Unknown error'}`);
  }
}

/**
 * Server action to send form data to SEDP
 */
export async function sendFormDataToSEDP(
  formCode: string,
  projectId: number,
  contractItemId: number, // contractItemId 파라미터 추가
  formData: GenericData[],
  columns: DataTableColumnJSON[]
): Promise<{ success: boolean; message: string; data?: any }> {
  try {
    // 1. Get project code
    const projectCode = await getProjectCodeById(projectId);

    // 2. Get class mapping
    const mappingsResult = await db.query.tagTypeClassFormMappings.findFirst({
      where: and(
        eq(tagTypeClassFormMappings.formCode, formCode),
        eq(tagTypeClassFormMappings.projectId, projectId)
      )
    });

    // Check if mappings is an array or a single object and handle accordingly
    const mappings = Array.isArray(mappingsResult) ? mappingsResult[0] : mappingsResult;

    // Default object code to fallback value if we can't find it
    let objectCode = ""; // Default fallback

    if (mappings && mappings.classLabel) {
      const objectCodeResult = await db.query.tagClasses.findFirst({
        where: and(
          eq(tagClasses.label, mappings.classLabel),
          eq(tagClasses.projectId, projectId)
        )
      });

      // Check if result is an array or a single object
      const objectCodeRecord = Array.isArray(objectCodeResult) ? objectCodeResult[0] : objectCodeResult;

      if (objectCodeRecord && objectCodeRecord.code) {
        objectCode = objectCodeRecord.code;
      } else {
        console.warn(`No tag class found for label ${mappings.classLabel} in project ${projectId}, using default`);
      }
    } else {
      console.warn(`No mapping found for formCode ${formCode} in project ${projectId}, using default object code`);
    }

    // 3. Transform data to SEDP format
    const sedpData = await transformFormDataToSEDP(
      formData,
      columns,
      formCode,
      objectCode,
      projectCode,
      contractItemId // Add contractItemId parameter
    );

    // 4. Send to SEDP API
    const result = await sendDataToSEDP(projectCode, sedpData);

    // 5. SEDP 전송 성공 후 formEntries에 status 업데이트
    try {
      // Get the current formEntries data
      const entries = await db
        .select()
        .from(formEntries)
        .where(
          and(
            eq(formEntries.formCode, formCode),
            eq(formEntries.contractItemId, contractItemId)
          )
        )
        .limit(1);

      if (entries && entries.length > 0) {
        const entry = entries[0];
        const dataArray = entry.data as Array<Record<string, any>>;

        if (Array.isArray(dataArray)) {
          // Extract TAG_NO list from formData
          const sentTagNumbers = new Set(
            formData
              .map(item => item.TAG_NO)
              .filter(tagNo => tagNo) // Remove null/undefined values
          );

          // Update status for sent tags
          const updatedDataArray = dataArray.map(item => {
            if (item.TAG_NO && sentTagNumbers.has(item.TAG_NO)) {
              return {
                ...item,
                status: "Sent to S-EDP" // SEDP로 전송된 데이터임을 표시
              };
            }
            return item;
          });

          // Update the database
          await db
            .update(formEntries)
            .set({
              data: updatedDataArray,
              updatedAt: new Date()
            })
            .where(eq(formEntries.id, entry.id));

          console.log(`Updated status for ${sentTagNumbers.size} tags to "Sent to S-EDP"`);
        }
      } else {
        console.warn(`No formEntries found for formCode: ${formCode}, contractItemId: ${contractItemId}`);
      }
    } catch (statusUpdateError) {
      // Status 업데이트 실패는 경고로만 처리 (SEDP 전송은 성공했으므로)
      console.warn("Failed to update status after SEDP send:", statusUpdateError);
    }

    return {
      success: true,
      message: "Data successfully sent to SEDP",
      data: result
    };
  } catch (error: any) {
    console.error("Error sending data to SEDP:", error);
    return {
      success: false,
      message: error.message || "Failed to send data to SEDP"
    };
  }
}


export async function deleteFormDataByTags({
  formCode,
  contractItemId,
  tagIdxs,
}: {
  formCode: string
  contractItemId: number
  tagIdxs: string[]
}): Promise<{
  error?: string
  success?: boolean
  deletedCount?: number
  deletedTagsCount?: number
}> {
  try {
    // 입력 검증
    if (!formCode || !contractItemId || !Array.isArray(tagIdxs) || tagIdxs.length === 0) {
      return {
        error: "Missing required parameters: formCode, contractItemId, tagIdxs",
      }
    }

    console.log(`[DELETE ACTION] Deleting tags for formCode: ${formCode}, contractItemId: ${contractItemId}, tagNos:`, tagIdxs)

    // 트랜잭션으로 안전하게 처리
    const result = await db.transaction(async (tx) => {
      // 1. 현재 formEntry 데이터 가져오기
      const currentEntryResult = await tx
        .select()
        .from(formEntries)
        .where(
          and(
            eq(formEntries.formCode, formCode),
            eq(formEntries.contractItemId, contractItemId)
          )
        )
        .orderBy(desc(formEntries.updatedAt))
        .limit(1)

      if (currentEntryResult.length === 0) {
        throw new Error("Form entry not found")
      }

      const currentEntry = currentEntryResult[0]
      let currentData = Array.isArray(currentEntry.data) ? currentEntry.data : []

      console.log(`[DELETE ACTION] Current data count: ${currentData.length}`)

      // 2. 삭제할 항목들 필터링 (formEntries에서)
      const updatedData = currentData.filter((item: any) =>
        !tagIdxs.includes(item.TAG_IDX)
      )

      const deletedFromFormEntries = currentData.length - updatedData.length

      console.log(`[DELETE ACTION] Updated data count: ${updatedData.length}`)
      console.log(`[DELETE ACTION] Deleted ${deletedFromFormEntries} items from formEntries`)

      if (deletedFromFormEntries === 0) {
        throw new Error("No items were found to delete in formEntries")
      }

      // 3. tags 테이블에서 해당 태그들 삭제
      const deletedTagsResult = await tx
        .delete(tags)
        .where(
          and(
            eq(tags.contractItemId, contractItemId),
            inArray(tags.tagIdx, tagIdxs)
          )
        )
        .returning({ tagNo: tags.tagNo })

      const deletedTagsCount = deletedTagsResult.length

      console.log(`[DELETE ACTION] Deleted ${deletedTagsCount} items from tags table`)
      console.log(`[DELETE ACTION] Deleted tag numbers:`, deletedTagsResult.map(t => t.tagNo))

      // 4. formEntries 데이터 업데이트
      await tx
        .update(formEntries)
        .set({
          data: updatedData,
          updatedAt: new Date(),
        })
        .where(
          and(
            eq(formEntries.formCode, formCode),
            eq(formEntries.contractItemId, contractItemId)
          )
        )

      return {
        deletedFromFormEntries,
        deletedTagsCount,
        deletedTagNumbers: deletedTagsResult.map(t => t.tagNo)
      }
    })

    // 5. 캐시 무효화
    const cacheKey = `form-data-${formCode}-${contractItemId}`
    revalidateTag(cacheKey)
    revalidateTag(`tags-${contractItemId}`)

    // 페이지 재검증 (필요한 경우)

    console.log(`[DELETE ACTION] Transaction completed successfully`)
    console.log(`[DELETE ACTION] FormEntries deleted: ${result.deletedFromFormEntries}`)
    console.log(`[DELETE ACTION] Tags deleted: ${result.deletedTagsCount}`)

    return {
      success: true,
      deletedCount: result.deletedFromFormEntries,
      deletedTagsCount: result.deletedTagsCount,
    }

  } catch (error) {
    console.error("[DELETE ACTION] Error deleting form data:", error)
    return {
      error: error instanceof Error ? error.message : "An unexpected error occurred",
    }
  }
}

/**
 * Server action to exclude selected tags by updating their status
 */
export async function excludeFormDataByTags({
  formCode,
  contractItemId,
  tagNumbers,
}: {
  formCode: string
  contractItemId: number
  tagNumbers: string[]
}): Promise<{
  error?: string
  success?: boolean
  excludedCount?: number
}> {
  try {
    // 입력 검증
    if (!formCode || !contractItemId || !Array.isArray(tagNumbers) || tagNumbers.length === 0) {
      return {
        error: "Missing required parameters: formCode, contractItemId, tagNumbers",
      }
    }

    console.log(`[EXCLUDE ACTION] Excluding tags for formCode: ${formCode}, contractItemId: ${contractItemId}, tagNumbers:`, tagNumbers)

    // 트랜잭션으로 안전하게 처리
    const result = await db.transaction(async (tx) => {
      // 1. 현재 formEntry 데이터 가져오기
      const currentEntryResult = await tx
        .select()
        .from(formEntries)
        .where(
          and(
            eq(formEntries.formCode, formCode),
            eq(formEntries.contractItemId, contractItemId)
          )
        )
        .orderBy(desc(formEntries.updatedAt))
        .limit(1)

      if (currentEntryResult.length === 0) {
        throw new Error("Form entry not found")
      }

      const currentEntry = currentEntryResult[0]
      let currentData = Array.isArray(currentEntry.data) ? currentEntry.data : []

      console.log(`[EXCLUDE ACTION] Current data count: ${currentData.length}`)

      // 2. TAG_NO가 일치하는 항목들의 status를 'excluded'로 업데이트
      let excludedCount = 0
      const updatedData = currentData.map((item: any) => {
        if (tagNumbers.includes(item.TAG_NO)) {
          excludedCount++
          return {
            ...item,
            status: 'excluded',
            excludedAt: new Date().toISOString() // 제외 시간 추가 (선택사항)
          }
        }
        return item
      })

      console.log(`[EXCLUDE ACTION] Excluded ${excludedCount} items`)

      if (excludedCount === 0) {
        throw new Error("No items were found to exclude")
      }

      // 3. formEntries 데이터 업데이트
      await tx
        .update(formEntries)
        .set({
          data: updatedData,
          updatedAt: new Date(),
        })
        .where(
          and(
            eq(formEntries.formCode, formCode),
            eq(formEntries.contractItemId, contractItemId)
          )
        )

      return {
        excludedCount,
        excludedTagNumbers: tagNumbers
      }
    })

    // 4. 캐시 무효화
    const cacheKey = `form-data-${formCode}-${contractItemId}`
    revalidateTag(cacheKey)

    console.log(`[EXCLUDE ACTION] Transaction completed successfully`)
    console.log(`[EXCLUDE ACTION] Tags excluded: ${result.excludedCount}`)

    return {
      success: true,
      excludedCount: result.excludedCount,
    }

  } catch (error) {
    console.error("[EXCLUDE ACTION] Error excluding form data:", error)
    return {
      error: error instanceof Error ? error.message : "An unexpected error occurred",
    }
  }
}



export async function getRegisters(projectCode: string): Promise<Register[]> {
  try {
    // 토큰(API 키) 가져오기
    const apiKey = await getSEDPToken();
    const SEDP_API_BASE_URL = process.env.SEDP_API_BASE_URL || 'http://sedpwebapi.ship.samsung.co.kr/api';

    const response = await fetch(
      `${SEDP_API_BASE_URL}/Register/Get`,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'accept': '*/*',
          'ApiKey': apiKey,
          'ProjectNo': projectCode
        },
        body: JSON.stringify({
          ProjectNo: projectCode,
          ContainDeleted: false
        })
      }
    );

    if (!response.ok) {
      throw new Error(`레지스터 요청 실패: ${response.status} ${response.statusText}`);
    }

    // 안전하게 JSON 파싱
    let data;
    try {
      data = await response.json();
    } catch (parseError) {
      console.error(`프로젝트 ${projectCode}의 레지스터 응답 파싱 실패:`, parseError);
      // 응답 내용 로깅
      const text = await response.clone().text();
      console.log(`응답 내용: ${text.substring(0, 200)}${text.length > 200 ? '...' : ''}`);
      throw new Error(`레지스터 응답 파싱 실패: ${parseError instanceof Error ? parseError.message : String(parseError)}`);
    }

    // 결과를 배열로 변환 (단일 객체인 경우 배열로 래핑)
    const registers: Register[] = Array.isArray(data) ? data : [data];

    console.log(`프로젝트 ${projectCode}에서 ${registers.length}개의 유효한 레지스터를 가져왔습니다.`);
    return registers;
  } catch (error) {
    console.error(`프로젝트 ${projectCode}의 레지스터 가져오기 실패:`, error);
    throw error;
  }
}