KiThe 0.3.18

A numerical suite for chemical kinetics and thermodynamics, combustion, heat and mass transfer,chemical equilibrium, chemical engeneering
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
//! Column Manager Module
//!
//! This module provides a table-based UI for managing experiment columns.
//!
//! ## Architecture
//! - `ColumnManagerState`: Maintains persistent cache of column samples and UI state
//! - Checkboxes track which columns are selected for operations
//! - Operations: drop (multiple) and rename (single only)
//!
//! ## Data Flow
//! 1. `update_cache()` fetches new samples and merges into existing cache
//! 2. Old column data persists until explicitly updated
//! 3. User selects columns via checkboxes
//! 4. Buttons trigger operations on selected columns

use crate::Kinetics::experimental_kinetics::experiment_series_main::{
    ExperimentThermalMode, TGASeries,
};
use crate::Kinetics::experimental_kinetics::one_experiment_dataset::PreviewRows;
use crate::gui::experimental_kinetics_gui::controller_methods::KineticMethodsWindowState;
use crate::gui::experimental_kinetics_gui::model::PlotModel;
use egui_extras::{Column, TableBuilder};
use log::info;
use std::collections::{HashMap, HashSet};

fn format_sample_value(value: f64) -> String {
    if value.is_finite() && value != 0.0 && (value.abs() < 0.001 || value.abs() >= 1_000_000.0) {
        format!("{value:.4e}")
    } else {
        format!("{value:.4}")
    }
}

