greentic-setup 0.5.9

End-to-end bundle setup engine for the Greentic platform — pack discovery, QA-driven configuration, secrets persistence, and bundle lifecycle management
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
//! Tenant config synchronization for webchat-gui OAuth settings.
//!
//! After setup persists OAuth answers to secrets, this module updates the
//! static tenant config JSON (`assets/webchat-gui/config/tenants/<tenant>.json`)
//! to enable/disable OAuth providers and set client IDs. This ensures the
//! webchat-gui runtime serves the correct auth config without manual editing.

use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use serde_json::{Map, Value};

use crate::platform_setup::load_effective_static_routes_defaults;

/// Well-known OIDC provider definitions.
struct OidcProviderDef {
    id_suffix: &'static str,
    label: &'static str,
    answer_enable_key: &'static str,
    answer_client_id_key: &'static str,
    authorization_url: &'static str,
    scope: &'static str,
}

const OIDC_PROVIDERS: &[OidcProviderDef] = &[
    OidcProviderDef {
        id_suffix: "google",
        label: "Sign in with Google",
        answer_enable_key: "oauth_enable_google",
        answer_client_id_key: "oauth_google_client_id",
        authorization_url: "https://accounts.google.com/o/oauth2/v2/auth",
        scope: "openid profile email",
    },
    OidcProviderDef {
        id_suffix: "microsoft",
        label: "Sign in with Microsoft",
        answer_enable_key: "oauth_enable_microsoft",
        answer_client_id_key: "oauth_microsoft_client_id",
        authorization_url: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
        scope: "openid profile email",
    },
    OidcProviderDef {
        id_suffix: "github",
        label: "Sign in with GitHub",
        answer_enable_key: "oauth_enable_github",
        answer_client_id_key: "oauth_github_client_id",
        authorization_url: "https://github.com/login/oauth/authorize",
        scope: "read:user user:email",
    },
];

/// Resolve the target tenant config file, scaffolding `<tenant>.json` from
/// `default.json` when it does not yet exist.
///
/// The webchat-gui SPA's runtime-bootstrap fetches `/config/tenants/<tenant>.json`
/// directly via `originalFetch` (bypassing its own 404-fallback interceptor) when
/// resolving skin / nav_links overrides. If we instead patched `default.json`
/// here, those tenant-specific fields would never be picked up at runtime, *and*
/// the template would be polluted with the wrong `tenant_id`. So when the
/// tenant-specific file is missing, we copy `default.json` to `<tenant>.json`
/// (rewriting `tenant_id`) and write into the new file.
///
/// Returns `Ok(None)` when neither `<tenant>.json` nor `default.json` exists —
/// callers should treat that as a no-op.
fn resolve_or_scaffold_tenant_config(bundle_path: &Path, tenant: &str) -> Result<Option<PathBuf>> {
    let tenants_dir = bundle_path.join("assets/webchat-gui/config/tenants");
    let tenant_path = tenants_dir.join(format!("{tenant}.json"));
    if tenant_path.exists() {
        return Ok(Some(tenant_path));
    }

    let default_path = tenants_dir.join("default.json");
    if !default_path.exists() {
        return Ok(None);
    }

    let raw = std::fs::read_to_string(&default_path)
        .with_context(|| format!("read default tenant config {}", default_path.display()))?;
    let mut config: Value = serde_json::from_str(&raw)
        .with_context(|| format!("parse default tenant config {}", default_path.display()))?;
    if let Some(obj) = config.as_object_mut() {
        obj.insert("tenant_id".to_string(), Value::String(tenant.to_string()));
    }
    let output = serde_json::to_string_pretty(&config)?;
    std::fs::write(&tenant_path, output)
        .with_context(|| format!("scaffold tenant config {}", tenant_path.display()))?;
    Ok(Some(tenant_path))
}

/// Synchronize webchat-gui OAuth answers to the tenant config JSON.
///
/// Only runs for `messaging-webchat-gui` providers. Updates the tenant config
/// at `<bundle>/assets/webchat-gui/config/tenants/<tenant>.json`, scaffolding
/// the file from `default.json` when missing.
pub fn sync_oauth_to_tenant_config(
    bundle_path: &Path,
    tenant: &str,
    provider_id: &str,
    answers: &Value,
) -> Result<bool> {
    if !provider_id.contains("webchat-gui") {
        return Ok(false);
    }

    let answers_obj = match answers.as_object() {
        Some(m) => m,
        None => return Ok(false),
    };

    let oauth_enabled = answers_obj
        .get("oauth_enabled")
        .and_then(|v| v.as_bool().or_else(|| v.as_str().map(|s| s == "true")))
        .unwrap_or(false);

    let Some(target) = resolve_or_scaffold_tenant_config(bundle_path, tenant)? else {
        return Ok(false);
    };

    update_tenant_config(
        &target,
        tenant,
        oauth_enabled,
        answers_obj,
        resolve_public_base_url(bundle_path, tenant, answers_obj)?,
    )
}

