oxirs-fuseki 0.2.4

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
Documentation
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
//! # Validation Endpoints
//!
//! Apache Fuseki-compatible validation services for SPARQL queries,
//! SPARQL Update operations, IRIs, RDF data, and language tags.
//!
//! ## Endpoints
//!
//! - `POST /$/validate/query` - Validate SPARQL queries
//! - `POST /$/validate/update` - Validate SPARQL Update operations
//! - `POST /$/validate/iri` - Validate IRIs
//! - `POST /$/validate/data` - Validate RDF data
//! - `POST /$/validate/langtag` - Validate BCP 47 language tags
//!
//! ## Features
//!
//! - Query validation with formatted output and algebra representation
//! - IRI validation with error/warning reporting
//! - RDF data validation for multiple formats (Turtle, N-Triples, N-Quads, RDF/XML, JSON-LD)
//! - Language tag validation according to BCP 47
//!
//! ## Example
//!
//! ```http
//! POST /$/validate/query HTTP/1.1
//! Content-Type: application/x-www-form-urlencoded
//!
//! query=SELECT ?s WHERE { ?s ?p ?o }
//! ```
//!
//! Response:
//! ```json
//! {
//!   "valid": true,
//!   "input": "SELECT ?s WHERE { ?s ?p ?o }",
//!   "formatted": "SELECT ?s\nWHERE { ?s ?p ?o }",
//!   "algebra": "(bgp (triple ?s ?p ?o))"
//! }
//! ```