fn display_experiment_id(id: &str) -> &str {
    if id.is_empty() { "(unnamed)" } else { id }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum MessageKind {
    Info,
    Success,
    Warning,
    Error,
}

#[derive(Clone, Debug)]
struct SystemMessage {
    kind: MessageKind,
    text: String,
}

impl SystemMessage {
    fn new(kind: MessageKind, text: impl Into<String>) -> Self {
        Self {
            kind,
            text: text.into(),
        }
    }

    fn accessible_text(&self) -> String {
        let level = match self.kind {
            MessageKind::Info => "Info",
            MessageKind::Success => "Success",
            MessageKind::Warning => "Warning",
            MessageKind::Error => "Error",
        };
        format!("{level}: {}", self.text)
    }
}

#[cfg(test)]
mod sample_format_tests {
    use super::{MessageKind, SystemMessage, format_sample_value};

    #[test]
    fn small_nonzero_values_remain_visible() {
        assert_eq!(format_sample_value(0.000_012_3), "1.2300e-5");
        assert_eq!(format_sample_value(-0.000_012_3), "-1.2300e-5");
    }

    #[test]
    fn ordinary_values_keep_compact_decimal_format() {
        assert_eq!(format_sample_value(12.345_67), "12.3457");
        assert_eq!(format_sample_value(0.0), "0.0000");
    }

    #[test]
    fn system_messages_have_distinct_severity() {
        assert_eq!(
            SystemMessage::new(MessageKind::Success, "Column renamed").accessible_text(),
            "Success: Column renamed"
        );
        assert_eq!(
            SystemMessage::new(MessageKind::Warning, "Time already uses hours").kind,
            MessageKind::Warning
        );
        assert_eq!(
            SystemMessage::new(MessageKind::Error, "Could not rename").kind,
            MessageKind::Error
        );
        assert_eq!(
            SystemMessage::new(MessageKind::Info, "Ready").accessible_text(),
            "Info: Ready"
        );
    }

    #[test]
    fn column_manager_status_is_published_to_the_main_message_label() {
        let mut state = super::ColumnManagerState::default();
        let mut model = crate::gui::experimental_kinetics_gui::model::PlotModel::new();
        state.set_message(MessageKind::Error, "Could not drop column: busy");

        state.publish_message_to_main_status(&mut model);

        assert_eq!(model.message, "Error: Could not drop column: busy");
        state.set_message(MessageKind::Success, "Column dropped");
        state.publish_message_to_main_status(&mut model);
        assert_eq!(model.message, "Success: Column dropped");

        model.message = "Saved another file".to_string();
        state.set_message(MessageKind::Error, "Could not drop column: busy");
        state.publish_message_to_main_status(&mut model);
        assert_eq!(model.message, "Error: Could not drop column: busy");

        state.set_message_if_changed(MessageKind::Error, "Column sample refresh failed");
        let revision = state.message_revision;
        state.set_message_if_changed(MessageKind::Error, "Column sample refresh failed");
        assert_eq!(state.message_revision, revision);
    }
}
/// State for Column Manager window
/// Stores cached column samples and tracks user selections
#[derive(Default)]
pub struct ColumnManagerState {
    /// Persistent cache: experiment_id -> column_name -> sample_data
    /// Updated incrementally; old columns remain until replaced
    cached_samples: HashMap<String, PreviewRows>,
    /// Tracks checked columns: (experiment_id, column_name) -> checked
    column_checkboxes: HashMap<(String, String), bool>,
    /// Input field for new column name (used in rename operation)
    new_column_name: String,
    /// System message label for errors/status
    system_message: Option<SystemMessage>,
    message_revision: u64,
    last_published_revision: u64,
    /// Metadata editor window state
    metadata_window_open: bool,
    selected_experiment: Option<String>,
    table_experiment: Option<String>,
    compare_mode: bool,
    compare_experiments: HashSet<String>,
    selected_column: Option<(String, String)>,
    comment_input: String,
    heating_rate_input: String,
    temperature_input: String,
    /// Confirmation dialog state for experiment dropping
    confirm_drop_experiment: Option<String>,
    confirm_drop_columns: Vec<(String, String)>,
    details_tab: DetailsTab,
}

#[derive(Default, Clone, Copy, PartialEq, Eq)]
enum DetailsTab {
    #[default]
    Overview,
    Processing,
    Quality,
    History,
}

impl ColumnManagerState {
    fn set_message(&mut self, kind: MessageKind, text: impl Into<String>) {
        self.system_message = Some(SystemMessage::new(kind, text));
        self.message_revision = self.message_revision.wrapping_add(1);
    }

    fn set_message_if_changed(&mut self, kind: MessageKind, text: impl Into<String>) {
        let text = text.into();
        if self
            .system_message
            .as_ref()
            .is_some_and(|current| current.kind == kind && current.text == text)
        {
            return;
        }
        self.set_message(kind, text);
    }

    fn publish_message_to_main_status(&mut self, model: &mut PlotModel) {
        if self.message_revision == self.last_published_revision {
            return;
        }
        let Some(message) = &self.system_message else {
            return;
        };
        let text = message.accessible_text();
        model.message = text;
        self.last_published_revision = self.message_revision;
    }

    pub(crate) fn close_window_on_layer(&mut self, layer: Option<egui::LayerId>) -> bool {
        let Some(layer) = layer else { return false };
        if layer.id == egui::Id::unique(Some("Confirm Drop Experiment"))
            && self.confirm_drop_experiment.is_some()
        {
            self.confirm_drop_experiment = None;
            return true;
        }
        if layer.id == egui::Id::unique(Some("Confirm Drop Columns"))
            && !self.confirm_drop_columns.is_empty()
        {
            self.confirm_drop_columns.clear();
            return true;
        }
        if layer.id == egui::Id::unique(Some("Edit Metadata")) && self.metadata_window_open {
            self.metadata_window_open = false;
            return true;
        }
        false
    }

    pub fn new() -> Self {
        Self::default()
    }

    /// Updates cache with new samples from model
    /// Merges new data into existing cache, preserving old columns
    fn update_cache(&mut self, model: &mut PlotModel) -> Result<(), String> {
        let new_samples = model
            .column_samples_for_all_experiments(50)
            .map_err(|e| format!("{:?}", e))?;
        info!("new samples for experiment {:?}", &new_samples.keys());
        for (exp_id, preview) in &new_samples {
            info!("for experiment {} created columns", exp_id);
            for col in preview.columns.keys() {
                info!("{:?}", col);
            }
        }
        for (exp_id, preview) in new_samples {
            self.cached_samples.insert(exp_id, preview);
        }

        let experiments = model.list_of_experiments();
        self.cached_samples
            .retain(|exp_id, _| experiments.contains(exp_id));

        for exp_id in experiments {
            let columns = model
                .list_of_columns(&exp_id)
                .map_err(|e| format!("{:?}", e))?;
            if let Some(preview) = self.cached_samples.get_mut(&exp_id) {
                preview.columns.retain(|name, _| columns.contains(name));
            }
        }
        Ok(())
    }

    /// Renders control buttons and input field
    fn render_controls(&mut self, ui: &mut egui::Ui, model: &mut PlotModel) {
        let experiments = model.list_of_experiments();
        if self
            .table_experiment
            .as_ref()
            .is_none_or(|id| !experiments.contains(id))
        {
            self.table_experiment = experiments.first().cloned();
        }
        self.compare_experiments
            .retain(|id| experiments.contains(id));
        ui.horizontal(|ui| {
            ui.checkbox(&mut self.compare_mode, "Compare experiments")
                .on_hover_text("Read-only view of equally sized samples from selected experiments. Source grids are not interpolated.");
            if !self.compare_mode {
                egui::ComboBox::from_label("Experiment")
                    .selected_text(
                        self.table_experiment
                            .as_deref()
                            .unwrap_or("Select experiment"),
                    )
                    .show_ui(ui, |ui| {
                        for id in &experiments {
                            ui.selectable_value(&mut self.table_experiment, Some(id.clone()), id);
                        }
                    });
            }
        });
        if self.compare_mode {
            egui::CollapsingHeader::new("Experiments to compare")
                .default_open(true)
                .show(ui, |ui| {
                    ui.horizontal_wrapped(|ui| {
                        for id in &experiments {
                            let mut selected = self.compare_experiments.contains(id);
                            let label = if id.is_empty() {
                                "(unnamed experiment)"
                            } else {
                                id
                            };
                            if ui.checkbox(&mut selected, label).changed() {
                                if selected {
                                    self.compare_experiments.insert(id.clone());
                                } else {
                                    self.compare_experiments.remove(id);
                                }
                            }
                        }
                    });
                });
        }
        let mutation_enabled = !self.compare_mode && self.table_experiment.is_some();
        ui.horizontal(|ui| {
            let selected_count = self.selected_column_checkboxes().len();
            if ui
                .add_enabled(
                    mutation_enabled && selected_count > 0,
                    egui::Button::new(format!("Delete columns ({selected_count})")),
                )
                .on_hover_text(
                    "Deletes the checked columns from the current experiment after confirmation.",
                )
                .clicked()
            {
                self.handle_drop_columns();
            }
            if let Some(id) = self.table_experiment.clone() {
                if ui
                    .add_enabled(
                        mutation_enabled,
                        egui::Button::new(format!(
                            "Delete experiment: {}",
                            display_experiment_id(&id)
                        )),
                    )
                    .on_hover_text(
                        "Deletes the current experiment and its plots after confirmation.",
                    )
                    .clicked()
                {
                    self.confirm_drop_experiment = Some(id);
                }
            }
            ui.menu_button("More", |ui| {
                if ui
                    .add_enabled(mutation_enabled, egui::Button::new("Edit Metadata"))
                    .on_hover_text("Edit the current experiment's comment and thermal metadata. This does not change columns.")
                    .clicked()
                {
                    self.selected_experiment = self.table_experiment.clone();
                    self.metadata_window_open = true;
                    ui.close();
                }
                ui.separator();
                if ui
                    .add_enabled(
                        mutation_enabled,
                        egui::Button::new("Check time monotonicity"),
                    )
                    .on_hover_text("Checks the current experiment's bound time column. Enter no values; data is not modified.")
                    .clicked()
                {
                    self.handle_check_time_monotonity(model);
                    ui.close();
                }
                if ui
                    .add_enabled(mutation_enabled, egui::Button::new("Sort by time"))
                    .on_hover_text("Sorts every row in the current experiment by its bound time column. No new column is created.")
                    .clicked()
                {
                    self.handle_sort_by_time(model);
                    ui.close();
                }
            });
        });

        if let Some(message) = &self.system_message {
            let color = match message.kind {
                MessageKind::Info => ui.visuals().text_color(),
                MessageKind::Success => egui::Color32::LIGHT_GREEN,
                MessageKind::Warning => egui::Color32::YELLOW,
                MessageKind::Error => egui::Color32::LIGHT_RED,
            };
            ui.colored_label(color, message.accessible_text());
        }
        ui.separator();
    }

    /// Handles drop operation: allows multiple selected columns
    fn selected_column_checkboxes(&self) -> Vec<(String, String)> {
        let Some(active_id) = self.table_experiment.as_deref() else {
            return Vec::new();
        };
        let mut selected = self
            .column_checkboxes
            .iter()
            .filter(|((exp_id, _), checked)| exp_id == active_id && **checked)
            .map(|((exp_id, name), _)| (exp_id.clone(), name.clone()))
            .collect::<Vec<_>>();
        selected.sort();
        selected
    }

    fn handle_drop_columns(&mut self) {
        if self.compare_mode {
            self.set_message(
                MessageKind::Error,
                "Column deletion is unavailable in comparison mode",
            );
            return;
        }
        let selected = self.selected_column_checkboxes();
        if selected.is_empty() {
            self.set_message(MessageKind::Error, "No columns selected for dropping");
            return;
        }
        self.confirm_drop_columns = selected;
    }

    fn commit_drop_columns(&mut self, model: &mut PlotModel) {
        let selected = std::mem::take(&mut self.confirm_drop_columns);
        let Some((exp_id, _)) = selected.first() else {
            self.set_message(MessageKind::Error, "No columns selected for dropping");
            return;
        };
        if selected
            .iter()
            .any(|(selected_exp, _)| selected_exp != exp_id)
        {
            self.set_message(
                MessageKind::Error,
                "A column-delete batch must target one experiment",
            );
            return;
        }
        let col_names = selected
            .iter()
            .map(|(_, name)| name.clone())
            .collect::<Vec<_>>();
        if let Err(error) = model.drop_columns_for_experiment(exp_id, &col_names) {
            self.set_message(
                MessageKind::Error,
                format!("Could not drop columns: {error:?}"),
            );
            return;
        }
        if let Some(preview) = self.cached_samples.get_mut(exp_id) {
            for name in &col_names {
                preview.columns.remove(name);
            }
        }
        for name in col_names {
            self.column_checkboxes.remove(&(exp_id.clone(), name));
        }
        self.set_message(MessageKind::Success, "Columns dropped successfully");
    }

    /// Handles rename operation: requires exactly one selected column
    fn handle_rename_column(&mut self, model: &mut PlotModel, exp_id: &str, old_name: &str) {
        if self.compare_mode {
            self.set_message(
                MessageKind::Error,
                "Column renaming is unavailable in comparison mode",
            );
            return;
        }

        if self.new_column_name.trim().is_empty() {
            self.set_message(MessageKind::Error, "Enter new column name");
            return;
        }

        if let Err(e) =
            model.rename_column_for_experiment(exp_id, old_name, self.new_column_name.trim())
        {
            self.set_message(
                MessageKind::Error,
                format!("Could not rename column: {e:?}"),
            );
            return;
        }

        if let Some(preview) = self.cached_samples.get_mut(exp_id) {
            preview.columns.remove(old_name);
        }
        self.column_checkboxes
            .remove(&(exp_id.to_string(), old_name.to_string()));
        if self
            .selected_column
            .as_ref()
            .map(|(selected_exp, selected_col)| selected_exp == exp_id && selected_col == old_name)
            .unwrap_or(false)
        {
            self.selected_column =
                Some((exp_id.to_string(), self.new_column_name.trim().to_string()));
        }
        self.set_message(MessageKind::Success, "Column renamed successfully");
        self.new_column_name.clear();
    }

    /// Handles drop experiment operation with confirmation dialog
    fn handle_check_time_monotonity(&mut self, model: &mut PlotModel) {
        let selected_experiments = self.table_experiment.iter().cloned().collect::<Vec<_>>();

        if selected_experiments.is_empty() {
            self.set_message(MessageKind::Error, "Select an experiment first");
            info!("Select an experiment first");
            return;
        }

        let mut per_experiment_messages = Vec::new();

        for exp_id in selected_experiments {
            match model.time_monotonicity_report_for_experiment(&exp_id) {
                Ok(report) if report.is_strictly_increasing => {
                    let msg = format!(
                        "Experiment '{}': time monotonicity OK for '{}'",
                        exp_id, report.time_column
                    );
                    info!("{}", msg);
                    per_experiment_messages.push(msg);
                }
                Ok(report) => {
                    let mut details = Vec::new();
                    if !report.null_indices.is_empty() {
                        details.push(format!("nulls at {:?}", report.null_indices));
                    }
                    if !report.nan_indices.is_empty() {
                        details.push(format!("NaNs at {:?}", report.nan_indices));
                    }
                    if !report.infinite_indices.is_empty() {
                        details.push(format!("infinities at {:?}", report.infinite_indices));
                    }
                    if !report.duplicate_pairs.is_empty() {
                        details.push(format!("duplicates at {:?}", report.duplicate_pairs));
                    }
                    if !report.decreasing_pairs.is_empty() {
                        details.push(format!("decreasing at {:?}", report.decreasing_pairs));
                    }
                    let msg = format!(
                        "Experiment '{}': time monotonicity FAILED for '{}' ({})",
                        exp_id,
                        report.time_column,
                        details.join("; ")
                    );
                    info!("{}", msg);
                    per_experiment_messages.push(msg);
                }
                Err(e) => {
                    let msg = format!(
                        "Experiment '{}': time monotonicity check error: {}",
                        exp_id, e
                    );
                    info!("{}", msg);
                    per_experiment_messages.push(msg);
                }
            }
        }

        let kind = if per_experiment_messages
            .iter()
            .any(|message| message.contains("FAILED") || message.contains("error:"))
        {
            MessageKind::Warning
        } else {
            MessageKind::Success
        };
        self.set_message(kind, per_experiment_messages.join(" | "));
    }

    fn handle_sort_by_time(&mut self, model: &mut PlotModel) {
        let selected_experiments = self.table_experiment.iter().cloned().collect::<Vec<_>>();

        if selected_experiments.is_empty() {
            self.set_message(MessageKind::Error, "Select an experiment first");
            info!("Select an experiment first");
            return;
        }

        let mut per_experiment_messages = Vec::new();

        for exp_id in selected_experiments {
            match model.time_monotonicity_report_for_experiment(&exp_id) {
                Ok(report) if !report.sortable_without_data_loss => {
                    let msg = format!(
                        "Experiment '{}': cannot sort time column '{}' because it contains null/NaN/inf values",
                        exp_id, report.time_column
                    );
                    info!("{}", msg);
                    per_experiment_messages.push(msg);
                }
                Ok(report) if report.is_strictly_increasing => {
                    let msg = format!("Experiment '{}': time already monotonic", exp_id);
                    info!("{}", msg);
                    per_experiment_messages.push(msg);
                }
                Ok(report) => match model.sort_experiment_by_time(&exp_id) {
                    Ok(()) => {
                        let msg = if !report.duplicate_pairs.is_empty() {
                            format!(
                                "Experiment '{}': sorted by time; duplicate time values remain in '{}'",
                                exp_id, report.time_column
                            )
                        } else {
                            format!(
                                "Experiment '{}': sorted by time column '{}'",
                                exp_id, report.time_column
                            )
                        };
                        info!("{}", msg);
                        per_experiment_messages.push(msg);
                    }
                    Err(e) => {
                        let msg = format!("Experiment '{}': sort by time failed: {}", exp_id, e);
                        info!("{}", msg);
                        per_experiment_messages.push(msg);
                    }
                },
                Err(e) => {
                    let msg = format!(
                        "Experiment '{}': time monotonicity report error: {}",
                        exp_id, e
                    );
                    info!("{}", msg);
                    per_experiment_messages.push(msg);
                }
            }
        }

        let kind = if per_experiment_messages
            .iter()
            .any(|message| message.contains("failed") || message.contains("cannot sort"))
        {
            MessageKind::Error
        } else if per_experiment_messages
            .iter()
            .any(|message| message.contains("duplicates remain"))
        {
            MessageKind::Warning
        } else if per_experiment_messages
            .iter()
            .any(|message| message.contains("sorted by time"))
        {
            MessageKind::Success
        } else {
            MessageKind::Info
        };
        self.set_message(kind, per_experiment_messages.join(" | "));
    }

    /// Renders the data table with checkboxes in header
    /// Each column represents one experiment-column pair
    fn render_table(&mut self, ui: &mut egui::Ui, model: &PlotModel) {
        let experiments = if self.compare_mode {
            let mut selected = self.compare_experiments.iter().cloned().collect::<Vec<_>>();
            selected.sort();
            selected
        } else {
            self.table_experiment.iter().cloned().collect()
        };
        if experiments.is_empty() {
            ui.label(if self.compare_mode {
                "Select experiments to compare"
            } else {
                "No experiments loaded"
            });
            return;
        }

        // Build list of all (exp_id, col_name) pairs for table columns
        let mut all_columns = Vec::new();
        for exp_id in &experiments {
            if let Ok(cols) = model.list_of_columns(exp_id) {
                for col_name in cols {
                    all_columns.push((exp_id.clone(), col_name));
                }
            }
        }

        let show_row_index = !self.compare_mode;
        ui.push_id("column_manager_table", |ui| {
            egui::ScrollArea::both()
                .id_salt("column_manager_table_scroll")
                .auto_shrink([false, false])
                .max_height(ui.available_height().max(1.0))
                .show(ui, |ui| {
                    let mut table = TableBuilder::new(ui).striped(true);
                    if show_row_index {
                        table = table.column(Column::auto().resizable(true));
                    }
                    table
                        .columns(Column::auto().resizable(true), all_columns.len())
                        .header(40.0, |mut header| {
                            if show_row_index {
                                header.col(|ui| {
                                    ui.heading("Row");
                                });
                            }
                            // Each column header: checkbox + clickable title.
                            for (exp_id, col_name) in &all_columns {
                                header.col(|ui| {
                                    ui.vertical(|ui| {
                                        let key = (exp_id.clone(), col_name.clone());
                                        let checked =
                                            self.column_checkboxes.entry(key).or_insert(false);
                                        let is_selected = self
                                            .selected_column
                                            .as_ref()
                                            .is_some_and(|(selected_exp, selected_col)| {
                                                selected_exp == exp_id && selected_col == col_name
                                            });
                                        let title = ui.selectable_label(
                                            is_selected,
                                            format!("{} {}", exp_id, col_name),
                                        );
                                        let checkbox = ui.add_enabled(
                                            !self.compare_mode,
                                            egui::Checkbox::new(checked, ""),
                                        ).on_hover_text(if self.compare_mode {
                                            "Column selection and data-changing actions are disabled in comparison mode."
                                        } else {
                                            "Select this column for the confirmed bulk-delete action. Clicking the column name only opens its details."
                                        });
                                        let _ = checkbox.labelled_by(title.id);
                                        if title.clicked() {
                                            self.selected_column =
                                                Some((exp_id.clone(), col_name.clone()));
                                        }
                                        if let Ok(Some(provenance)) =
                                            model.column_provenance_for_experiment(exp_id, col_name)
                                        {
                                            title.on_hover_text(provenance);
                                        }
                                    });
                                });
                            }
                        })
                        .body(|mut body| {
                            let preview_lengths = experiments
                                .iter()
                                .filter_map(|id| {
                                    self.cached_samples
                                        .get(id)
                                        .map(|preview| preview.row_indices.len())
                                })
                                .collect::<Vec<_>>();
                            let row_count = if self.compare_mode {
                                preview_lengths.into_iter().min().unwrap_or(0)
                            } else {
                                preview_lengths.into_iter().max().unwrap_or(0)
                            };

                            for row_idx in 0..row_count {
                                body.row(18.0, |mut row| {
                                    if show_row_index {
                                        row.col(|ui| {
                                            let indices = experiments
                                                .iter()
                                                .filter_map(|id| {
                                                    let preview = self.cached_samples.get(id)?;
                                                    preview
                                                        .row_indices
                                                        .get(row_idx)
                                                        .map(|source_row| source_row.to_string())
                                                })
                                                .collect::<Vec<_>>()
                                                .join(" | ");
                                            ui.label(indices);
                                        });
                                    }
                                    for (exp_id, col_name) in &all_columns {
                                        row.col(|ui| {
                                            if let Some(preview) = self.cached_samples.get(exp_id) {
                                                let sample_idx =
                                                    if self.compare_mode && row_count > 1 {
                                                        row_idx
                                                            * preview
                                                                .row_indices
                                                                .len()
                                                                .saturating_sub(1)
                                                            / (row_count - 1)
                                                    } else {
                                                        row_idx
                                                    };
                                                if let Some(values) = preview.columns.get(col_name)
                                                {
                                                    match values.get(sample_idx).copied().flatten()
                                                    {
                                                        Some(val) => {
                                                            ui.label(format_sample_value(val))
                                                        }
                                                        None => ui.label("null"),
                                                    };
                                                } else {
                                                    ui.label("-");
                                                }
                                            } else {
                                                ui.label("-");
                                            }
                                        });
                                    }
                                });
                            }
                        });
                });
        });
    }

    fn render_selected_column_details(&mut self, ui: &mut egui::Ui, model: &mut PlotModel) {
        ui.push_id("column_manager_details", |ui| {
            ui.separator();
            ui.horizontal(|ui| {
                ui.heading("Column details");
                if ui.small_button("Clear").on_hover_text("Close the column inspector; this does not change data.").clicked() {
                    self.selected_column = None;
                }
            });

            let Some((exp_id, col_name)) = self.selected_column.clone() else {
                ui.label("Click a column title to inspect its provenance and operation history.");
                return;
            };

            ui.horizontal(|ui| {
                for (tab, label) in [(DetailsTab::Overview, "Overview"), (DetailsTab::Processing, "Processing"), (DetailsTab::Quality, "Quality"), (DetailsTab::History, "History")] {
                    ui.selectable_value(&mut self.details_tab, tab, label);
                }
            });

            let details_height = ui.available_height().max(140.0);
            egui::ScrollArea::vertical()
                .id_salt(format!(
                    "column_manager_details_scroll:{}:{}",
                    exp_id, col_name
                ))
                .max_height(details_height)
                .show(ui, |ui| {
                    if self.details_tab == DetailsTab::Overview {
                        ui.strong(format!("{} / {}", display_experiment_id(&exp_id), col_name));
                        if let Ok(experiment) = model.series.get_experiment_by_id(&exp_id) {
                            if let Some(meta) = experiment.dataset.schema.columns.get(&col_name) {
                                ui.label(format!("Unit: {:?} | Nature: {:?}", meta.unit, meta.nature));
                                let bindings = [("time", experiment.dataset.schema.time.as_deref()), ("temperature", experiment.dataset.schema.temperature.as_deref()), ("mass", experiment.dataset.schema.mass.as_deref()), ("conversion", experiment.dataset.schema.alpha.as_deref()), ("dimensionless mass", experiment.dataset.schema.eta.as_deref())];
                                let bound = bindings.iter().filter_map(|(role, name)| (*name == Some(col_name.as_str())).then_some(*role)).collect::<Vec<_>>();
                                ui.label(format!("Schema binding: {}", if bound.is_empty() { "none".to_string() } else { bound.join(", ") }));
                            }
                        }
                        ui.label(format!("On plot: {}", model.plots.iter().any(|curve| curve.experiment_id == exp_id && (curve.x_name == col_name || curve.y_name == col_name))));
                        let mass_active = model.active_mass_source_for_experiment(&exp_id).is_ok_and(|source| source == col_name);
                        let temp_active = model.active_temperature_source_for_experiment(&exp_id).is_ok_and(|source| source == col_name);
                        ui.label(format!("Calculation source: {}", match (mass_active, temp_active) { (true, true) => "mass and temperature", (true, false) => "mass", (false, true) => "temperature", _ => "none" }));
                        if self.compare_mode { ui.label("Comparison mode is read-only."); }
                        else {
                            ui.horizontal(|ui| {
                                let label = ui.label("Rename to:");
                                let input = ui.text_edit_singleline(&mut self.new_column_name);
                                let _ = input.labelled_by(label.id);
                                if ui.add_enabled(!self.new_column_name.trim().is_empty(), egui::Button::new("Rename"))
                                    .on_hover_text("Renames this column; no new column is created.")
                                    .clicked() { self.handle_rename_column(model, &exp_id, &col_name); }
                            });
                        }
                    }

                    let provenance = model
                .series
                .get_experiment_by_id(&exp_id)
                .ok()
                .and_then(|experiment| experiment.dataset.column_provenance(&col_name))
                .cloned();
                    let history = model
                        .column_history_feed_for_experiment(&exp_id, &col_name)
                        .unwrap_or_else(|e| format!("Failed to read history: {:?}", e));

                    if self.details_tab == DetailsTab::Processing { ui.label("Provenance"); }
            if let Some(provenance) = provenance {
                if self.details_tab == DetailsTab::Processing { ui.label(format!("Source columns: {}", provenance.root_columns.join(", "))); }
                for (index, step) in provenance.steps.iter().enumerate() {
                    if self.details_tab == DetailsTab::Processing {
                    ui.separator();
                    ui.label(format!(
                        "Step {}: {} -> {}",
                        index + 1,
                        step.input_columns.join(", "),
                        step.output_column
                    ));
                    ui.label(step.kind.to_string());
                    }

                    if self.details_tab == DetailsTab::Quality { if let Some(quality) = &step.quality {
                        ui.label(format!("Quality: {}", step.kind));
                        if let Some(report) = &quality.report {
                        ui.label(format!("Compared {} vs {} ({}, {})", report.column_raw, report.column_filtered, quality.compared_to, quality.status));
                        egui::Grid::new(("column_quality", &exp_id, &col_name, index))
                            .striped(true)
                            .show(ui, |ui| {
                                ui.strong("Metric");
                                ui.strong("Value");
                                ui.strong("Interpretation");
                                ui.end_row();

                                for (name, value, meaning) in [
                                    (
                                        "RMSE",
                                        report.rmse,
                                        "Difference from the comparison column; lower is closer.",
                                    ),
                                    (
                                        "Normalized RMSE",
                                        report.normalized_rmse,
                                        "Scale-normalized difference; lower is closer.",
                                    ),
                                    (
                                        "Correlation",
                                        report.correlation,
                                        "Shape similarity; values closer to 1 indicate stronger agreement.",
                                    ),
                                    (
                                        "First-difference roughness ratio",
                                        report.roughness_ratio_1st,
                                        "Output/input first-difference roughness; below 1 is smoother.",
                                    ),
                                    (
                                        "Second-difference roughness ratio",
                                        report.roughness_ratio_2nd,
                                        "Output/input second-difference roughness; below 1 is smoother.",
                                    ),
                                ] {
                                    ui.label(name);
                                    ui.label(format!("{value:.6}"));
                                    ui.label(meaning);
                                    ui.end_row();
                                }
                            });
                        ui.label(
                            "These metrics describe change relative to the comparison column; they do not establish accuracy against a true signal.",
                        );
                        } else {
                            ui.label(format!("Quality metrics unavailable: {}", quality.status));
                        }
                    }}
                }
            } else {
                if matches!(self.details_tab, DetailsTab::Processing | DetailsTab::Quality) { ui.label("No provenance available"); }
            }

                    if self.details_tab == DetailsTab::History {
                        ui.label("Operation history");
                        ui.monospace(history);
                    }
                });
        });
    }

    /// Renders metadata editor window
    fn show_metadata_window(&mut self, ctx: &egui::Context, model: &mut PlotModel) {
        if !self.metadata_window_open {
            return;
        }

        egui::Window::new("Edit Metadata")
            .open(&mut self.metadata_window_open.clone())
            .resizable(true)
            .default_size([400.0, 300.0])
            .show(ctx, |ui| {
                let experiments = model.list_of_experiments();

                egui::ComboBox::from_label("Select Experiment")
                    .selected_text(self.selected_experiment.as_deref().unwrap_or("None"))
                    .show_ui(ui, |ui| {
                        for exp_id in &experiments {
                            ui.selectable_value(
                                &mut self.selected_experiment,
                                Some(exp_id.clone()),
                                exp_id,
                            );
                        }
                    });

                ui.separator();

                let comment_label = ui.label("Comment:").on_hover_text(
                    "Optional experiment note. Leave blank to keep the current comment.",
                );
                ui.text_edit_multiline(&mut self.comment_input)
                    .labelled_by(comment_label.id);

                let rate_label = ui.label("Heating Rate (K/min):").on_hover_text(
                    "Optional numeric metadata in K/min; leave blank to keep the current value.",
                );
                ui.text_edit_singleline(&mut self.heating_rate_input)
                    .labelled_by(rate_label.id);

                let temperature_label = ui.label("Temperature (K):").on_hover_text(
                    "Optional numeric metadata in K; leave blank to keep the current value.",
                );
                ui.text_edit_singleline(&mut self.temperature_input)
                    .labelled_by(temperature_label.id);

                ui.separator();

                if ui
                    .button("Apply")
                    .on_hover_text(
                        "Saves experiment metadata only; no data columns are created or changed.",
                    )
                    .clicked()
                {
                    if let Some(exp_id) = self.selected_experiment.clone() {
                        match apply_metadata_draft(
                            &mut model.series,
                            &exp_id,
                            &self.comment_input,
                            &self.heating_rate_input,
                            &self.temperature_input,
                        ) {
                            Ok(()) => {
                                self.set_message(
                                    MessageKind::Success,
                                    "Metadata updated successfully",
                                );
                                self.metadata_window_open = false;
                            }
                            Err(error) => self.set_message(
                                MessageKind::Error,
                                format!("Metadata update failed: {error}"),
                            ),
                        }
                    } else {
                        self.set_message(MessageKind::Error, "Select an experiment first");
                    }
                }
            });
    }
}

fn parse_optional_finite_positive(input: &str, label: &str) -> Result<Option<f64>, String> {
    if input.trim().is_empty() {
        return Ok(None);
    }
    let value = input
        .trim()
        .parse::<f64>()
        .map_err(|_| format!("{label} must be a number"))?;
    if !value.is_finite() || value <= 0.0 {
        return Err(format!("{label} must be finite and greater than zero"));
    }
    Ok(Some(value))
}

fn apply_metadata_draft(
    series: &mut TGASeries,
    experiment_id: &str,
    comment: &str,
    heating_rate_input: &str,
    temperature_input: &str,
) -> Result<(), String> {
    // Parse the complete draft before touching the experiment, then commit once.
    let heating_rate = parse_optional_finite_positive(heating_rate_input, "Heating rate")?;
    let temperature = parse_optional_finite_positive(temperature_input, "Temperature")?;
    if heating_rate.is_some() && temperature.is_some() {
        return Err("Set either heating rate or isothermal temperature, not both".to_string());
    }

    series
        .try_mutate_by_id(experiment_id, |experiment| {
            if !comment.trim().is_empty() {
                experiment.meta.comment = Some(comment.trim().to_string());
            }
            if let Some(rate) = heating_rate {
                experiment.meta.heating_rate = Some(rate);
                experiment.meta.thermal_mode = ExperimentThermalMode::NonIsothermal;
                experiment.meta.isothermal_temperature = None;
            }
            if let Some(temperature) = temperature {
                experiment.meta.isothermal_temperature = Some(temperature);
                experiment.meta.thermal_mode = ExperimentThermalMode::Isothermal;
                experiment.meta.heating_rate = None;
            }
            Ok(())
        })
        .map_err(|error| error.to_string())
}

#[cfg(test)]
mod metadata_input_tests {
    use super::{apply_metadata_draft, parse_optional_finite_positive};
    use crate::Kinetics::experimental_kinetics::experiment_series_main::{
        ExperimentMeta, ExperimentThermalMode, TGAExperiment, TGASeries,
    };
    use crate::Kinetics::experimental_kinetics::testing_mod::VirtualTGA;
    use crate::gui::experimental_kinetics_gui::model::PlotModel;
    use egui_kittest::Harness;
    use egui_kittest::kittest::Queryable;

    struct MetadataUiState {
        model: PlotModel,
        manager: super::ColumnManagerState,
    }

    fn series_with_metadata() -> TGASeries {
        let mut series = TGASeries::new();
        let data = VirtualTGA {
            time: vec![0.0, 1.0, 2.0],
            temperature: vec![500.0; 3],
            mass: vec![10.0, 9.0, 8.0],
        };
        let dataset = crate::Kinetics::experimental_kinetics::one_experiment_dataset::TGADataset::create_from_synthetic_data(&data).unwrap();
        series
            .push(TGAExperiment::new(dataset).with_id("meta"))
            .unwrap();
        series
            .try_mutate_by_id("meta", |experiment| {
                experiment.meta = ExperimentMeta {
                    id: "meta".into(),
                    heating_rate: Some(4.0),
                    isothermal_temperature: None,
                    thermal_mode: ExperimentThermalMode::NonIsothermal,
                    comment: Some("keep me".into()),
                };
                Ok(())
            })
            .unwrap();
        series
    }

    #[test]
    fn metadata_numeric_inputs_reject_invalid_values_before_commit() {
        assert_eq!(
            parse_optional_finite_positive("", "Temperature").unwrap(),
            None
        );
        assert_eq!(
            parse_optional_finite_positive("523.15", "Temperature").unwrap(),
            Some(523.15)
        );
        for input in ["abc", "NaN", "inf", "0", "-2"] {
            assert!(
                parse_optional_finite_positive(input, "Temperature").is_err(),
                "accepted {input}"
            );
        }
    }

    #[test]
    fn invalid_metadata_draft_preserves_all_existing_fields() {
        let mut series = series_with_metadata();
        let before = series.get_experiment_by_id("meta").unwrap().meta.clone();

        let error = apply_metadata_draft(&mut series, "meta", "changed", "5", "NaN").unwrap_err();

        assert!(error.contains("finite and greater than zero"));
        let after = &series.get_experiment_by_id("meta").unwrap().meta;
        assert_eq!(after.id, before.id);
        assert_eq!(after.heating_rate, before.heating_rate);
        assert_eq!(after.isothermal_temperature, before.isothermal_temperature);
        assert_eq!(after.thermal_mode, before.thermal_mode);
        assert_eq!(after.comment, before.comment);
    }

    #[test]
    fn valid_metadata_draft_commits_mode_and_comment_together() {
        let mut series = series_with_metadata();
        apply_metadata_draft(&mut series, "meta", "  updated  ", "", "523.15").unwrap();

        let meta = &series.get_experiment_by_id("meta").unwrap().meta;
        assert_eq!(meta.comment.as_deref(), Some("updated"));
        assert_eq!(meta.thermal_mode, ExperimentThermalMode::Isothermal);
        assert_eq!(meta.isothermal_temperature, Some(523.15));
        assert!(meta.heating_rate.is_none());
    }

    #[test]
    fn egui_apply_with_invalid_metadata_keeps_the_original_record() {
        let mut model = PlotModel::default();
        model.series = series_with_metadata();
        let mut manager = super::ColumnManagerState::new();
        manager.metadata_window_open = true;
        manager.selected_experiment = Some("meta".into());
        manager.comment_input = "must not partially apply".into();
        manager.heating_rate_input = "5".into();
        manager.temperature_input = "NaN".into();
        let before = model
            .series
            .get_experiment_by_id("meta")
            .unwrap()
            .meta
            .clone();
        let mut harness = Harness::new_ui_state(
            |ui, state: &mut MetadataUiState| {
                state
                    .manager
                    .show_metadata_window(ui.ctx(), &mut state.model);
            },
            MetadataUiState { model, manager },
        );

        harness.get_by_label("Apply").click();
        harness.run();

        let state = harness.state();
        let after = &state
            .model
            .series
            .get_experiment_by_id("meta")
            .unwrap()
            .meta;
        assert_eq!(after.comment, before.comment);
        assert_eq!(after.heating_rate, before.heating_rate);
        assert_eq!(after.isothermal_temperature, before.isothermal_temperature);
        assert_eq!(after.thermal_mode, before.thermal_mode);
        assert!(
            state
                .manager
                .system_message
                .as_ref()
                .unwrap()
                .text
                .contains("Metadata update failed")
        );
    }

    #[test]
    fn egui_apply_commits_valid_metadata_and_comment_together() {
        let mut model = PlotModel::default();
        model.series = series_with_metadata();
        let mut manager = super::ColumnManagerState::new();
        manager.metadata_window_open = true;
        manager.selected_experiment = Some("meta".into());
        manager.comment_input = "updated from GUI".into();
        manager.temperature_input = "523.15".into();
        let mut harness = Harness::new_ui_state(
            |ui, state: &mut MetadataUiState| {
                state
                    .manager
                    .show_metadata_window(ui.ctx(), &mut state.model);
            },
            MetadataUiState { model, manager },
        );

        harness.get_by_label("Apply").click();
        harness.run();

        let state = harness.state();
        let meta = &state
            .model
            .series
            .get_experiment_by_id("meta")
            .unwrap()
            .meta;
        assert_eq!(meta.comment.as_deref(), Some("updated from GUI"));
        assert_eq!(meta.thermal_mode, ExperimentThermalMode::Isothermal);
        assert_eq!(meta.isothermal_temperature, Some(523.15));
        assert!(meta.heating_rate.is_none());
        assert!(
            state
                .manager
                .system_message
                .as_ref()
                .unwrap()
                .text
                .contains("Metadata updated successfully")
        );
    }
}

/// Main window function for Column Manager
pub fn show_column_manager_window(
    ctx: &egui::Context,
    model: &mut PlotModel,
    state: &mut ColumnManagerState,
    methods_state: &mut KineticMethodsWindowState,
    open: &mut bool,
) {
    state.show_metadata_window(ctx, model);
    // state.system_message.clear();
    egui::Window::new("Column Manager")
        .open(open)
        .resizable(true)
        .default_size([800.0, 600.0])
        .show(ctx, |ui| {
            let mut commit_reconstruction = false;
            let mut discard_reconstruction = false;
            let mut confirm_columns = false;
            let mut cancel_columns = false;
            if let Err(e) = state.update_cache(model) {
                let text = format!("Could not refresh column samples: {e}");
                state.set_message_if_changed(MessageKind::Error, text.clone());
                ui.colored_label(egui::Color32::LIGHT_RED, text);
                return;
            }

            if let Some(batch) = methods_state.fit_pending_batch.as_ref() {
                ui.separator();
                ui.heading("Fit reconstruction preview");
                ui.label(format!(
                    "Source: {} | X: {} | {} rows | {} fitted column(s) | {:?}",
                    batch.source_experiment_id,
                    batch.x_col,
                    batch.x_values.len(),
                    batch.columns.len(),
                    batch.mode,
                ));
                for column in &batch.columns {
                    let coefficients = column.fit.coefficients.iter()
                        .map(|(name, value)| format!("{name}={value:.5e}"))
                        .collect::<Vec<_>>().join(", ");
                    ui.label(format!(
                        "{}: R2={:.6}, model={}, Y={}, parameters: {}",
                        column.name,
                        column.fit.r2,
                        column.fit.kinetic_model.display_name(),
                        column.fit.y_col,
                        coefficients,
                    ));
                }
                ui.horizontal(|ui| {
                    let label = ui.label("New experiment ID:");
                    let input = ui.text_edit_singleline(&mut methods_state.fit_reconstructed_experiment_id);
                    let _ = input.labelled_by(label.id);
                    if ui.button("Add reconstructed experiment")
                        .on_hover_text("Creates a separate complete experiment and plots its fitted columns. The source experiment is not modified.")
                        .clicked()
                    {
                        commit_reconstruction = true;
                    }
                    if ui.button("Discard preview")
                        .on_hover_text("Discard this evaluation and its temporary fit sessions; source experiment data remains unchanged.")
                        .clicked() {
                        discard_reconstruction = true;
                    }
                });
                egui::CollapsingHeader::new("Sample values")
                    .default_open(true)
                    .show(ui, |ui| {
                        let (start, end) = match batch.mode {
                            crate::Kinetics::experimental_kinetics::math::fitting::FitEvaluationMode::Extrapolate { start, end, .. }
                            | crate::Kinetics::experimental_kinetics::math::fitting::FitEvaluationMode::FillGap { start, end, .. } => (start, end),
                        };
                        let preview_indices = batch.x_values.iter().enumerate()
                            .filter(|(_, x)| **x >= start && **x <= end)
                            .map(|(index, _)| index)
                            .take(8)
                            .collect::<Vec<_>>();
                        egui::ScrollArea::both().max_height(140.0).show(ui, |ui| {
                            egui::Grid::new("fit_reconstruction_preview_values")
                                .striped(true)
                                .show(ui, |ui| {
                                    ui.strong(&batch.x_col);
                                    for column in &batch.columns { ui.strong(&column.name); }
                                    ui.end_row();
                                    for index in &preview_indices {
                                        ui.label(format!("{:.6}", batch.x_values[*index]));
                                        for column in &batch.columns {
                                            ui.label(format!("{:.6}", column.values[*index]));
                                        }
                                        ui.end_row();
                                    }
                                });
                        });
                    });
            }
            if commit_reconstruction {
                if let Some(batch) = methods_state.fit_pending_batch.as_ref() {
                    let id = methods_state.fit_reconstructed_experiment_id.trim().to_string();
                    match model.push_fitted_evaluation_batch_and_plot(batch, id) {
                        Ok(()) => {
                            methods_state.fit_pending_batch = None;
                            methods_state.fit_sessions.clear();
                            state.set_message(MessageKind::Success, "Reconstructed experiment added; source data unchanged");
                        }
                        Err(crate::gui::experimental_kinetics_gui::model::TGAGUIError::PostCommitWarning(warning)) => {
                            methods_state.fit_pending_batch = None;
                            methods_state.fit_sessions.clear();
                            state.set_message(MessageKind::Warning, warning);
                        }
                        Err(error) => state.set_message(MessageKind::Error, format!("Reconstruction was not added: {error:?}")),
                    }
                }
            }
            if discard_reconstruction {
                methods_state.fit_pending_batch = None;
                methods_state.fit_sessions.clear();
                methods_state.fit_model_result = None;
            }

            if !state.confirm_drop_columns.is_empty() {
                egui::Window::new("Confirm Drop Columns")
                    .collapsible(false)
                    .resizable(false)
                    .show(ctx, |ui| {
                        ui.label(format!("Delete {} column(s) from {}?", state.confirm_drop_columns.len(), display_experiment_id(&state.confirm_drop_columns[0].0)));
                        for (_, name) in &state.confirm_drop_columns {
                            ui.label(name);
                        }
                        ui.horizontal(|ui| {
                            if ui.button("Delete columns")
                                .on_hover_text("Permanently removes the listed columns from this experiment; undo may be available from History.")
                                .clicked() { confirm_columns = true; }
                            if ui.button("Cancel").on_hover_text("Close this confirmation without changing data.").clicked() { cancel_columns = true; }
                        });
                    });
            }
            if confirm_columns {
                state.commit_drop_columns(model);
            }
            if cancel_columns {
                state.confirm_drop_columns.clear();
            }

            // Handle experiment drop confirmation dialog
            if let Some(exp_id) = state.confirm_drop_experiment.clone() {
                let mut confirmed = false;
                let mut cancelled = false;

                egui::Window::new("Confirm Drop Experiment")
                    .collapsible(false)
                    .resizable(false)
                    .show(ctx, |ui| {
                        ui.label(format!("Delete experiment {} and its plots?", display_experiment_id(&exp_id)));
                        ui.horizontal(|ui| {
                            if ui.button("Yes").on_hover_text("Deletes this experiment and all its plots.").clicked() {
                                confirmed = true;
                            }
                            if ui.button("No").on_hover_text("Keep the experiment unchanged.").clicked() {
                                cancelled = true;
                            }
                        });
                    });

                if confirmed {
                    if let Err(e) = model.drop_experiment_by_id(&exp_id) {
                        state.set_message(MessageKind::Error, format!("Could not drop experiment: {e:?}"));
                    } else {
                        state.set_message(MessageKind::Success, format!("Experiment {} dropped successfully", exp_id));
                        // Clear cached data for this experiment
                        state.cached_samples.remove(&exp_id);
                        // Remove all checkboxes for this experiment
                        state.column_checkboxes.retain(|(eid, _), _| eid != &exp_id);
                        if state
                            .selected_column
                            .as_ref()
                            .map(|(selected_exp, _)| selected_exp == &exp_id)
                            .unwrap_or(false)
                        {
                            state.selected_column = None;
                        }
                    }
                    state.confirm_drop_experiment = None;
                }

                if cancelled {
                    state.confirm_drop_experiment = None;
                }
            }

            state.render_controls(ui, model);
            let max_samples_height = (ui.available_height() - 120.0).clamp(180.0, 760.0);
            egui::Resize::default()
                .id_salt("column_manager_samples_resize")
                .default_height((max_samples_height * 0.72).max(180.0))
                .min_size(egui::vec2(300.0, 110.0))
                .max_height(max_samples_height)
                .resizable(egui::Vec2b::new(false, true))
                .show(ui, |ui| state.render_table(ui, model));
            state.render_selected_column_details(ui, model);
        });
    state.publish_message_to_main_status(model);
}