fn update_tenant_config(
    config_path: &Path,
    tenant: &str,
    oauth_enabled: bool,
    answers: &Map<String, Value>,
    public_base_url: Option<String>,
) -> Result<bool> {
    let raw = std::fs::read_to_string(config_path)
        .with_context(|| format!("read tenant config {}", config_path.display()))?;

    let mut config: Value = serde_json::from_str(&raw).context("parse tenant config as JSON")?;

    let auth = config.as_object_mut().and_then(|m| {
        m.entry("auth")
            .or_insert_with(|| Value::Object(Map::new()))
            .as_object_mut()
    });

    let Some(auth) = auth else {
        return Ok(false);
    };

    let providers = auth
        .entry("providers")
        .or_insert_with(|| Value::Array(vec![]));

    let Some(providers_arr) = providers.as_array_mut() else {
        return Ok(false);
    };

    let mut changed = false;

    for def in OIDC_PROVIDERS {
        let enabled = oauth_enabled
            && answers
                .get(def.answer_enable_key)
                .and_then(|v| v.as_bool().or_else(|| v.as_str().map(|s| s == "true")))
                .unwrap_or(false);

        let client_id = answers
            .get(def.answer_client_id_key)
            .and_then(Value::as_str)
            .unwrap_or("")
            .to_string();

        let provider_id = format!("{tenant}-{}", def.id_suffix);
        let redirect_uri = public_base_url.as_deref().map(|public_base_url| {
            format!(
                "{}/v1/web/webchat/{}/",
                public_base_url.trim_end_matches('/'),
                tenant,
            )
        });

        // Find existing provider entry — match by exact ID or by suffix (e.g. "google", "microsoft")
        if let Some(existing) = providers_arr.iter_mut().find(|p| {
            let id = p.get("id").and_then(Value::as_str).unwrap_or("");
            id == provider_id || id == def.id_suffix
        }) {
            if let Some(obj) = existing.as_object_mut() {
                obj.insert("enabled".to_string(), Value::Bool(enabled));
                if !client_id.is_empty() {
                    obj.insert("clientId".to_string(), Value::String(client_id));
                }
                if let Some(redirect_uri) = redirect_uri.as_ref() {
                    obj.insert(
                        "redirectUri".to_string(),
                        Value::String(redirect_uri.clone()),
                    );
                }
                changed = true;
            }
        } else if enabled {
            // Add new provider entry
            let mut entry = Map::new();
            entry.insert("id".to_string(), Value::String(provider_id));
            entry.insert("label".to_string(), Value::String(def.label.to_string()));
            entry.insert("type".to_string(), Value::String("oidc".to_string()));
            entry.insert("enabled".to_string(), Value::Bool(true));
            entry.insert(
                "authorizationUrl".to_string(),
                Value::String(def.authorization_url.to_string()),
            );
            if !client_id.is_empty() {
                entry.insert("clientId".to_string(), Value::String(client_id));
            }
            if let Some(redirect_uri) = redirect_uri.as_ref() {
                entry.insert(
                    "redirectUri".to_string(),
                    Value::String(redirect_uri.clone()),
                );
            }
            entry.insert("scope".to_string(), Value::String(def.scope.to_string()));
            entry.insert(
                "responseType".to_string(),
                Value::String("code".to_string()),
            );
            providers_arr.push(Value::Object(entry));
            changed = true;
        }
    }

    // Handle custom OIDC provider
    let custom_enabled = oauth_enabled
        && answers
            .get("oauth_enable_custom")
            .and_then(|v| v.as_bool().or_else(|| v.as_str().map(|s| s == "true")))
            .unwrap_or(false);

    if custom_enabled {
        let custom_id = format!("{tenant}-custom-oidc");
        let label = answers
            .get("oauth_custom_label")
            .and_then(Value::as_str)
            .unwrap_or("SSO Login")
            .to_string();
        let auth_url = answers
            .get("oauth_custom_auth_url")
            .and_then(Value::as_str)
            .unwrap_or("")
            .to_string();
        let client_id = answers
            .get("oauth_custom_client_id")
            .and_then(Value::as_str)
            .unwrap_or("")
            .to_string();
        let scopes = answers
            .get("oauth_custom_scopes")
            .and_then(Value::as_str)
            .unwrap_or("openid profile email")
            .to_string();
        let redirect_uri = public_base_url.as_deref().map(|public_base_url| {
            format!(
                "{}/v1/web/webchat/{}/",
                public_base_url.trim_end_matches('/'),
                tenant,
            )
        });

        if let Some(existing) = providers_arr
            .iter_mut()
            .find(|p| p.get("id").and_then(Value::as_str) == Some(&custom_id))
        {
            if let Some(obj) = existing.as_object_mut() {
                obj.insert("enabled".to_string(), Value::Bool(true));
                obj.insert("label".to_string(), Value::String(label));
                if !auth_url.is_empty() {
                    obj.insert("authorizationUrl".to_string(), Value::String(auth_url));
                }
                if !client_id.is_empty() {
                    obj.insert("clientId".to_string(), Value::String(client_id));
                }
                if let Some(redirect_uri) = redirect_uri.as_ref() {
                    obj.insert(
                        "redirectUri".to_string(),
                        Value::String(redirect_uri.clone()),
                    );
                }
                obj.insert("scope".to_string(), Value::String(scopes));
                changed = true;
            }
        } else {
            let mut entry = Map::new();
            entry.insert("id".to_string(), Value::String(custom_id));
            entry.insert("label".to_string(), Value::String(label));
            entry.insert("type".to_string(), Value::String("oidc".to_string()));
            entry.insert("enabled".to_string(), Value::Bool(true));
            if !auth_url.is_empty() {
                entry.insert("authorizationUrl".to_string(), Value::String(auth_url));
            }
            if !client_id.is_empty() {
                entry.insert("clientId".to_string(), Value::String(client_id));
            }
            if let Some(redirect_uri) = redirect_uri.as_ref() {
                entry.insert(
                    "redirectUri".to_string(),
                    Value::String(redirect_uri.clone()),
                );
            }
            entry.insert("scope".to_string(), Value::String(scopes));
            entry.insert(
                "responseType".to_string(),
                Value::String("code".to_string()),
            );
            providers_arr.push(Value::Object(entry));
            changed = true;
        }
    }

    if changed {
        let output = serde_json::to_string_pretty(&config)?;
        std::fs::write(config_path, output)
            .with_context(|| format!("write tenant config {}", config_path.display()))?;
    }

    Ok(changed)
}