use axum::{
    extract::{Query, State},
    http::StatusCode,
    response::{IntoResponse, Response},
    Json,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

use crate::error::FusekiResult;
use crate::server::AppState;

// ============================================================================
// Validation Request/Response Types
// ============================================================================

/// Query validation request
#[derive(Debug, Clone, Deserialize)]
pub struct QueryValidationRequest {
    /// The SPARQL query to validate
    pub query: String,
    /// Optional syntax specification (defaults to SPARQL 1.1)
    #[serde(default = "default_sparql_syntax")]
    pub syntax: String,
    /// Whether to include algebra output
    #[serde(default = "default_true")]
    pub include_algebra: bool,
    /// Whether to include optimized algebra
    #[serde(default = "default_true")]
    pub include_optimized: bool,
}

fn default_sparql_syntax() -> String {
    "sparql11".to_string()
}

fn default_true() -> bool {
    true
}

/// Query validation response
#[derive(Debug, Clone, Serialize)]
pub struct QueryValidationResponse {
    /// Whether the query is valid
    pub valid: bool,
    /// Original input query
    pub input: String,
    /// Formatted/pretty-printed query (if valid)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub formatted: Option<String>,
    /// Query type (SELECT, CONSTRUCT, ASK, DESCRIBE)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub query_type: Option<String>,
    /// Algebra representation
    #[serde(skip_serializing_if = "Option::is_none")]
    pub algebra: Option<String>,
    /// Optimized algebra representation
    #[serde(skip_serializing_if = "Option::is_none")]
    pub algebra_optimized: Option<String>,
    /// Variables in the query
    #[serde(skip_serializing_if = "Option::is_none")]
    pub variables: Option<Vec<String>>,
    /// Prefixes used in the query
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prefixes: Option<Vec<PrefixMapping>>,
    /// Parse errors (if invalid)
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub errors: Vec<ValidationError>,
    /// Warnings (non-fatal issues)
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<ValidationWarning>,
}

/// Update validation request
#[derive(Debug, Clone, Deserialize)]
pub struct UpdateValidationRequest {
    /// The SPARQL Update to validate
    pub update: String,
    /// Optional syntax specification
    #[serde(default = "default_sparql_syntax")]
    pub syntax: String,
}

/// Update validation response
#[derive(Debug, Clone, Serialize)]
pub struct UpdateValidationResponse {
    /// Whether the update is valid
    pub valid: bool,
    /// Original input update
    pub input: String,
    /// Formatted/pretty-printed update (if valid)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub formatted: Option<String>,
    /// Operation types (INSERT DATA, DELETE DATA, etc.)
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub operations: Vec<String>,
    /// Graphs affected by the update
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub affected_graphs: Vec<String>,
    /// Parse errors (if invalid)
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub errors: Vec<ValidationError>,
    /// Warnings
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<ValidationWarning>,
}

/// IRI validation request
#[derive(Debug, Clone, Deserialize)]
pub struct IriValidationRequest {
    /// IRIs to validate (can be multiple)
    pub iris: Vec<String>,
    /// Whether to check for relative IRIs (default: true)
    #[serde(default = "default_true")]
    pub check_relative: bool,
}

/// IRI validation response
#[derive(Debug, Clone, Serialize)]
pub struct IriValidationResponse {
    /// Validation results for each IRI
    pub results: Vec<IriValidationResult>,
    /// Overall summary
    pub summary: ValidationSummary,
}

/// Individual IRI validation result
#[derive(Debug, Clone, Serialize)]
pub struct IriValidationResult {
    /// The IRI that was validated
    pub iri: String,
    /// Whether the IRI is valid
    pub valid: bool,
    /// Whether the IRI is absolute
    pub is_absolute: bool,
    /// Whether the IRI is relative
    pub is_relative: bool,
    /// IRI scheme (http, https, urn, etc.)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scheme: Option<String>,
    /// Errors found
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub errors: Vec<String>,
    /// Warnings found
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<String>,
}

/// RDF data validation request
#[derive(Debug, Clone, Deserialize)]
pub struct DataValidationRequest {
    /// The RDF data to validate
    pub data: String,
    /// Content type / format (turtle, ntriples, nquads, rdfxml, jsonld)
    #[serde(default = "default_format")]
    pub format: String,
    /// Base IRI for relative IRI resolution
    #[serde(skip_serializing_if = "Option::is_none")]
    pub base: Option<String>,
}

fn default_format() -> String {
    "turtle".to_string()
}

/// RDF data validation response
#[derive(Debug, Clone, Serialize)]
pub struct DataValidationResponse {
    /// Whether the data is valid
    pub valid: bool,
    /// Format detected/used
    pub format: String,
    /// Number of triples/quads parsed
    pub triple_count: usize,
    /// Number of graphs (for quads)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub graph_count: Option<usize>,
    /// Subjects found
    pub subject_count: usize,
    /// Predicates found
    pub predicate_count: usize,
    /// Objects found
    pub object_count: usize,
    /// Blank nodes found
    pub blank_node_count: usize,
    /// Literals found
    pub literal_count: usize,
    /// Parse errors (if invalid)
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub errors: Vec<ValidationError>,
    /// Warnings
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<ValidationWarning>,
    /// Sample triples (first 10)
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub sample_triples: Vec<String>,
}

/// Language tag validation request
#[derive(Debug, Clone, Deserialize)]
pub struct LangTagValidationRequest {
    /// Language tags to validate
    pub tags: Vec<String>,
}

/// Language tag validation response
#[derive(Debug, Clone, Serialize)]
pub struct LangTagValidationResponse {
    /// Validation results for each tag
    pub results: Vec<LangTagValidationResult>,
    /// Overall summary
    pub summary: ValidationSummary,
}

/// Individual language tag validation result
#[derive(Debug, Clone, Serialize)]
pub struct LangTagValidationResult {
    /// The tag that was validated
    pub tag: String,
    /// Whether the tag is valid (well-formed)
    pub valid: bool,
    /// Primary language subtag
    #[serde(skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,
    /// Script subtag
    #[serde(skip_serializing_if = "Option::is_none")]
    pub script: Option<String>,
    /// Region subtag
    #[serde(skip_serializing_if = "Option::is_none")]
    pub region: Option<String>,
    /// Variant subtags
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub variants: Vec<String>,
    /// Extension subtags
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub extensions: Vec<String>,
    /// Private use subtag
    #[serde(skip_serializing_if = "Option::is_none")]
    pub private_use: Option<String>,
    /// Errors found
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub errors: Vec<String>,
    /// Warnings found
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<String>,
}

/// Validation error details
#[derive(Debug, Clone, Serialize)]
pub struct ValidationError {
    /// Error message
    pub message: String,
    /// Line number (if applicable)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub line: Option<usize>,
    /// Column number (if applicable)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub column: Option<usize>,
    /// Error code
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,
}

/// Validation warning
#[derive(Debug, Clone, Serialize)]
pub struct ValidationWarning {
    /// Warning message
    pub message: String,
    /// Line number (if applicable)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub line: Option<usize>,
    /// Warning code
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,
}

/// Prefix mapping
#[derive(Debug, Clone, Serialize)]
pub struct PrefixMapping {
    /// Prefix (e.g., "rdf")
    pub prefix: String,
    /// IRI (e.g., `http://www.w3.org/1999/02/22-rdf-syntax-ns#`)
    pub iri: String,
}

/// Validation summary
#[derive(Debug, Clone, Serialize)]
pub struct ValidationSummary {
    /// Total items validated
    pub total: usize,
    /// Number of valid items
    pub valid: usize,
    /// Number of invalid items
    pub invalid: usize,
    /// Number of warnings
    pub warnings: usize,
}

// ============================================================================
// Query Parameters for GET requests
// ============================================================================

/// Query parameters for query validation
#[derive(Debug, Clone, Deserialize)]
pub struct QueryValidationParams {
    pub query: Option<String>,
    pub syntax: Option<String>,
}

/// Query parameters for update validation
#[derive(Debug, Clone, Deserialize)]
pub struct UpdateValidationParams {
    pub update: Option<String>,
    pub syntax: Option<String>,
}

/// Query parameters for IRI validation
#[derive(Debug, Clone, Deserialize)]
pub struct IriValidationParams {
    /// Single IRI or comma-separated list
    pub iri: Option<String>,
}

/// Query parameters for language tag validation
#[derive(Debug, Clone, Deserialize)]
pub struct LangTagValidationParams {
    /// Single tag or comma-separated list
    pub tag: Option<String>,
}

// ============================================================================
// Validation Handlers
// ============================================================================

/// Validate SPARQL query (POST)
pub async fn validate_query(
    State(_state): State<Arc<AppState>>,
    Json(request): Json<QueryValidationRequest>,
) -> Response {
    let result = validate_sparql_query_internal(&request);
    (StatusCode::OK, Json(result)).into_response()
}

/// Validate SPARQL query (GET)
pub async fn validate_query_get(
    State(_state): State<Arc<AppState>>,
    Query(params): Query<QueryValidationParams>,
) -> Response {
    let query = match params.query {
        Some(q) => q,
        None => {
            return (
                StatusCode::BAD_REQUEST,
                Json(QueryValidationResponse {
                    valid: false,
                    input: String::new(),
                    formatted: None,
                    query_type: None,
                    algebra: None,
                    algebra_optimized: None,
                    variables: None,
                    prefixes: None,
                    errors: vec![ValidationError {
                        message: "Missing 'query' parameter".to_string(),
                        line: None,
                        column: None,
                        code: Some("MISSING_PARAM".to_string()),
                    }],
                    warnings: vec![],
                }),
            )
                .into_response();
        }
    };

    let request = QueryValidationRequest {
        query,
        syntax: params.syntax.unwrap_or_else(default_sparql_syntax),
        include_algebra: true,
        include_optimized: true,
    };

    let result = validate_sparql_query_internal(&request);
    (StatusCode::OK, Json(result)).into_response()
}

/// Validate SPARQL Update (POST)
pub async fn validate_update(
    State(_state): State<Arc<AppState>>,
    Json(request): Json<UpdateValidationRequest>,
) -> Response {
    let result = validate_sparql_update_internal(&request);
    (StatusCode::OK, Json(result)).into_response()
}

/// Validate SPARQL Update (GET)
pub async fn validate_update_get(
    State(_state): State<Arc<AppState>>,
    Query(params): Query<UpdateValidationParams>,
) -> Response {
    let update = match params.update {
        Some(u) => u,
        None => {
            return (
                StatusCode::BAD_REQUEST,
                Json(UpdateValidationResponse {
                    valid: false,
                    input: String::new(),
                    formatted: None,
                    operations: vec![],
                    affected_graphs: vec![],
                    errors: vec![ValidationError {
                        message: "Missing 'update' parameter".to_string(),
                        line: None,
                        column: None,
                        code: Some("MISSING_PARAM".to_string()),
                    }],
                    warnings: vec![],
                }),
            )
                .into_response();
        }
    };

    let request = UpdateValidationRequest {
        update,
        syntax: params.syntax.unwrap_or_else(default_sparql_syntax),
    };

    let result = validate_sparql_update_internal(&request);
    (StatusCode::OK, Json(result)).into_response()
}

/// Validate IRIs (POST)
pub async fn validate_iri(
    State(_state): State<Arc<AppState>>,
    Json(request): Json<IriValidationRequest>,
) -> Response {
    let result = validate_iris_internal(&request);
    (StatusCode::OK, Json(result)).into_response()
}

/// Validate IRIs (GET)
pub async fn validate_iri_get(
    State(_state): State<Arc<AppState>>,
    Query(params): Query<IriValidationParams>,
) -> Response {
    let iris = match params.iri {
        Some(iri_str) => iri_str.split(',').map(|s| s.trim().to_string()).collect(),
        None => {
            return (
                StatusCode::BAD_REQUEST,
                Json(IriValidationResponse {
                    results: vec![],
                    summary: ValidationSummary {
                        total: 0,
                        valid: 0,
                        invalid: 0,
                        warnings: 0,
                    },
                }),
            )
                .into_response();
        }
    };

    let request = IriValidationRequest {
        iris,
        check_relative: true,
    };

    let result = validate_iris_internal(&request);
    (StatusCode::OK, Json(result)).into_response()
}

/// Validate RDF data (POST)
pub async fn validate_data(
    State(_state): State<Arc<AppState>>,
    Json(request): Json<DataValidationRequest>,
) -> Response {
    let result = validate_rdf_data_internal(&request);
    (StatusCode::OK, Json(result)).into_response()
}

/// Validate language tags (POST)
pub async fn validate_langtag(
    State(_state): State<Arc<AppState>>,
    Json(request): Json<LangTagValidationRequest>,
) -> Response {
    let result = validate_langtags_internal(&request);
    (StatusCode::OK, Json(result)).into_response()
}

/// Validate language tags (GET)
pub async fn validate_langtag_get(
    State(_state): State<Arc<AppState>>,
    Query(params): Query<LangTagValidationParams>,
) -> Response {
    let tags = match params.tag {
        Some(tag_str) => tag_str.split(',').map(|s| s.trim().to_string()).collect(),
        None => {
            return (
                StatusCode::BAD_REQUEST,
                Json(LangTagValidationResponse {
                    results: vec![],
                    summary: ValidationSummary {
                        total: 0,
                        valid: 0,
                        invalid: 0,
                        warnings: 0,
                    },
                }),
            )
                .into_response();
        }
    };

    let request = LangTagValidationRequest { tags };

    let result = validate_langtags_internal(&request);
    (StatusCode::OK, Json(result)).into_response()
}

// ============================================================================
// Internal Validation Functions
// ============================================================================

/// Internal function to validate SPARQL queries
fn validate_sparql_query_internal(request: &QueryValidationRequest) -> QueryValidationResponse {
    let query_str = request.query.trim();

    if query_str.is_empty() {
        return QueryValidationResponse {
            valid: false,
            input: request.query.clone(),
            formatted: None,
            query_type: None,
            algebra: None,
            algebra_optimized: None,
            variables: None,
            prefixes: None,
            errors: vec![ValidationError {
                message: "Empty query string".to_string(),
                line: Some(1),
                column: Some(1),
                code: Some("EMPTY_QUERY".to_string()),
            }],
            warnings: vec![],
        };
    }

    // Validate the query using available validation functions
    // Note: oxirs-core doesn't expose a simple Query::parse API yet,
    // so we use a combination of syntax checking and the existing validate function
    match crate::handlers::sparql::validate_sparql_query(query_str) {
        Ok(_) => {
            // Extract query information
            let query_type = detect_query_type(query_str);
            let variables = extract_query_variables(query_str);
            let prefixes = extract_prefixes(query_str);

            // Generate formatted output
            let formatted = format_query(query_str);

            // Generate algebra representation (simplified)
            let algebra = if request.include_algebra {
                Some(generate_algebra_representation(query_str))
            } else {
                None
            };

            // Generate optimized algebra (placeholder - would need actual optimizer)
            let algebra_optimized = if request.include_optimized {
                Some(generate_optimized_algebra(query_str))
            } else {
                None
            };

            let mut warnings = vec![];

            // Check for potential issues
            if query_str.contains("SELECT *") {
                warnings.push(ValidationWarning {
                    message: "Using SELECT * may return more data than needed".to_string(),
                    line: None,
                    code: Some("SELECT_STAR".to_string()),
                });
            }

            if !query_str.to_uppercase().contains("LIMIT")
                && query_type == Some("SELECT".to_string())
            {
                warnings.push(ValidationWarning {
                    message: "Query has no LIMIT clause, may return large result sets".to_string(),
                    line: None,
                    code: Some("NO_LIMIT".to_string()),
                });
            }

            QueryValidationResponse {
                valid: true,
                input: request.query.clone(),
                formatted: Some(formatted),
                query_type,
                algebra,
                algebra_optimized,
                variables: Some(variables),
                prefixes: Some(prefixes),
                errors: vec![],
                warnings,
            }
        }
        Err(e) => {
            // Parse error occurred
            let (line, column) = extract_error_location(&e.to_string());

            QueryValidationResponse {
                valid: false,
                input: request.query.clone(),
                formatted: None,
                query_type: None,
                algebra: None,
                algebra_optimized: None,
                variables: None,
                prefixes: None,
                errors: vec![ValidationError {
                    message: e.to_string(),
                    line,
                    column,
                    code: Some("PARSE_ERROR".to_string()),
                }],
                warnings: vec![],
            }
        }
    }
}

/// Internal function to validate SPARQL Update operations
fn validate_sparql_update_internal(request: &UpdateValidationRequest) -> UpdateValidationResponse {
    let update_str = request.update.trim();

    if update_str.is_empty() {
        return UpdateValidationResponse {
            valid: false,
            input: request.update.clone(),
            formatted: None,
            operations: vec![],
            affected_graphs: vec![],
            errors: vec![ValidationError {
                message: "Empty update string".to_string(),
                line: Some(1),
                column: Some(1),
                code: Some("EMPTY_UPDATE".to_string()),
            }],
            warnings: vec![],
        };
    }

    // Validate the update using basic syntax checking
    // Note: oxirs-core doesn't expose a simple Update::parse API yet
    let is_valid_update = validate_sparql_update_syntax(update_str);

    match is_valid_update {
        Ok(_) => {
            // Extract operation types
            let operations = extract_update_operations(update_str);

            // Extract affected graphs
            let affected_graphs = extract_affected_graphs(update_str);

            // Format the update
            let formatted = format_update(update_str);

            let mut warnings = vec![];

            // Check for potential issues
            if update_str.to_uppercase().contains("DELETE WHERE")
                && !update_str.to_uppercase().contains("GRAPH")
            {
                warnings.push(ValidationWarning {
                    message: "DELETE WHERE without GRAPH clause affects default graph".to_string(),
                    line: None,
                    code: Some("DEFAULT_GRAPH_DELETE".to_string()),
                });
            }

            if update_str.to_uppercase().contains("DROP ALL") {
                warnings.push(ValidationWarning {
                    message: "DROP ALL will remove all data from all graphs".to_string(),
                    line: None,
                    code: Some("DROP_ALL".to_string()),
                });
            }

            if update_str.to_uppercase().contains("CLEAR ALL") {
                warnings.push(ValidationWarning {
                    message: "CLEAR ALL will remove all triples from all graphs".to_string(),
                    line: None,
                    code: Some("CLEAR_ALL".to_string()),
                });
            }

            UpdateValidationResponse {
                valid: true,
                input: request.update.clone(),
                formatted: Some(formatted),
                operations,
                affected_graphs,
                errors: vec![],
                warnings,
            }
        }
        Err(e) => {
            let (line, column) = extract_error_location(&e.to_string());

            UpdateValidationResponse {
                valid: false,
                input: request.update.clone(),
                formatted: None,
                operations: vec![],
                affected_graphs: vec![],
                errors: vec![ValidationError {
                    message: e.to_string(),
                    line,
                    column,
                    code: Some("PARSE_ERROR".to_string()),
                }],
                warnings: vec![],
            }
        }
    }
}

/// Internal function to validate IRIs
fn validate_iris_internal(request: &IriValidationRequest) -> IriValidationResponse {
    let mut results = Vec::new();
    let mut valid_count = 0;
    let mut warning_count = 0;

    for iri_str in &request.iris {
        let iri_str = iri_str.trim();
        let mut errors = Vec::new();
        let mut warnings = Vec::new();
        let mut is_valid = true;
        let mut is_absolute = false;
        let mut is_relative = false;
        let mut scheme = None;

        if iri_str.is_empty() {
            errors.push("Empty IRI string".to_string());
            is_valid = false;
        } else {
            // Parse the IRI using oxirs-core
            match oxirs_core::model::NamedNode::new(iri_str) {
                Ok(node) => {
                    let iri = node.as_str();

                    // Check if absolute or relative
                    if iri.contains("://") {
                        is_absolute = true;
                        // Extract scheme
                        if let Some(idx) = iri.find("://") {
                            scheme = Some(iri[..idx].to_string());
                        }
                    } else if iri.starts_with("urn:") || iri.starts_with("mailto:") {
                        is_absolute = true;
                        if let Some(idx) = iri.find(':') {
                            scheme = Some(iri[..idx].to_string());
                        }
                    } else {
                        is_relative = true;
                        if request.check_relative {
                            warnings.push("Relative IRI detected".to_string());
                            warning_count += 1;
                        }
                    }

                    // Additional validation checks
                    if iri.contains(' ') {
                        warnings.push("IRI contains spaces".to_string());
                        warning_count += 1;
                    }

                    if iri.contains("..") {
                        warnings.push("IRI contains '..' path segments".to_string());
                        warning_count += 1;
                    }

                    // Check for common issues
                    if iri.ends_with('#') || iri.ends_with('/') {
                        // This is actually fine for namespace IRIs
                    }

                    valid_count += 1;
                }
                Err(e) => {
                    errors.push(format!("Invalid IRI: {}", e));
                    is_valid = false;
                }
            }
        }

        results.push(IriValidationResult {
            iri: iri_str.to_string(),
            valid: is_valid,
            is_absolute,
            is_relative,
            scheme,
            errors,
            warnings,
        });
    }

    IriValidationResponse {
        summary: ValidationSummary {
            total: results.len(),
            valid: valid_count,
            invalid: results.len() - valid_count,
            warnings: warning_count,
        },
        results,
    }
}

/// Internal function to validate RDF data
fn validate_rdf_data_internal(request: &DataValidationRequest) -> DataValidationResponse {
    let data_str = request.data.trim();

    if data_str.is_empty() {
        return DataValidationResponse {
            valid: false,
            format: request.format.clone(),
            triple_count: 0,
            graph_count: None,
            subject_count: 0,
            predicate_count: 0,
            object_count: 0,
            blank_node_count: 0,
            literal_count: 0,
            errors: vec![ValidationError {
                message: "Empty data string".to_string(),
                line: Some(1),
                column: Some(1),
                code: Some("EMPTY_DATA".to_string()),
            }],
            warnings: vec![],
            sample_triples: vec![],
        };
    }

    // Determine format
    let format = normalize_format(&request.format);
    let base_iri = request
        .base
        .clone()
        .unwrap_or_else(|| "http://example.org/base/".to_string());

    // Parse RDF data
    match parse_rdf_data(data_str, &format, &base_iri) {
        Ok(parse_result) => {
            let mut warnings = vec![];

            // Check for potential issues
            if parse_result.blank_node_count > parse_result.triple_count / 2 {
                warnings.push(ValidationWarning {
                    message: "High ratio of blank nodes to triples".to_string(),
                    line: None,
                    code: Some("HIGH_BNODE_RATIO".to_string()),
                });
            }

            DataValidationResponse {
                valid: true,
                format,
                triple_count: parse_result.triple_count,
                graph_count: parse_result.graph_count,
                subject_count: parse_result.subject_count,
                predicate_count: parse_result.predicate_count,
                object_count: parse_result.object_count,
                blank_node_count: parse_result.blank_node_count,
                literal_count: parse_result.literal_count,
                errors: vec![],
                warnings,
                sample_triples: parse_result.sample_triples,
            }
        }
        Err(e) => {
            let (line, column) = extract_error_location(&e);

            DataValidationResponse {
                valid: false,
                format,
                triple_count: 0,
                graph_count: None,
                subject_count: 0,
                predicate_count: 0,
                object_count: 0,
                blank_node_count: 0,
                literal_count: 0,
                errors: vec![ValidationError {
                    message: e,
                    line,
                    column,
                    code: Some("PARSE_ERROR".to_string()),
                }],
                warnings: vec![],
                sample_triples: vec![],
            }
        }
    }
}

/// Internal function to validate language tags
fn validate_langtags_internal(request: &LangTagValidationRequest) -> LangTagValidationResponse {
    let mut results = Vec::new();
    let mut valid_count = 0;
    let mut warning_count = 0;

    for tag_str in &request.tags {
        let tag_str = tag_str.trim();
        let mut errors = Vec::new();
        let mut warnings = Vec::new();
        let mut is_valid = true;

        let mut language = None;
        let mut script = None;
        let mut region = None;
        let mut variants = Vec::new();
        let mut extensions = Vec::new();
        let mut private_use = None;

        if tag_str.is_empty() {
            errors.push("Empty language tag".to_string());
            is_valid = false;
        } else {
            // Parse BCP 47 language tag
            let result = parse_language_tag(tag_str);

            match result {
                Ok(parsed) => {
                    language = parsed.language;
                    script = parsed.script;
                    region = parsed.region;
                    variants = parsed.variants;
                    extensions = parsed.extensions;
                    private_use = parsed.private_use;

                    // Validate components
                    if language.is_none() && private_use.is_none() {
                        errors.push("Missing primary language subtag".to_string());
                        is_valid = false;
                    }

                    // Check for deprecated tags
                    if let Some(ref lang) = language {
                        if is_deprecated_language(lang) {
                            warnings.push(format!("Language subtag '{}' is deprecated", lang));
                            warning_count += 1;
                        }
                    }

                    // Check for unusual combinations
                    if script.is_some() && language.is_none() {
                        warnings.push("Script subtag without language subtag".to_string());
                        warning_count += 1;
                    }

                    if is_valid {
                        valid_count += 1;
                    }
                }
                Err(e) => {
                    errors.push(e);
                    is_valid = false;
                }
            }
        }

        results.push(LangTagValidationResult {
            tag: tag_str.to_string(),
            valid: is_valid,
            language,
            script,
            region,
            variants,
            extensions,
            private_use,
            errors,
            warnings,
        });
    }

    LangTagValidationResponse {
        summary: ValidationSummary {
            total: results.len(),
            valid: valid_count,
            invalid: results.len() - valid_count,
            warnings: warning_count,
        },
        results,
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Validate SPARQL Update syntax
fn validate_sparql_update_syntax(update: &str) -> Result<(), String> {
    if update.trim().is_empty() {
        return Err("Empty update string".to_string());
    }

    // Use regex for word-boundary matching to avoid false positives like "INSERTT"
    let operations_patterns = [
        r"(?i)\bINSERT\s+DATA\b",
        r"(?i)\bDELETE\s+DATA\b",
        r"(?i)\bINSERT\s*\{", // INSERT { ... } or INSERT WHERE { ... }
        r"(?i)\bDELETE\s*\{", // DELETE { ... } or DELETE WHERE { ... }
        r"(?i)\bLOAD\b",
        r"(?i)\bCLEAR\b",
        r"(?i)\bDROP\b",
        r"(?i)\bCREATE\b",
        r"(?i)\bCOPY\b",
        r"(?i)\bMOVE\b",
        r"(?i)\bADD\b",
        r"(?i)\bDELETE\s+WHERE\b",
        r"(?i)\bINSERT\s+WHERE\b",
        r"(?i)\bWITH\s+", // WITH <graph> ... INSERT/DELETE
    ];

    let has_valid_operation = operations_patterns.iter().any(|pattern| {
        regex::Regex::new(pattern)
            .map(|re| re.is_match(update))
            .unwrap_or(false)
    });

    if !has_valid_operation {
        return Err("Update must contain a valid SPARQL Update operation (INSERT, DELETE, LOAD, CLEAR, DROP, CREATE, COPY, MOVE, ADD)".to_string());
    }

    // Basic brace matching check
    let open_braces = update.matches('{').count();
    let close_braces = update.matches('}').count();
    if open_braces != close_braces {
        return Err(format!(
            "Mismatched braces: {} open, {} close",
            open_braces, close_braces
        ));
    }

    Ok(())
}

/// Detect query type from SPARQL query string
fn detect_query_type(query: &str) -> Option<String> {
    let upper = query.to_uppercase();
    if upper.contains("SELECT") {
        Some("SELECT".to_string())
    } else if upper.contains("CONSTRUCT") {
        Some("CONSTRUCT".to_string())
    } else if upper.contains("ASK") {
        Some("ASK".to_string())
    } else if upper.contains("DESCRIBE") {
        Some("DESCRIBE".to_string())
    } else {
        None
    }
}

/// Extract variable names from query
fn extract_query_variables(query: &str) -> Vec<String> {
    let mut variables = Vec::new();
    let mut chars = query.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '?' || c == '$' {
            let mut var_name = String::new();
            while let Some(&nc) = chars.peek() {
                if nc.is_alphanumeric() || nc == '_' {
                    var_name.push(chars.next().expect("char should exist after peek"));
                } else {
                    break;
                }
            }
            if !var_name.is_empty() && !variables.contains(&var_name) {
                variables.push(var_name);
            }
        }
    }

    variables
}

/// Extract prefix declarations from query
fn extract_prefixes(query: &str) -> Vec<PrefixMapping> {
    let mut prefixes = Vec::new();
    let prefix_regex =
        regex::Regex::new(r"(?i)PREFIX\s+(\w*):\s*<([^>]+)>").expect("valid regex pattern");

    for cap in prefix_regex.captures_iter(query) {
        prefixes.push(PrefixMapping {
            prefix: cap.get(1).map_or("", |m| m.as_str()).to_string(),
            iri: cap.get(2).map_or("", |m| m.as_str()).to_string(),
        });
    }

    prefixes
}

/// Format/pretty-print a SPARQL query
fn format_query(query: &str) -> String {
    // Simple formatting - indent WHERE clause, etc.
    let mut formatted = query.to_string();

    // Add newlines before main keywords
    let keywords = [
        "SELECT",
        "CONSTRUCT",
        "ASK",
        "DESCRIBE",
        "WHERE",
        "ORDER BY",
        "LIMIT",
        "OFFSET",
        "FILTER",
        "OPTIONAL",
        "UNION",
        "BIND",
        "VALUES",
        "GROUP BY",
        "HAVING",
    ];

    for keyword in keywords {
        let pattern = format!(r"(?i)\b{}\b", keyword);
        let re = regex::Regex::new(&pattern).expect("valid regex pattern");
        formatted = re
            .replace_all(&formatted, |caps: &regex::Captures| {
                format!(
                    "\n{}",
                    caps.get(0).expect("capture group 0 should exist").as_str()
                )
            })
            .to_string();
    }

    // Clean up multiple newlines
    let multi_newline = regex::Regex::new(r"\n\s*\n").expect("valid regex pattern");
    formatted = multi_newline.replace_all(&formatted, "\n").to_string();

    formatted.trim().to_string()
}

/// Format SPARQL Update
fn format_update(update: &str) -> String {
    let mut formatted = update.to_string();

    let keywords = [
        "INSERT", "DELETE", "WHERE", "DATA", "GRAPH", "WITH", "USING", "CREATE", "DROP", "COPY",
        "MOVE", "ADD", "CLEAR", "LOAD",
    ];

    for keyword in keywords {
        let pattern = format!(r"(?i)\b{}\b", keyword);
        let re = regex::Regex::new(&pattern).expect("valid regex pattern");
        formatted = re
            .replace_all(&formatted, |caps: &regex::Captures| {
                format!(
                    "\n{}",
                    caps.get(0).expect("capture group 0 should exist").as_str()
                )
            })
            .to_string();
    }

    let multi_newline = regex::Regex::new(r"\n\s*\n").expect("valid regex pattern");
    formatted = multi_newline.replace_all(&formatted, "\n").to_string();

    formatted.trim().to_string()
}

/// Generate algebra representation (simplified)
fn generate_algebra_representation(query: &str) -> String {
    // This is a simplified representation
    // A full implementation would use the actual SPARQL algebra
    let query_type = detect_query_type(query).unwrap_or_else(|| "UNKNOWN".to_string());

    let variables = extract_query_variables(query);
    let var_str = variables
        .iter()
        .map(|v| format!("?{}", v))
        .collect::<Vec<_>>()
        .join(" ");

    // Extract basic triple patterns
    let triple_pattern_re = regex::Regex::new(r"\{([^{}]+)\}").expect("valid regex pattern");
    let patterns: Vec<String> = triple_pattern_re
        .captures_iter(query)
        .filter_map(|cap| cap.get(1).map(|m| m.as_str().trim().to_string()))
        .collect();

    format!(
        "({}
  (project ({})
    (bgp
      {}
    )))",
        query_type.to_lowercase(),
        var_str,
        patterns.join("\n      ")
    )
}

/// Generate optimized algebra representation
fn generate_optimized_algebra(query: &str) -> String {
    // For now, return same as regular algebra
    // A full implementation would apply optimizations
    let algebra = generate_algebra_representation(query);
    format!("; Optimized\n{}", algebra)
}

/// Extract update operation types
fn extract_update_operations(update: &str) -> Vec<String> {
    let mut operations = Vec::new();
    let upper = update.to_uppercase();

    let op_types = [
        "INSERT DATA",
        "DELETE DATA",
        "INSERT",
        "DELETE",
        "CLEAR",
        "DROP",
        "CREATE",
        "COPY",
        "MOVE",
        "ADD",
        "LOAD",
    ];

    for op in op_types {
        if upper.contains(op) {
            operations.push(op.to_string());
        }
    }

    // Remove duplicates while preserving order
    let mut seen = std::collections::HashSet::new();
    operations.retain(|op| seen.insert(op.clone()));

    operations
}

/// Extract affected graphs from update
fn extract_affected_graphs(update: &str) -> Vec<String> {
    let mut graphs = Vec::new();

    // Look for GRAPH <uri> patterns
    let graph_re = regex::Regex::new(r"(?i)GRAPH\s*<([^>]+)>").expect("valid regex pattern");
    for cap in graph_re.captures_iter(update) {
        if let Some(g) = cap.get(1) {
            let graph_uri = g.as_str().to_string();
            if !graphs.contains(&graph_uri) {
                graphs.push(graph_uri);
            }
        }
    }

    // Check for DEFAULT keyword
    if update.to_uppercase().contains("DEFAULT") && !graphs.contains(&"default".to_string()) {
        graphs.push("default".to_string());
    }

    // Check for ALL keyword
    if update.to_uppercase().contains(" ALL") && !graphs.contains(&"all".to_string()) {
        graphs.push("all".to_string());
    }

    graphs
}

/// Extract error location from error message
fn extract_error_location(error: &str) -> (Option<usize>, Option<usize>) {
    // Try to extract line and column from error message
    let line_re = regex::Regex::new(r"line\s*(\d+)").expect("valid regex pattern");
    let col_re = regex::Regex::new(r"col(?:umn)?\s*(\d+)").expect("valid regex pattern");

    let line = line_re
        .captures(error)
        .and_then(|c| c.get(1))
        .and_then(|m| m.as_str().parse().ok());
    let column = col_re
        .captures(error)
        .and_then(|c| c.get(1))
        .and_then(|m| m.as_str().parse().ok());

    (line, column)
}

/// Normalize RDF format name
fn normalize_format(format: &str) -> String {
    match format.to_lowercase().as_str() {
        "turtle" | "ttl" | "text/turtle" => "turtle".to_string(),
        "ntriples" | "nt" | "application/n-triples" => "ntriples".to_string(),
        "nquads" | "nq" | "application/n-quads" => "nquads".to_string(),
        "rdfxml" | "rdf/xml" | "application/rdf+xml" => "rdfxml".to_string(),
        "jsonld" | "json-ld" | "application/ld+json" => "jsonld".to_string(),
        "trig" | "application/trig" => "trig".to_string(),
        _ => format.to_lowercase(),
    }
}

/// RDF parse result
struct RdfParseResult {
    triple_count: usize,
    graph_count: Option<usize>,
    subject_count: usize,
    predicate_count: usize,
    object_count: usize,
    blank_node_count: usize,
    literal_count: usize,
    sample_triples: Vec<String>,
}

/// Parse RDF data and collect statistics
fn parse_rdf_data(data: &str, format: &str, _base_iri: &str) -> Result<RdfParseResult, String> {
    use std::collections::HashSet;

    let mut subjects = HashSet::new();
    let mut predicates = HashSet::new();
    let mut objects = HashSet::new();
    let mut blank_nodes = HashSet::new();
    let mut literal_count = 0;
    let mut sample_triples = Vec::new();

    // Parse based on format using oxirs-core parsers
    let triples = match format {
        "turtle" | "ttl" => oxirs_core::format::turtle::TurtleParser::new()
            .parse_str(data)
            .map_err(|e| format!("Turtle parse error: {}", e))?,
        "ntriples" | "nt" => oxirs_core::format::ntriples::NTriplesParser::new()
            .parse_str(data)
            .map_err(|e| format!("N-Triples parse error: {}", e))?,
        "rdfxml" | "rdf/xml" => oxirs_core::format::rdfxml::RdfXmlParser::new()
            .parse_str(data)
            .map_err(|e| format!("RDF/XML parse error: {}", e))?,
        _ => {
            return Err(format!("Unsupported format: {}", format));
        }
    };

    let triple_count = triples.len();

    for triple in &triples {
        // Track subjects
        let subject_str = format!("{:?}", triple.subject());
        if subject_str.contains("BlankNode") {
            blank_nodes.insert(subject_str.clone());
        }
        subjects.insert(subject_str);

        // Track predicates
        predicates.insert(format!("{:?}", triple.predicate()));

        // Track objects
        let object_str = format!("{:?}", triple.object());
        if object_str.contains("BlankNode") {
            blank_nodes.insert(object_str.clone());
        } else if object_str.contains("Literal") {
            literal_count += 1;
        }
        objects.insert(object_str);

        // Sample triples
        if sample_triples.len() < 10 {
            sample_triples.push(format!(
                "{:?} {:?} {:?}",
                triple.subject(),
                triple.predicate(),
                triple.object()
            ));
        }
    }

    Ok(RdfParseResult {
        triple_count,
        graph_count: None, // Only for quads
        subject_count: subjects.len(),
        predicate_count: predicates.len(),
        object_count: objects.len(),
        blank_node_count: blank_nodes.len(),
        literal_count,
        sample_triples,
    })
}

/// Parsed language tag
struct ParsedLanguageTag {
    language: Option<String>,
    script: Option<String>,
    region: Option<String>,
    variants: Vec<String>,
    extensions: Vec<String>,
    private_use: Option<String>,
}

/// Parse BCP 47 language tag
fn parse_language_tag(tag: &str) -> Result<ParsedLanguageTag, String> {
    let parts: Vec<&str> = tag.split('-').collect();

    if parts.is_empty() {
        return Err("Empty language tag".to_string());
    }

    let mut language = None;
    let mut script = None;
    let mut region = None;
    let mut variants = Vec::new();
    let mut extensions = Vec::new();
    let mut private_use = None;

    let mut i = 0;

    // Check for private use tag
    if parts[0].to_lowercase() == "x" {
        if parts.len() > 1 {
            private_use = Some(parts[1..].join("-"));
        }
        return Ok(ParsedLanguageTag {
            language,
            script,
            region,
            variants,
            extensions,
            private_use,
        });
    }

    // Primary language subtag (2-3 letters or 4 letters for reserved)
    if parts[i].len() >= 2
        && parts[i].len() <= 3
        && parts[i].chars().all(|c| c.is_ascii_alphabetic())
    {
        language = Some(parts[i].to_lowercase());
        i += 1;
    } else if parts[i].len() == 4 && parts[i].chars().all(|c| c.is_ascii_alphabetic()) {
        // Reserved for future use
        language = Some(parts[i].to_lowercase());
        i += 1;
    } else {
        return Err(format!("Invalid primary language subtag: {}", parts[0]));
    }

    // Extended language subtags (3 letters each, up to 3)
    while i < parts.len()
        && parts[i].len() == 3
        && parts[i].chars().all(|c| c.is_ascii_alphabetic())
    {
        // Extended language subtags are appended to language
        if let Some(ref mut lang) = language {
            *lang = format!("{}-{}", lang, parts[i].to_lowercase());
        }
        i += 1;
        if i >= 4 {
            break; // Max 3 extended subtags
        }
    }

    // Script subtag (4 letters)
    if i < parts.len() && parts[i].len() == 4 && parts[i].chars().all(|c| c.is_ascii_alphabetic()) {
        let s = parts[i];
        // Capitalize first letter, lowercase rest
        script = Some(format!(
            "{}{}",
            s.chars()
                .next()
                .expect("script subtag should have at least one char")
                .to_uppercase(),
            s[1..].to_lowercase()
        ));
        i += 1;
    }

    // Region subtag (2 letters or 3 digits)
    if i < parts.len() {
        let p = parts[i];
        if (p.len() == 2 && p.chars().all(|c| c.is_ascii_alphabetic()))
            || (p.len() == 3 && p.chars().all(|c| c.is_ascii_digit()))
        {
            region = Some(p.to_uppercase());
            i += 1;
        }
    }

    // Variant subtags (5-8 alphanum or 4 starting with digit)
    while i < parts.len() {
        let p = parts[i];
        if (p.len() >= 5 && p.len() <= 8 && p.chars().all(|c| c.is_ascii_alphanumeric()))
            || (p.len() == 4
                && p.chars().next().is_some_and(|c| c.is_ascii_digit())
                && p.chars().all(|c| c.is_ascii_alphanumeric()))
        {
            variants.push(p.to_lowercase());
            i += 1;
        } else {
            break;
        }
    }

    // Extension subtags (singleton followed by 2-8 alphanum)
    while i < parts.len() {
        let p = parts[i];
        if p.len() == 1 && p.chars().all(|c| c.is_ascii_alphanumeric()) && p.to_lowercase() != "x" {
            let singleton = p.to_lowercase();
            let mut ext_parts = vec![singleton];
            i += 1;
            while i < parts.len() {
                let ep = parts[i];
                if ep.len() >= 2 && ep.len() <= 8 && ep.chars().all(|c| c.is_ascii_alphanumeric()) {
                    ext_parts.push(ep.to_lowercase());
                    i += 1;
                } else {
                    break;
                }
            }
            if ext_parts.len() > 1 {
                extensions.push(ext_parts.join("-"));
            }
        } else {
            break;
        }
    }

    // Private use subtag
    if i < parts.len() && parts[i].to_lowercase() == "x" {
        i += 1;
        if i < parts.len() {
            private_use = Some(parts[i..].join("-"));
        }
    }

    Ok(ParsedLanguageTag {
        language,
        script,
        region,
        variants,
        extensions,
        private_use,
    })
}

/// Check if a language subtag is deprecated
fn is_deprecated_language(lang: &str) -> bool {
    // List of deprecated language subtags (partial)
    let deprecated = [
        "iw",
        "ji",
        "in",
        "no-bok",
        "no-nyn",
        "sgn-be-fr",
        "sgn-be-nl",
        "sgn-ch-de",
    ];
    deprecated.contains(&lang.to_lowercase().as_str())
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validate_sparql_query_valid() {
        let request = QueryValidationRequest {
            query: "SELECT ?s ?p ?o WHERE { ?s ?p ?o }".to_string(),
            syntax: "sparql11".to_string(),
            include_algebra: true,
            include_optimized: true,
        };

        let response = validate_sparql_query_internal(&request);
        assert!(response.valid);
        assert_eq!(response.query_type, Some("SELECT".to_string()));
        assert!(response.variables.is_some());
        let vars = response.variables.unwrap();
        assert!(vars.contains(&"s".to_string()));
        assert!(vars.contains(&"p".to_string()));
        assert!(vars.contains(&"o".to_string()));
    }

    #[test]
    fn test_validate_sparql_query_invalid() {
        let request = QueryValidationRequest {
            query: "SELEKT ?s WHERE { ?s ?p ?o }".to_string(),
            syntax: "sparql11".to_string(),
            include_algebra: false,
            include_optimized: false,
        };

        let response = validate_sparql_query_internal(&request);
        assert!(!response.valid);
        assert!(!response.errors.is_empty());
    }

    #[test]
    fn test_validate_sparql_query_empty() {
        let request = QueryValidationRequest {
            query: "".to_string(),
            syntax: "sparql11".to_string(),
            include_algebra: false,
            include_optimized: false,
        };

        let response = validate_sparql_query_internal(&request);
        assert!(!response.valid);
        assert_eq!(response.errors[0].code, Some("EMPTY_QUERY".to_string()));
    }

    #[test]
    fn test_validate_sparql_update_valid() {
        let request = UpdateValidationRequest {
            update: "INSERT DATA { <http://example/s> <http://example/p> \"value\" }".to_string(),
            syntax: "sparql11".to_string(),
        };

        let response = validate_sparql_update_internal(&request);
        assert!(response.valid);
        assert!(response.operations.contains(&"INSERT DATA".to_string()));
    }

    #[test]
    fn test_validate_sparql_update_invalid() {
        let request = UpdateValidationRequest {
            update: "INSERTT DATA { <http://example/s> <http://example/p> \"value\" }".to_string(),
            syntax: "sparql11".to_string(),
        };

        let response = validate_sparql_update_internal(&request);
        assert!(!response.valid);
    }

    #[test]
    fn test_validate_iri_valid() {
        let request = IriValidationRequest {
            iris: vec![
                "http://example.org/resource".to_string(),
                "https://www.w3.org/2001/XMLSchema#string".to_string(),
                "urn:isbn:0451450523".to_string(),
            ],
            check_relative: true,
        };

        let response = validate_iris_internal(&request);
        assert_eq!(response.summary.total, 3);
        assert_eq!(response.summary.valid, 3);
        assert!(response.results[0].is_absolute);
        assert_eq!(response.results[0].scheme, Some("http".to_string()));
        assert_eq!(response.results[2].scheme, Some("urn".to_string()));
    }

    #[test]
    fn test_validate_iri_relative() {
        let request = IriValidationRequest {
            iris: vec!["resource/path".to_string()],
            check_relative: true,
        };

        let response = validate_iris_internal(&request);
        // Note: oxirs-core may reject relative IRIs as invalid NamedNodes
        // This test verifies the warning behavior
    }

    #[test]
    fn test_validate_rdf_data_turtle() {
        let request = DataValidationRequest {
            data: r#"
                @prefix ex: <http://example.org/> .
                ex:subject ex:predicate "object" .
                ex:s2 ex:p2 ex:o2 .
            "#
            .to_string(),
            format: "turtle".to_string(),
            base: Some("http://example.org/base/".to_string()),
        };

        let response = validate_rdf_data_internal(&request);
        assert!(response.valid);
        assert_eq!(response.triple_count, 2);
    }

    #[test]
    fn test_validate_rdf_data_invalid() {
        let request = DataValidationRequest {
            data: "@prefix ex: <broken".to_string(),
            format: "turtle".to_string(),
            base: None,
        };

        let response = validate_rdf_data_internal(&request);
        assert!(!response.valid);
        assert!(!response.errors.is_empty());
    }

    #[test]
    fn test_validate_langtag_valid() {
        let request = LangTagValidationRequest {
            tags: vec![
                "en".to_string(),
                "en-US".to_string(),
                "zh-Hans-CN".to_string(),
                "de-DE-1996".to_string(),
            ],
        };

        let response = validate_langtags_internal(&request);
        assert_eq!(response.summary.total, 4);
        assert_eq!(response.summary.valid, 4);

        assert_eq!(response.results[0].language, Some("en".to_string()));
        assert_eq!(response.results[1].region, Some("US".to_string()));
        assert_eq!(response.results[2].script, Some("Hans".to_string()));
    }

    #[test]
    fn test_validate_langtag_private_use() {
        let request = LangTagValidationRequest {
            tags: vec!["x-custom".to_string()],
        };

        let response = validate_langtags_internal(&request);
        assert_eq!(response.summary.valid, 1);
        assert_eq!(response.results[0].private_use, Some("custom".to_string()));
    }

    #[test]
    fn test_detect_query_type() {
        assert_eq!(
            detect_query_type("SELECT ?x WHERE { ?x ?y ?z }"),
            Some("SELECT".to_string())
        );
        assert_eq!(
            detect_query_type("CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }"),
            Some("CONSTRUCT".to_string())
        );
        assert_eq!(
            detect_query_type("ASK { ?s ?p ?o }"),
            Some("ASK".to_string())
        );
        assert_eq!(
            detect_query_type("DESCRIBE <http://example.org>"),
            Some("DESCRIBE".to_string())
        );
    }

    #[test]
    fn test_extract_query_variables() {
        let vars = extract_query_variables(
            "SELECT ?name ?age WHERE { ?person foaf:name ?name ; foaf:age ?age }",
        );
        assert!(vars.contains(&"name".to_string()));
        assert!(vars.contains(&"age".to_string()));
        assert!(vars.contains(&"person".to_string()));
    }

    #[test]
    fn test_extract_prefixes() {
        let prefixes = extract_prefixes("PREFIX foaf: <http://xmlns.com/foaf/0.1/>\nPREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\nSELECT * WHERE { ?s ?p ?o }");
        assert_eq!(prefixes.len(), 2);
        assert!(prefixes.iter().any(|p| p.prefix == "foaf"));
        assert!(prefixes.iter().any(|p| p.prefix == "rdf"));
    }

    #[test]
    fn test_extract_update_operations() {
        let ops = extract_update_operations(
            "INSERT DATA { <s> <p> \"o\" } ; DELETE DATA { <s2> <p2> \"o2\" }",
        );
        assert!(ops.contains(&"INSERT DATA".to_string()));
        assert!(ops.contains(&"DELETE DATA".to_string()));
    }

    #[test]
    fn test_extract_affected_graphs() {
        let graphs = extract_affected_graphs(
            "INSERT DATA { GRAPH <http://example.org/g1> { <s> <p> \"o\" } }",
        );
        assert!(graphs.contains(&"http://example.org/g1".to_string()));
    }

    #[test]
    fn test_parse_language_tag() {
        let result = parse_language_tag("en-Latn-US-valencia").unwrap();
        assert_eq!(result.language, Some("en".to_string()));
        assert_eq!(result.script, Some("Latn".to_string()));
        assert_eq!(result.region, Some("US".to_string()));
        assert!(result.variants.contains(&"valencia".to_string()));
    }

    #[test]
    fn test_is_deprecated_language() {
        assert!(is_deprecated_language("iw")); // Hebrew (deprecated)
        assert!(!is_deprecated_language("he")); // Hebrew (current)
        assert!(is_deprecated_language("ji")); // Yiddish (deprecated)
    }
}