fn resolve_public_base_url(
    bundle_path: &Path,
    tenant: &str,
    answers: &Map<String, Value>,
) -> Result<Option<String>> {
    if let Some(value) = answers
        .get("public_base_url")
        .and_then(Value::as_str)
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .filter(|value| !is_placeholder_public_base_url(value))
    {
        return Ok(Some(value.to_string()));
    }

    let from_policy = load_effective_static_routes_defaults(bundle_path, tenant, Some("default"))?
        .and_then(|policy| policy.public_base_url)
        .map(|value| value.trim().to_string())
        .filter(|value| !value.is_empty())
        .filter(|value| !is_placeholder_public_base_url(value));

    Ok(from_policy)
}

/// Sanitize a `nav_links` array (from either the table-wizard answer or a
/// parsed `nav_links_json` string). Drops malformed entries silently —
/// label or url missing/empty, label not a string or locale-keyed object —
/// and normalises the on-disk shape to `{label, url, external?}`.
fn sanitize_nav_link_array(arr: &[Value]) -> Vec<Value> {
    arr.iter()
        .filter_map(|entry| {
            let obj = entry.as_object()?;
            // `label` accepts either a plain string or a locale-keyed JSON
            // object (e.g. `{"en":"Help","id":"Bantuan"}`).
            let label_value = sanitize_i18n_text(obj.get("label")?)?;

            let url = obj.get("url").and_then(Value::as_str).map(str::trim)?;
            if url.is_empty() {
                return None;
            }

            let mut clean = serde_json::Map::new();
            clean.insert("label".to_string(), label_value);
            clean.insert("url".to_string(), Value::String(url.to_string()));
            if obj.get("external").and_then(Value::as_bool) == Some(true) {
                clean.insert("external".to_string(), Value::Bool(true));
            }
            // Optional `num`: short prefix chip (e.g. "M5"). Same i18n
            // resolution as label.
            if let Some(num) = obj.get("num").and_then(sanitize_i18n_text_opt) {
                clean.insert("num".to_string(), num);
            }
            // Tooltip is collected as three flat columns by the wizard
            // (`tooltip_eyebrow`, `tooltip_title`, `tooltip_lede`); rebuild
            // the nested `tooltip: { eyebrow?, title?, lede? }` object here
            // so the runtime SPA's renderTopbarNav sees the canonical shape.
            // Operators who hand-edit tenant.json can also pass `tooltip` as
            // an already-nested object; if present, we sanitise that path
            // instead of the flat columns.
            let nested_tooltip = obj.get("tooltip").and_then(|v| v.as_object());
            let tooltip_obj = if let Some(map) = nested_tooltip {
                build_tooltip_obj(map.get("eyebrow"), map.get("title"), map.get("lede"))
            } else {
                build_tooltip_obj(
                    obj.get("tooltip_eyebrow"),
                    obj.get("tooltip_title"),
                    obj.get("tooltip_lede"),
                )
            };
            if let Some(t) = tooltip_obj {
                clean.insert("tooltip".to_string(), t);
            }
            Some(Value::Object(clean))
        })
        .collect()
}

/// Sanitize an i18n-aware text value: accepts either a plain string or a
/// locale-keyed object whose values are strings. Returns `None` when the
/// input is missing, the wrong shape, or has only empty values.
fn sanitize_i18n_text(value: &Value) -> Option<Value> {
    match value {
        Value::String(s) => {
            let trimmed = s.trim();
            if trimmed.is_empty() {
                None
            } else {
                Some(Value::String(trimmed.to_string()))
            }
        }
        Value::Object(map) => {
            let mut clean = serde_json::Map::new();
            for (locale, v) in map {
                if let Some(s) = v.as_str() {
                    let trimmed = s.trim();
                    if !trimmed.is_empty() {
                        clean.insert(locale.clone(), Value::String(trimmed.to_string()));
                    }
                }
            }
            if clean.is_empty() {
                None
            } else {
                Some(Value::Object(clean))
            }
        }
        _ => None,
    }
}

/// Convenience: take an `Option<&Value>` and return `Option<Value>`.
fn sanitize_i18n_text_opt(value: &Value) -> Option<Value> {
    sanitize_i18n_text(value)
}

/// Build a tooltip object from optional eyebrow/title/lede inputs. Returns
/// `None` if all three are empty (no tooltip → omit the field).
fn build_tooltip_obj(
    eyebrow: Option<&Value>,
    title: Option<&Value>,
    lede: Option<&Value>,
) -> Option<Value> {
    let mut clean = serde_json::Map::new();
    if let Some(v) = eyebrow.and_then(sanitize_i18n_text_opt) {
        clean.insert("eyebrow".to_string(), v);
    }
    if let Some(v) = title.and_then(sanitize_i18n_text_opt) {
        clean.insert("title".to_string(), v);
    }
    if let Some(v) = lede.and_then(sanitize_i18n_text_opt) {
        clean.insert("lede".to_string(), v);
    }
    if clean.is_empty() {
        None
    } else {
        Some(Value::Object(clean))
    }
}

fn is_placeholder_public_base_url(value: &str) -> bool {
    let normalized = value.trim().trim_end_matches('/').to_ascii_lowercase();
    normalized.is_empty()
        || normalized.contains("example.com")
        || normalized.contains("localhost")
        || normalized.contains("127.0.0.1")
}

/// Synchronize webchat-gui `skin` answer to the tenant config JSON.
///
/// Only runs for `messaging-webchat-gui` providers. When the operator picks a
/// non-empty `skin` value in setup (e.g. `3aigent`), this writes that value to
/// the `skin` field in `<bundle>/assets/webchat-gui/config/tenants/<tenant>.json`,
/// falling back to `default.json` if a tenant-specific file does not exist.
///
/// The webchat-gui SPA's runtime-bootstrap reads this field and, when present,
/// overrides URL-path skin loading to load `/skins/<skin>/skin.json` instead.
pub fn sync_skin_to_tenant_config(
    bundle_path: &Path,
    tenant: &str,
    provider_id: &str,
    answers: &Value,
) -> Result<bool> {
    if !provider_id.contains("webchat-gui") {
        return Ok(false);
    }

    let skin = answers
        .as_object()
        .and_then(|m| m.get("skin"))
        .and_then(Value::as_str)
        .map(str::trim)
        .filter(|s| !s.is_empty());

    let Some(skin) = skin else {
        return Ok(false);
    };

    let Some(target) = resolve_or_scaffold_tenant_config(bundle_path, tenant)? else {
        return Ok(false);
    };

    let raw = std::fs::read_to_string(&target)
        .with_context(|| format!("read tenant config {}", target.display()))?;
    let mut config: Value = serde_json::from_str(&raw)
        .with_context(|| format!("parse tenant config {}", target.display()))?;

    let obj = match config.as_object_mut() {
        Some(o) => o,
        None => return Ok(false),
    };

    let already_set = obj
        .get("skin")
        .and_then(Value::as_str)
        .map(|existing| existing == skin)
        .unwrap_or(false);
    if already_set {
        return Ok(false);
    }

    obj.insert("skin".to_string(), Value::String(skin.to_string()));
    let output = serde_json::to_string_pretty(&config)?;
    std::fs::write(&target, output)
        .with_context(|| format!("write tenant config {}", target.display()))?;
    Ok(true)
}

/// Sync the webchat-gui setup answer `nav_links_json` into the tenant config's
/// `nav_links` array.
///
/// Operators enter the array as a JSON string in the wizard
/// (e.g. `[{"label":"Module 5","url":"https://...","external":true}]`). This
/// function parses that string, validates each entry has non-empty `label` and
/// `url` strings (skipping malformed entries instead of failing), and writes
/// the resulting array to `<bundle>/assets/webchat-gui/config/tenants/<tenant>.json`.
///
/// An empty answer (or one that parses to an empty array) clears any existing
/// `nav_links` so removing all entries via the wizard hides the topbar nav at
/// runtime.
///
/// The webchat-gui SPA's runtime-bootstrap reads this array and renders one
/// anchor per entry between the brand block and the locale picker.
/// Read the persisted `nav_links` array from `<bundle>/assets/webchat-gui/
/// config/tenants/<tenant>.json` so the setup wizard can hydrate the table
/// on a re-run. Falls back to `default.json` when no tenant-specific file
/// exists. Returns `None` when neither file exists or `nav_links` is absent.
pub fn read_existing_nav_links(bundle_path: &Path, tenant: &str) -> Option<Vec<Value>> {
    let tenants_dir = bundle_path.join("assets/webchat-gui/config/tenants");
    let candidates = [
        tenants_dir.join(format!("{tenant}.json")),
        tenants_dir.join("default.json"),
    ];
    for path in &candidates {
        let raw = match std::fs::read_to_string(path) {
            Ok(s) => s,
            Err(_) => continue,
        };
        let value: Value = match serde_json::from_str(&raw) {
            Ok(v) => v,
            Err(_) => continue,
        };
        if let Some(arr) = value
            .get("nav_links")
            .and_then(Value::as_array)
            .filter(|a| !a.is_empty())
        {
            // Flatten nested `tooltip` object into the flat
            // `tooltip_eyebrow`/`tooltip_title`/`tooltip_lede` columns the
            // wizard renders. The on-disk shape matches the runtime SPA's
            // expectations; the wizard's column shape does not, so we
            // translate here on hydration (and back to nested on write in
            // sanitize_nav_link_array).
            let flattened: Vec<Value> = arr
                .iter()
                .map(|entry| {
                    let mut out = entry.as_object().cloned().unwrap_or_default();
                    if let Some(tooltip) =
                        out.remove("tooltip").and_then(|t| t.as_object().cloned())
                    {
                        for (sub_key, target_key) in [
                            ("eyebrow", "tooltip_eyebrow"),
                            ("title", "tooltip_title"),
                            ("lede", "tooltip_lede"),
                        ] {
                            if let Some(v) = tooltip.get(sub_key) {
                                out.insert(target_key.to_string(), v.clone());
                            }
                        }
                    }
                    Value::Object(out)
                })
                .collect();
            return Some(flattened);
        }
    }
    None
}

pub fn sync_nav_links_to_tenant_config(
    bundle_path: &Path,
    tenant: &str,
    provider_id: &str,
    answers: &Value,
) -> Result<bool> {
    if !provider_id.contains("webchat-gui") {
        return Ok(false);
    }

    let answers_obj = match answers.as_object() {
        Some(m) => m,
        None => return Ok(false),
    };

    // Three answer shapes are accepted, in priority order:
    //
    // 1. `nav_links` as a native array — produced by the new
    //    `kind: table` wizard. Each row is already a JSON object with
    //    `label`/`url`/`external` keys; we just sanitise.
    // 2. `nav_links_json` as a JSON string — legacy advanced-input answer
    //    that pre-dates the table wizard. Parsed, then sanitised.
    // 3. Neither present — leave the existing tenant config untouched.
    let parsed_links: Vec<Value> =
        if let Some(arr) = answers_obj.get("nav_links").and_then(Value::as_array) {
            sanitize_nav_link_array(arr)
        } else if let Some(raw) = answers_obj
            .get("nav_links_json")
            .and_then(Value::as_str)
            .map(str::trim)
        {
            if raw.is_empty() {
                Vec::new()
            } else {
                let parsed: Value = serde_json::from_str(raw).with_context(|| {
                    format!("parse nav_links_json answer (expected JSON array): {raw}")
                })?;
                let Some(arr) = parsed.as_array() else {
                    anyhow::bail!("nav_links_json must be a JSON array, got: {raw}");
                };
                sanitize_nav_link_array(arr)
            }
        } else {
            return Ok(false);
        };

    let Some(target) = resolve_or_scaffold_tenant_config(bundle_path, tenant)? else {
        return Ok(false);
    };

    let raw_config = std::fs::read_to_string(&target)
        .with_context(|| format!("read tenant config {}", target.display()))?;
    let mut config: Value = serde_json::from_str(&raw_config)
        .with_context(|| format!("parse tenant config {}", target.display()))?;

    let obj = match config.as_object_mut() {
        Some(o) => o,
        None => return Ok(false),
    };

    let next = Value::Array(parsed_links);
    if obj.get("nav_links") == Some(&next) {
        return Ok(false);
    }

    obj.insert("nav_links".to_string(), next);

    let output = serde_json::to_string_pretty(&config)?;
    std::fs::write(&target, output)
        .with_context(|| format!("write tenant config {}", target.display()))?;
    Ok(true)
}

#[cfg(test)]
mod tests {
    use super::{
        is_placeholder_public_base_url, resolve_or_scaffold_tenant_config, resolve_public_base_url,
        sync_nav_links_to_tenant_config, sync_oauth_to_tenant_config, sync_skin_to_tenant_config,
        update_tenant_config,
    };
    use serde_json::{Map, Value, json};

    #[test]
    fn resolve_public_base_url_ignores_placeholder_answer() {
        let temp = tempfile::tempdir().unwrap();
        let answers = json!({
            "public_base_url": "https://example.com"
        });
        let resolved =
            resolve_public_base_url(temp.path(), "demo", answers.as_object().unwrap()).unwrap();
        assert!(resolved.is_none());
    }

    #[test]
    fn resolve_public_base_url_prefers_non_placeholder_answer() {
        let temp = tempfile::tempdir().unwrap();
        let answers = json!({
            "public_base_url": "https://demo.example.net"
        });
        let resolved =
            resolve_public_base_url(temp.path(), "demo", answers.as_object().unwrap()).unwrap();
        assert_eq!(resolved.as_deref(), Some("https://demo.example.net"));
    }

    #[test]
    fn update_tenant_config_preserves_existing_redirect_when_public_base_url_missing() {
        let temp = tempfile::tempdir().unwrap();
        let config_path = temp.path().join("demo.json");
        std::fs::write(
            &config_path,
            serde_json::to_string_pretty(&json!({
                "auth": {
                    "providers": [
                        {
                            "id": "demo-google",
                            "enabled": true,
                            "redirectUri": "https://existing.example.net/v1/web/webchat/demo/"
                        }
                    ]
                }
            }))
            .unwrap(),
        )
        .unwrap();

        let mut answers = Map::new();
        answers.insert("oauth_enabled".into(), Value::Bool(true));
        answers.insert("oauth_enable_google".into(), Value::Bool(true));
        answers.insert(
            "oauth_google_client_id".into(),
            Value::String("client-id".into()),
        );

        update_tenant_config(&config_path, "demo", true, &answers, None).unwrap();

        let updated: Value =
            serde_json::from_str(&std::fs::read_to_string(&config_path).unwrap()).unwrap();
        assert_eq!(
            updated["auth"]["providers"][0]["redirectUri"].as_str(),
            Some("https://existing.example.net/v1/web/webchat/demo/")
        );
    }

    #[test]
    fn placeholder_detection_catches_local_and_example_urls() {
        assert!(is_placeholder_public_base_url("https://example.com"));
        assert!(is_placeholder_public_base_url("http://localhost:8080"));
        assert!(is_placeholder_public_base_url("http://127.0.0.1:8080"));
        assert!(!is_placeholder_public_base_url("https://demo.example.net"));
    }

    #[test]
    fn sync_skin_skips_non_webchat_provider() {
        let temp = tempfile::tempdir().unwrap();
        let answers = json!({ "skin": "3aigent" });
        let changed =
            sync_skin_to_tenant_config(temp.path(), "demo", "messaging-slack", &answers).unwrap();
        assert!(!changed);
    }

    #[test]
    fn sync_skin_skips_when_answer_missing_or_empty() {
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();
        std::fs::write(tenants_dir.join("demo.json"), r#"{"tenant_id":"demo"}"#).unwrap();

        for empty in [json!({}), json!({"skin": ""}), json!({"skin": "   "})] {
            let changed =
                sync_skin_to_tenant_config(temp.path(), "demo", "messaging-webchat-gui", &empty)
                    .unwrap();
            assert!(!changed, "should not write for {empty}");
        }
    }

    #[test]
    fn sync_skin_writes_field_to_tenant_json() {
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();
        let tenant_file = tenants_dir.join("demo.json");
        std::fs::write(
            &tenant_file,
            r#"{"tenant_id":"demo","legacy_skin":"_template"}"#,
        )
        .unwrap();

        let answers = json!({ "skin": "3aigent" });
        let changed =
            sync_skin_to_tenant_config(temp.path(), "demo", "messaging-webchat-gui", &answers)
                .unwrap();
        assert!(changed);

        let updated: Value =
            serde_json::from_str(&std::fs::read_to_string(&tenant_file).unwrap()).unwrap();
        assert_eq!(updated["skin"].as_str(), Some("3aigent"));
        // legacy_skin must be preserved (separate concern)
        assert_eq!(updated["legacy_skin"].as_str(), Some("_template"));
    }

    #[test]
    fn sync_skin_scaffolds_tenant_json_from_default_when_missing() {
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();
        let default_file = tenants_dir.join("default.json");
        std::fs::write(
            &default_file,
            r#"{"tenant_id":"default","legacy_skin":"_template"}"#,
        )
        .unwrap();

        let answers = json!({ "skin": "3aigent" });
        let changed =
            sync_skin_to_tenant_config(temp.path(), "demo", "messaging-webchat-gui", &answers)
                .unwrap();
        assert!(changed);

        // demo.json was scaffolded with skin field and remapped tenant_id
        let demo_file = tenants_dir.join("demo.json");
        assert!(demo_file.exists(), "demo.json must be scaffolded");
        let demo_parsed: Value =
            serde_json::from_str(&std::fs::read_to_string(&demo_file).unwrap()).unwrap();
        assert_eq!(demo_parsed["tenant_id"].as_str(), Some("demo"));
        assert_eq!(demo_parsed["skin"].as_str(), Some("3aigent"));
        assert_eq!(demo_parsed["legacy_skin"].as_str(), Some("_template"));

        // default.json must be left untouched — runtime depends on it as a template
        let default_after: Value =
            serde_json::from_str(&std::fs::read_to_string(&default_file).unwrap()).unwrap();
        assert_eq!(default_after["tenant_id"].as_str(), Some("default"));
        assert!(default_after.get("skin").is_none());
    }

    #[test]
    fn sync_skin_returns_false_when_no_tenant_or_default_config_exists() {
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();

        let answers = json!({ "skin": "3aigent" });
        let changed =
            sync_skin_to_tenant_config(temp.path(), "demo", "messaging-webchat-gui", &answers)
                .unwrap();
        assert!(!changed);
    }

    #[test]
    fn sync_skin_is_idempotent() {
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();
        let tenant_file = tenants_dir.join("demo.json");
        std::fs::write(&tenant_file, r#"{"tenant_id":"demo","skin":"3aigent"}"#).unwrap();

        let answers = json!({ "skin": "3aigent" });
        let changed =
            sync_skin_to_tenant_config(temp.path(), "demo", "messaging-webchat-gui", &answers)
                .unwrap();
        assert!(!changed, "no-op when value already matches");
    }

    #[test]
    fn sync_nav_links_skips_non_webchat_provider() {
        let temp = tempfile::tempdir().unwrap();
        let answers = json!({ "nav_links_json": r#"[{"label":"X","url":"/x"}]"# });
        let changed =
            sync_nav_links_to_tenant_config(temp.path(), "demo", "messaging-slack", &answers)
                .unwrap();
        assert!(!changed);
    }

    #[test]
    fn sync_nav_links_skips_when_answer_absent() {
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();
        std::fs::write(tenants_dir.join("demo.json"), r#"{"tenant_id":"demo"}"#).unwrap();

        let answers = json!({});
        let changed =
            sync_nav_links_to_tenant_config(temp.path(), "demo", "messaging-webchat-gui", &answers)
                .unwrap();
        assert!(!changed, "absent answer leaves config alone");
    }

    #[test]
    fn sync_nav_links_accepts_native_array_from_table_wizard() {
        // The new `kind: table` wizard writes the answer as a native JSON
        // array (not a JSON-string-as-array) under the `nav_links` key.
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();
        let tenant_file = tenants_dir.join("demo.json");
        std::fs::write(&tenant_file, r#"{"tenant_id":"demo"}"#).unwrap();

        let answers = json!({
            "nav_links": [
                { "label": "Help", "url": "/help", "external": false },
                { "label": "Docs", "url": "https://docs.example", "external": true },
                // Whitespace-only label dropped silently.
                { "label": "  ", "url": "/skipped" }
            ]
        });
        let changed =
            sync_nav_links_to_tenant_config(temp.path(), "demo", "messaging-webchat-gui", &answers)
                .unwrap();
        assert!(changed);

        let updated: Value =
            serde_json::from_str(&std::fs::read_to_string(&tenant_file).unwrap()).unwrap();
        let links = updated["nav_links"].as_array().expect("nav_links array");
        assert_eq!(links.len(), 2, "third row dropped (label whitespace)");
        assert_eq!(links[0]["label"].as_str(), Some("Help"));
        assert_eq!(links[0]["url"].as_str(), Some("/help"));
        assert!(links[0].get("external").is_none(), "external=false omitted");
        assert_eq!(links[1]["label"].as_str(), Some("Docs"));
        assert_eq!(links[1]["external"].as_bool(), Some(true));
    }

    #[test]
    fn sync_nav_links_writes_array_to_tenant_json() {
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();
        let tenant_file = tenants_dir.join("demo.json");
        std::fs::write(&tenant_file, r#"{"tenant_id":"demo"}"#).unwrap();

        let answers = json!({
            "nav_links_json": r#"[
                { "label": "Module 5", "url": "https://example.com/m5", "external": true },
                { "label": "Help",     "url": "/help" }
            ]"#
        });
        let changed =
            sync_nav_links_to_tenant_config(temp.path(), "demo", "messaging-webchat-gui", &answers)
                .unwrap();
        assert!(changed);

        let updated: Value =
            serde_json::from_str(&std::fs::read_to_string(&tenant_file).unwrap()).unwrap();
        let links = updated["nav_links"].as_array().expect("nav_links array");
        assert_eq!(links.len(), 2);
        assert_eq!(links[0]["label"].as_str(), Some("Module 5"));
        assert_eq!(links[0]["url"].as_str(), Some("https://example.com/m5"));
        assert_eq!(links[0]["external"].as_bool(), Some(true));
        assert_eq!(links[1]["label"].as_str(), Some("Help"));
        assert_eq!(links[1]["url"].as_str(), Some("/help"));
        // external defaults to absent (not false) when omitted
        assert!(links[1].get("external").is_none());
    }

    #[test]
    fn sync_nav_links_drops_malformed_entries() {
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();
        let tenant_file = tenants_dir.join("demo.json");
        std::fs::write(&tenant_file, r#"{"tenant_id":"demo"}"#).unwrap();

        // Mix of valid + bad entries: missing url, empty label, non-object.
        let answers = json!({
            "nav_links_json": r#"[
                { "label": "Good", "url": "/ok" },
                { "label": "No URL" },
                { "label": "", "url": "/blank" },
                "not an object",
                { "label": "Also good", "url": "https://x" }
            ]"#
        });
        let changed =
            sync_nav_links_to_tenant_config(temp.path(), "demo", "messaging-webchat-gui", &answers)
                .unwrap();
        assert!(changed);

        let updated: Value =
            serde_json::from_str(&std::fs::read_to_string(&tenant_file).unwrap()).unwrap();
        let links = updated["nav_links"].as_array().unwrap();
        assert_eq!(links.len(), 2);
        assert_eq!(links[0]["label"].as_str(), Some("Good"));
        assert_eq!(links[1]["label"].as_str(), Some("Also good"));
    }

    #[test]
    fn sync_nav_links_clears_existing_when_answer_is_empty_array() {
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();
        let tenant_file = tenants_dir.join("demo.json");
        std::fs::write(
            &tenant_file,
            r#"{"tenant_id":"demo","nav_links":[{"label":"Old","url":"/old"}]}"#,
        )
        .unwrap();

        let answers = json!({ "nav_links_json": "[]" });
        let changed =
            sync_nav_links_to_tenant_config(temp.path(), "demo", "messaging-webchat-gui", &answers)
                .unwrap();
        assert!(changed);

        let updated: Value =
            serde_json::from_str(&std::fs::read_to_string(&tenant_file).unwrap()).unwrap();
        assert!(updated["nav_links"].as_array().unwrap().is_empty());
    }

    #[test]
    fn sync_nav_links_is_idempotent() {
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();
        let tenant_file = tenants_dir.join("demo.json");
        std::fs::write(
            &tenant_file,
            r#"{"tenant_id":"demo","nav_links":[{"label":"X","url":"/x"}]}"#,
        )
        .unwrap();

        let answers = json!({ "nav_links_json": r#"[{"label":"X","url":"/x"}]"# });
        let changed =
            sync_nav_links_to_tenant_config(temp.path(), "demo", "messaging-webchat-gui", &answers)
                .unwrap();
        assert!(!changed, "no-op when content already matches");
    }

    #[test]
    fn sync_nav_links_returns_error_on_invalid_json() {
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();
        std::fs::write(tenants_dir.join("demo.json"), r#"{"tenant_id":"demo"}"#).unwrap();

        let answers = json!({ "nav_links_json": "not valid json" });
        let err =
            sync_nav_links_to_tenant_config(temp.path(), "demo", "messaging-webchat-gui", &answers)
                .unwrap_err();
        assert!(err.to_string().contains("parse nav_links_json"));
    }

    #[test]
    fn sync_nav_links_scaffolds_tenant_json_from_default_when_missing() {
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();
        let default_file = tenants_dir.join("default.json");
        std::fs::write(&default_file, r#"{"tenant_id":"default"}"#).unwrap();

        let answers = json!({ "nav_links_json": r#"[{"label":"Help","url":"/help"}]"# });
        let changed =
            sync_nav_links_to_tenant_config(temp.path(), "demo", "messaging-webchat-gui", &answers)
                .unwrap();
        assert!(changed);

        let demo_file = tenants_dir.join("demo.json");
        assert!(demo_file.exists());
        let demo_parsed: Value =
            serde_json::from_str(&std::fs::read_to_string(&demo_file).unwrap()).unwrap();
        assert_eq!(demo_parsed["tenant_id"].as_str(), Some("demo"));
        assert_eq!(demo_parsed["nav_links"][0]["label"].as_str(), Some("Help"));

        let default_parsed: Value =
            serde_json::from_str(&std::fs::read_to_string(&default_file).unwrap()).unwrap();
        assert!(default_parsed.get("nav_links").is_none());
    }

    #[test]
    fn sync_oauth_scaffolds_tenant_json_from_default_when_missing() {
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();
        let default_file = tenants_dir.join("default.json");
        std::fs::write(
            &default_file,
            serde_json::to_string_pretty(&json!({
                "tenant_id": "default",
                "auth": { "providers": [] }
            }))
            .unwrap(),
        )
        .unwrap();

        let answers = json!({
            "oauth_enabled": true,
            "oauth_enable_google": true,
            "oauth_google_client_id": "client-xyz"
        });
        let changed =
            sync_oauth_to_tenant_config(temp.path(), "demo", "messaging-webchat-gui", &answers)
                .unwrap();
        assert!(changed);

        let demo_file = tenants_dir.join("demo.json");
        assert!(demo_file.exists());
        let demo_parsed: Value =
            serde_json::from_str(&std::fs::read_to_string(&demo_file).unwrap()).unwrap();
        assert_eq!(demo_parsed["tenant_id"].as_str(), Some("demo"));
        let provider = demo_parsed["auth"]["providers"]
            .as_array()
            .and_then(|arr| arr.iter().find(|p| p["id"] == "demo-google"))
            .expect("demo-google provider was added");
        assert_eq!(provider["enabled"].as_bool(), Some(true));
        assert_eq!(provider["clientId"].as_str(), Some("client-xyz"));

        // default.json must not be polluted
        let default_parsed: Value =
            serde_json::from_str(&std::fs::read_to_string(&default_file).unwrap()).unwrap();
        let default_providers = default_parsed["auth"]["providers"].as_array().unwrap();
        assert!(default_providers.is_empty(), "default.json must stay empty");
    }

    #[test]
    fn resolve_or_scaffold_returns_existing_tenant_file_unchanged() {
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();
        let tenant_file = tenants_dir.join("demo.json");
        std::fs::write(&tenant_file, r#"{"tenant_id":"demo","skin":"existing"}"#).unwrap();

        let resolved = resolve_or_scaffold_tenant_config(temp.path(), "demo")
            .unwrap()
            .unwrap();
        assert_eq!(resolved, tenant_file);

        // File contents must be untouched
        assert_eq!(
            std::fs::read_to_string(&tenant_file).unwrap(),
            r#"{"tenant_id":"demo","skin":"existing"}"#
        );
    }

    #[test]
    fn resolve_or_scaffold_returns_none_when_neither_exists() {
        let temp = tempfile::tempdir().unwrap();
        let resolved = resolve_or_scaffold_tenant_config(temp.path(), "demo").unwrap();
        assert!(resolved.is_none());
    }

    #[test]
    fn resolve_or_scaffold_for_default_tenant_returns_default_json() {
        let temp = tempfile::tempdir().unwrap();
        let tenants_dir = temp.path().join("assets/webchat-gui/config/tenants");
        std::fs::create_dir_all(&tenants_dir).unwrap();
        let default_file = tenants_dir.join("default.json");
        std::fs::write(&default_file, r#"{"tenant_id":"default"}"#).unwrap();

        let resolved = resolve_or_scaffold_tenant_config(temp.path(), "default")
            .unwrap()
            .unwrap();
        assert_eq!(resolved, default_file);
    }
}