qleany 1.7.3

Architecture generator for Rust and C++/Qt applications.
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
//! Generate tab - File generation UI logic
//!
//! This module handles the Generate tab functionality including:
//! - Listing files to be generated
//! - Computing file status (New, Modified, Unchanged) via fill_code + fill_status pipeline
//! - Filtering files by group, text, and status
//! - Previewing generated code
//! - Starting/cancelling file generation

use std::collections::BTreeSet;
use std::sync::Arc;

use slint::{ComponentHandle, Model, SharedString, VecModel};

use crate::app_context::AppContext;
use crate::commands::{
    cpp_qt_file_generation_commands, file_commands, file_generation_shared_steps_commands,
    global_commands, rust_file_generation_commands, system_commands,
};
use crate::event_hub_client::EventHubClient;
use crate::{App, AppState, GenerateCommands, ListItem};
use common::direct_access::system::SystemRelationshipField;
use common::entities::FileNature;
use common::entities::FileStatus;
use common::long_operation::OperationProgress;
use common::types::EntityId;
use cpp_qt_file_generation::{FillCppQtFilesDto, GenerateCppQtFilesDto};
use file_generation_shared_steps::GetDiffDto;
use rust_file_generation::{FillRustFilesDto, GenerateRustCodeDto, GenerateRustFilesDto};

const ROOT_SYSTEM_ID: u64 = 1;

enum Language {
    Rust,
    CppQt,
}

fn determine_language(app: &App, app_context: &Arc<AppContext>) -> Result<Language, String> {
    if let Some(global_id) = crate::tabs::common::get_global_id(app, app_context)
        && let Ok(Some(global)) = global_commands::get_global(app_context, &global_id)
    {
        match global.language.as_str() {
            "rust" => Ok(Language::Rust),
            "cpp-qt" => Ok(Language::CppQt),
            other => Err(format!("Unsupported language: {}", other)),
        }
    } else {
        Err("Failed to determine language".to_string())
    }
}

// ─── File display helpers ───────────────────────────────────────────────────

struct FileDisplayData {
    file_id: i32,
    display_name: String,
    group: String,
    status: FileStatus,
    nature: FileNature,
}

/// Read all files from DB and return display data
fn read_files_from_db(app_context: &Arc<AppContext>) -> Result<Vec<FileDisplayData>, String> {
    let file_ids = system_commands::get_system_relationship(
        app_context,
        &ROOT_SYSTEM_ID,
        &SystemRelationshipField::Files,
    )
    .map_err(|e| e.to_string())?;
    let files = file_commands::get_file_multi(app_context, &file_ids).map_err(|e| e.to_string())?;

    Ok(files
        .into_iter()
        .flatten()
        .map(|f| FileDisplayData {
            file_id: f.id as i32,
            display_name: format!("{}{}", f.relative_path, f.name),
            group: f.group,
            status: f.status,
            nature: f.nature,
        })
        .collect())
}

/// Convert FileStatus to gradient color
fn status_to_color(status: &FileStatus) -> slint::Color {
    match status {
        FileStatus::Modified => slint::Color::from_rgb_u8(255, 152, 0), // orange
        FileStatus::New => slint::Color::from_rgb_u8(76, 175, 80),      // green
        FileStatus::Unchanged => slint::Color::from_rgb_u8(158, 158, 158), // grey
        FileStatus::Unknown => slint::Color::default(),                 // transparent
    }
}

type DisplayParts = (
    Vec<SharedString>, // text_prefixes
    Vec<SharedString>, // text_bolds
    Vec<SharedString>, // elided_texts
    Vec<SharedString>, // elided_prefixes
    Vec<SharedString>, // elided_bolds
);

/// Compute text display parts (prefix/bold split and elided versions) for file names
fn compute_display_parts(file_names: &[&str]) -> DisplayParts {
    let max_text_length = 50;
    let mut text_prefixes = Vec::new();
    let mut text_bolds = Vec::new();
    let mut elided_texts = Vec::new();
    let mut elided_prefixes = Vec::new();
    let mut elided_bolds = Vec::new();

    for file_name in file_names {
        // Split at last slash for bold display
        if let Some(last_slash) = file_name.rfind('/') {
            text_prefixes.push(SharedString::from(&file_name[..=last_slash]));
            text_bolds.push(SharedString::from(&file_name[last_slash + 1..]));
        } else {
            text_prefixes.push(SharedString::default());
            text_bolds.push(SharedString::from(*file_name));
        }

        // Elided versions for long names
        let char_count = file_name.chars().count();
        if char_count > max_text_length {
            let suffix: String = file_name
                .chars()
                .skip(char_count - max_text_length)
                .collect();
            let elided_full = format!("...{}", suffix);

            if let Some(last_slash) = elided_full.rfind('/') {
                elided_prefixes.push(SharedString::from(&elided_full[..=last_slash]));
                elided_bolds.push(SharedString::from(&elided_full[last_slash + 1..]));
            } else {
                elided_prefixes.push(SharedString::default());
                elided_bolds.push(SharedString::from(elided_full.as_str()));
            }
            elided_texts.push(SharedString::from(elided_full.as_str()));
        } else {
            elided_texts.push(SharedString::default());
            elided_prefixes.push(SharedString::default());
            elided_bolds.push(SharedString::default());
        }
    }

    (
        text_prefixes,
        text_bolds,
        elided_texts,
        elided_prefixes,
        elided_bolds,
    )
}

/// Set file UI models from filtered display data
fn set_file_ui_models(app: &App, filtered: &[&FileDisplayData]) {
    let file_items: Vec<ListItem> = filtered
        .iter()
        .map(|f| ListItem {
            id: f.file_id,
            text: SharedString::from(f.display_name.as_str()),
            subtitle: SharedString::default(),
            checked: true,
            gradient_color: status_to_color(&f.status),
        })
        .collect();

    let file_names: Vec<&str> = filtered.iter().map(|f| f.display_name.as_str()).collect();
    let (text_prefixes, text_bolds, elided_texts, elided_prefixes, elided_bolds) =
        compute_display_parts(&file_names);

    let selected_count = file_items.iter().filter(|f| f.checked).count() as i32;

    let state = app.global::<AppState>();
    state.set_file_cr_list(std::rc::Rc::new(VecModel::from(file_items)).into());
    state.set_file_text_prefixes(std::rc::Rc::new(VecModel::from(text_prefixes)).into());
    state.set_file_text_bolds(std::rc::Rc::new(VecModel::from(text_bolds)).into());
    state.set_file_elided_texts(std::rc::Rc::new(VecModel::from(elided_texts)).into());
    state.set_file_elided_prefixes(std::rc::Rc::new(VecModel::from(elided_prefixes)).into());
    state.set_file_elided_bolds(std::rc::Rc::new(VecModel::from(elided_bolds)).into());
    state.set_selected_files_count(selected_count);
    state.set_selected_file_index(-1);
    state.set_code_preview(SharedString::from(""));
}

/// Get the currently selected group name from UI state
fn get_selected_group_name(app: &App) -> String {
    let state = app.global::<AppState>();
    let group_list = state.get_group_list();
    let group_cr_list = state.get_group_cr_list();

    for i in 0..group_cr_list.row_count() {
        if let Some(item) = group_cr_list.row_data(i)
            && item.checked
        {
            return group_list
                .row_data(item.id as usize)
                .unwrap_or_default()
                .to_string();
        }
    }
    "All".to_string()
}

/// Filter files by current UI state (group, text, show_all) and update display
fn update_file_display_from_db(app: &App, app_context: &Arc<AppContext>) {
    let files = match read_files_from_db(app_context) {
        Ok(f) => f,
        Err(e) => {
            log::error!("Failed to read files from DB: {}", e);
            return;
        }
    };

    let state = app.global::<AppState>();
    let show_modified = state.get_show_modified_files();
    let show_new = state.get_show_new_files();
    let show_unchanged = state.get_show_unchanged_files();
    let show_infra = state.get_show_infra_files();
    let show_aggregate = state.get_show_aggregate_files();
    let show_scaffold = state.get_show_scaffold_files();
    let text_filter = state.get_file_filter_text().to_string().to_lowercase();
    let selected_group = get_selected_group_name(app);

    let filtered: Vec<&FileDisplayData> = files
        .iter()
        .filter(|f| match f.status {
            FileStatus::Modified => show_modified,
            FileStatus::New => show_new,
            FileStatus::Unchanged => show_unchanged,
            FileStatus::Unknown => true, // always show Unknown status
        })
        .filter(|f| match f.nature {
            FileNature::Infrastructure => show_infra,
            FileNature::Aggregate => show_aggregate,
            FileNature::Scaffold => show_scaffold,
        })
        .filter(|f| selected_group == "All" || f.group == selected_group)
        .filter(|f| text_filter.is_empty() || f.display_name.to_lowercase().contains(&text_filter))
        .collect();

    set_file_ui_models(app, &filtered);
}

/// Build full display (groups + files) from DB
fn update_full_display(app: &App, app_context: &Arc<AppContext>) {
    let files = match read_files_from_db(app_context) {
        Ok(f) => f,
        Err(e) => {
            log::error!("Failed to read files from DB: {}", e);
            return;
        }
    };

    // Build sorted group list
    let mut group_set: BTreeSet<String> = BTreeSet::new();
    for f in &files {
        group_set.insert(f.group.clone());
    }
    let group_names: Vec<String> = group_set.into_iter().collect();

    // Build group items with "All" as first entry (checked by default)
    let mut group_items: Vec<ListItem> = vec![ListItem {
        id: 0,
        text: SharedString::from("All"),
        subtitle: SharedString::default(),
        checked: true,
        gradient_color: slint::Color::default(),
    }];
    for (idx, name) in group_names.iter().enumerate() {
        group_items.push(ListItem {
            id: (idx + 1) as i32,
            text: SharedString::from(name.as_str()),
            subtitle: SharedString::default(),
            checked: false,
            gradient_color: slint::Color::default(),
        });
    }

    let state = app.global::<AppState>();

    // Store group names for lookup
    let mut group_list: Vec<SharedString> = vec![SharedString::from("All")];
    group_list.extend(group_names.iter().map(|s| SharedString::from(s.as_str())));

    state.set_group_cr_list(std::rc::Rc::new(VecModel::from(group_items)).into());
    state.set_group_list(std::rc::Rc::new(VecModel::from(group_list)).into());

    // Reset selection and filter
    state.set_selected_group_index(-1);
    state.set_selected_file_index(-1);
    state.set_code_preview(SharedString::from(""));
    state.set_file_filter_text(SharedString::from(""));

    // Apply status and nature filters
    let show_modified = state.get_show_modified_files();
    let show_new = state.get_show_new_files();
    let show_unchanged = state.get_show_unchanged_files();
    let show_infra = state.get_show_infra_files();
    let show_aggregate = state.get_show_aggregate_files();
    let show_scaffold = state.get_show_scaffold_files();

    let filtered: Vec<&FileDisplayData> = files
        .iter()
        .filter(|f| match f.status {
            FileStatus::Modified => show_modified,
            FileStatus::New => show_new,
            FileStatus::Unchanged => show_unchanged,
            FileStatus::Unknown => true,
        })
        .filter(|f| match f.nature {
            FileNature::Infrastructure => show_infra,
            FileNature::Aggregate => show_aggregate,
            FileNature::Scaffold => show_scaffold,
        })
        .collect();

    set_file_ui_models(app, &filtered);

    log::info!(
        "Loaded {} groups and {} files ({} shown)",
        group_names.len(),
        files.len(),
        filtered.len()
    );
}

// ─── Fill code + status pipeline ────────────────────────────────────────────

/// Fill file entries in DB (step 1: quick)
fn fill_file_entries(app: &App, app_context: &Arc<AppContext>) -> Result<(), String> {
    match determine_language(app, app_context)? {
        Language::Rust => {
            let dto = FillRustFilesDto {
                only_list_already_existing: false,
            };
            rust_file_generation_commands::fill_rust_files(app_context, &dto).map(|_| ())
        }
        Language::CppQt => {
            let dto = FillCppQtFilesDto {
                only_list_already_existing: false,
            };
            cpp_qt_file_generation_commands::fill_cpp_qt_files(app_context, &dto).map(|_| ())
        }
    }
}

/// Start fill_code long operation (step 2: async)
fn start_fill_code(app: &App, app_context: &Arc<AppContext>) -> Result<String, String> {
    match determine_language(app, app_context)? {
        Language::Rust => rust_file_generation_commands::fill_code_in_rust_files(app_context),
        Language::CppQt => cpp_qt_file_generation_commands::fill_code_in_cpp_qt_files(app_context),
    }
}

/// Get fill_code progress
fn get_fill_code_progress(
    app: &App,
    app_context: &Arc<AppContext>,
    operation_id: &str,
) -> Result<Option<OperationProgress>, String> {
    match determine_language(app, app_context)? {
        Language::Rust => rust_file_generation_commands::get_fill_code_in_rust_files_progress(
            app_context,
            operation_id,
        ),
        Language::CppQt => cpp_qt_file_generation_commands::get_fill_code_in_cpp_qt_files_progress(
            app_context,
            operation_id,
        ),
    }
}

/// Get fill_code result
fn get_fill_code_result(
    app: &App,
    app_context: &Arc<AppContext>,
    operation_id: &str,
) -> Result<Option<()>, String> {
    match determine_language(app, app_context)? {
        Language::Rust => rust_file_generation_commands::get_fill_code_in_rust_files_result(
            app_context,
            operation_id,
        ),
        Language::CppQt => cpp_qt_file_generation_commands::get_fill_code_in_cpp_qt_files_result(
            app_context,
            operation_id,
        ),
    }
}

/// Poll fill_code operation, then fill status and update display when done
fn poll_fill_code_result(app_weak: slint::Weak<App>, ctx: Arc<AppContext>, operation_id: String) {
    if let Some(app) = app_weak.upgrade() {
        if !app.global::<AppState>().get_fill_code_is_running() {
            return;
        }

        // Update progress
        if let Ok(Some(progress)) = get_fill_code_progress(&app, &ctx, &operation_id) {
            app.global::<AppState>()
                .set_fill_code_progress(progress.percentage / 100.0);
            if let Some(msg) = progress.message {
                app.global::<AppState>()
                    .set_fill_code_message(SharedString::from(msg));
            }
        }

        // Check result
        match get_fill_code_result(&app, &ctx, &operation_id) {
            Ok(Some(())) => {
                // Fill code complete → fill status → update display
                log::info!("Fill code complete, computing status...");
                app.global::<AppState>()
                    .set_fill_code_message(SharedString::from("Comparing with disk..."));

                if let Err(e) = file_generation_shared_steps_commands::fill_status_in_files(&ctx) {
                    log::error!("Failed to fill status: {}", e);
                }

                update_full_display(&app, &ctx);
                app.global::<AppState>().set_fill_code_is_running(false);
            }
            Ok(None) => {
                // Still running, poll again
                let app_weak_clone = app.as_weak();
                let ctx_clone = Arc::clone(&ctx);
                let op_id = operation_id.clone();
                slint::Timer::single_shot(std::time::Duration::from_millis(200), move || {
                    poll_fill_code_result(app_weak_clone, ctx_clone, op_id);
                });
            }
            Err(e) => {
                log::error!("Error checking fill code result: {}", e);
                app.global::<AppState>().set_fill_code_is_running(false);
                // Show files without status
                update_full_display(&app, &ctx);
            }
        }
    }
}

/// Refresh file lists - runs the full pipeline (fill files → fill code → fill status → display)
fn refresh_file_lists(app: &App, app_context: &Arc<AppContext>) {
    // Step 1: Fill file entries in DB
    if let Err(e) = fill_file_entries(app, app_context) {
        log::error!("Failed to fill files: {}", e);
        app.global::<AppState>()
            .set_error_message(SharedString::from(e.as_str()));
        return;
    }

    // Step 2: Start fill_code long operation with progress
    app.global::<AppState>().set_fill_code_is_running(true);
    app.global::<AppState>().set_fill_code_progress(0.0);
    app.global::<AppState>()
        .set_fill_code_message(SharedString::from("Computing file status..."));

    match start_fill_code(app, app_context) {
        Ok(operation_id) => {
            app.global::<AppState>()
                .set_fill_code_operation_id(SharedString::from(operation_id.as_str()));
            poll_fill_code_result(app.as_weak(), Arc::clone(app_context), operation_id);
        }
        Err(e) => {
            log::error!("Failed to start fill code: {}", e);
            app.global::<AppState>().set_fill_code_is_running(false);
            // Show files without status
            update_full_display(app, app_context);
        }
    }
}

// ─── Code preview ───────────────────────────────────────────────────────────

/// Load code preview for selected file (generated code or diff depending on view_diff)
fn load_code_preview(app: &App, app_context: &Arc<AppContext>, file_id: i32) {
    if file_id < 0 {
        app.global::<AppState>()
            .set_code_preview(SharedString::from(""));
        return;
    }

    let view_diff = app.global::<AppState>().get_view_diff();

    if view_diff {
        load_diff_preview(app, app_context, file_id);
    } else {
        load_generated_code_preview(app, app_context, file_id);
    }
}

/// Load unified diff for the selected file
fn load_diff_preview(app: &App, app_context: &Arc<AppContext>, file_id: i32) {
    let dto = GetDiffDto {
        file_id: file_id as u64,
    };

    match file_generation_shared_steps_commands::get_file_diff(app_context, &dto) {
        Ok(result) => {
            let text = if result.diff_text.is_empty() {
                "No differences".to_string()
            } else {
                result.diff_text
            };
            app.global::<AppState>()
                .set_code_preview(SharedString::from(text.as_str()));
        }
        Err(e) => {
            log::error!("Failed to get file diff: {}", e);
            app.global::<AppState>()
                .set_code_preview(SharedString::from(format!("Error: {}", e).as_str()));
        }
    }
}

/// Load generated code preview for the selected file
fn load_generated_code_preview(app: &App, app_context: &Arc<AppContext>, file_id: i32) {
    match determine_language(app, app_context) {
        Ok(Language::Rust) => {
            let dto = GenerateRustCodeDto {
                file_id: file_id as u64,
            };

            match rust_file_generation_commands::generate_rust_code(app_context, &dto) {
                Ok(result) => {
                    app.global::<AppState>()
                        .set_code_preview(SharedString::from(result.generated_code.as_str()));
                }
                Err(e) => {
                    log::error!("Failed to generate code preview: {}", e);
                    app.global::<AppState>()
                        .set_code_preview(SharedString::from(format!("Error: {}", e).as_str()));
                }
            }
        }
        Ok(Language::CppQt) => {
            let dto = cpp_qt_file_generation::GenerateCppQtCodeDto {
                file_id: file_id as u64,
            };

            match cpp_qt_file_generation_commands::generate_cpp_qt_code(app_context, &dto) {
                Ok(result) => {
                    app.global::<AppState>()
                        .set_code_preview(SharedString::from(result.generated_code.as_str()));
                }
                Err(e) => {
                    log::error!("Failed to generate code preview: {}", e);
                    app.global::<AppState>()
                        .set_code_preview(SharedString::from(format!("Error: {}", e).as_str()));
                }
            }
        }
        Err(e) => {
            log::error!("Failed to determine language: {}", e);
            app.global::<AppState>()
                .set_code_preview(SharedString::from(format!("Error: {}", e).as_str()));
        }
    }
}

// ─── Callback setup ─────────────────────────────────────────────────────────

fn setup_list_files_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<GenerateCommands>().on_list_files({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move || {
            log::info!("List Files clicked");
            if let Some(app) = app_weak.upgrade() {
                refresh_file_lists(&app, &ctx);
            }
        }
    });
}

fn setup_start_generate_callback(app: &App, app_context: &Arc<AppContext>) {
    fn generate_files_helper(
        app: &App,
        app_context: &Arc<AppContext>,
        file_ids: Vec<EntityId>,
        root_path: String,
        prefix: String,
    ) -> Result<String, String> {
        match determine_language(app, app_context)? {
            Language::Rust => {
                let dto = GenerateRustFilesDto {
                    file_ids,
                    root_path,
                    prefix,
                };

                rust_file_generation_commands::generate_rust_files(app_context, &dto)
            }
            Language::CppQt => {
                let dto = GenerateCppQtFilesDto {
                    file_ids,
                    root_path,
                    prefix,
                };

                cpp_qt_file_generation_commands::generate_cpp_qt_files(app_context, &dto)
            }
        }
    }

    app.global::<GenerateCommands>().on_start_generate({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move || {
            log::info!("Start Generate clicked");
            if let Some(app) = app_weak.upgrade() {
                // Get checked file IDs from the file list
                let file_list = app.global::<AppState>().get_file_cr_list();
                let mut file_ids: Vec<u64> = Vec::new();

                for i in 0..file_list.row_count() {
                    if let Some(item) = file_list.row_data(i)
                        && item.checked
                    {
                        file_ids.push(item.id as u64);
                    }
                }

                if file_ids.is_empty() {
                    log::warn!("No files selected for generation");
                    return;
                }

                // Get generate_in_temp setting
                let in_temp = app.global::<AppState>().get_generate_in_temp();
                let root_path = ".".to_string();
                let prefix = if in_temp {
                    "temp".to_string()
                } else {
                    String::new()
                };

                // Set running state
                app.global::<AppState>().set_generate_is_running(true);
                app.global::<AppState>().set_generate_progress(0.0);
                app.global::<AppState>()
                    .set_generate_message(SharedString::from("Starting generation..."));

                match generate_files_helper(&app, &ctx, file_ids, root_path, prefix) {
                    Ok(operation_id) => {
                        log::info!("Started generation with operation ID: {}", operation_id);
                        let ctx_clone = Arc::clone(&ctx);
                        let app_weak_clone = app.as_weak();
                        let op_id = operation_id.clone();

                        slint::Timer::single_shot(
                            std::time::Duration::from_millis(500),
                            move || {
                                poll_generation_result(app_weak_clone, ctx_clone, op_id);
                            },
                        );
                    }
                    Err(e) => {
                        log::error!("Failed to start generation: {}", e);
                        app.global::<AppState>().set_generate_is_running(false);
                        app.global::<AppState>()
                            .set_error_message(SharedString::from(e.as_str()));
                    }
                }
            }
        }
    });
}

/// Poll for generation result
fn poll_generation_result(app_weak: slint::Weak<App>, ctx: Arc<AppContext>, operation_id: String) {
    struct GenerateFilesReturnDto {
        pub files: Vec<String>,
    }

    fn get_generate_files_result_helper(
        app: &App,
        app_context: &Arc<AppContext>,
        operation_id: &str,
    ) -> Result<Option<GenerateFilesReturnDto>, String> {
        match determine_language(app, app_context)? {
            Language::Rust => {
                match rust_file_generation_commands::get_generate_rust_files_result(
                    app_context,
                    operation_id,
                ) {
                    Ok(Some(result)) => Ok(Some(GenerateFilesReturnDto {
                        files: result.files,
                    })),
                    Ok(None) => Ok(None),
                    Err(e) => Err(format!("Error getting rust generation result: {}", e)),
                }
            }
            Language::CppQt => {
                match cpp_qt_file_generation_commands::get_generate_cpp_qt_files_result(
                    app_context,
                    operation_id,
                ) {
                    Ok(Some(result)) => Ok(Some(GenerateFilesReturnDto {
                        files: result.files,
                    })),
                    Ok(None) => Ok(None),
                    Err(e) => Err(format!("Error getting C++/Qt generation result: {}", e)),
                }
            }
        }
    }
    fn get_generate_files_progress_helper(
        app: &App,
        app_context: &Arc<AppContext>,
        operation_id: &str,
    ) -> Result<Option<OperationProgress>, String> {
        match determine_language(app, app_context)? {
            Language::Rust => {
                match rust_file_generation_commands::get_generate_rust_files_progress(
                    app_context,
                    operation_id,
                ) {
                    Ok(Some(progress)) => Ok(Some(progress)),
                    Ok(None) => Ok(None),
                    Err(e) => Err(format!("Error getting Rust generation progress: {}", e)),
                }
            }
            Language::CppQt => {
                match cpp_qt_file_generation_commands::get_generate_cpp_qt_files_progress(
                    app_context,
                    operation_id,
                ) {
                    Ok(Some(progress)) => Ok(Some(progress)),
                    Ok(None) => Ok(None),
                    Err(e) => Err(format!("Error getting C++/Qt generation progress: {}", e)),
                }
            }
        }
    }

    if let Some(app) = app_weak.upgrade() {
        // Check if still running
        if !app.global::<AppState>().get_generate_is_running() {
            return; // Cancelled
        }

        match get_generate_files_result_helper(&app, &ctx, &operation_id) {
            Ok(Some(result)) => {
                // Generation complete
                log::info!(
                    "Generation complete: {} files generated",
                    result.files.len()
                );
                app.global::<AppState>().set_generate_is_running(false);
                app.global::<AppState>().set_generate_progress(1.0);
                app.global::<AppState>()
                    .set_generate_message(SharedString::from(
                        format!("Generated {} files", result.files.len()).as_str(),
                    ));
            }
            Ok(None) => {
                // Still running, poll again
                let ctx_clone = Arc::clone(&ctx);
                let app_weak_clone = app.as_weak();
                let op_id = operation_id.clone();

                // Update progress from backend
                if let Ok(Some(progress)) =
                    get_generate_files_progress_helper(&app, &ctx, &operation_id)
                {
                    app.global::<AppState>()
                        .set_generate_progress(progress.percentage / 100.0);
                    if let Some(msg) = progress.message {
                        app.global::<AppState>()
                            .set_generate_message(SharedString::from(msg));
                    }
                }

                slint::Timer::single_shot(std::time::Duration::from_millis(500), move || {
                    poll_generation_result(app_weak_clone, ctx_clone, op_id);
                });
            }
            Err(e) => {
                log::error!("Error checking generation result: {}", e);
                app.global::<AppState>().set_generate_is_running(false);
                app.global::<AppState>()
                    .set_error_message(SharedString::from(e.as_str()));
            }
        }
    }
}

fn setup_cancel_generate_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<GenerateCommands>().on_cancel_generate({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move || {
            log::info!("Cancel Generate clicked");
            if let Some(app) = app_weak.upgrade() {
                app.global::<AppState>().set_generate_is_running(false);
                app.global::<AppState>()
                    .set_generate_message(SharedString::from("Cancelled"));
                let _ = ctx;
            }
        }
    });
}

fn setup_cancel_fill_code_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<GenerateCommands>().on_cancel_fill_code({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move || {
            if let Some(app) = app_weak.upgrade() {
                let state = app.global::<AppState>();
                if !state.get_fill_code_is_running() {
                    return;
                }
                log::info!("Cancelling fill_code operation (leaving generate tab)");

                let op_id = state.get_fill_code_operation_id().to_string();
                state.set_fill_code_is_running(false);
                state.set_fill_code_operation_id(SharedString::default());

                if !op_id.is_empty() {
                    let lom = ctx.long_operation_manager.lock().unwrap();
                    lom.cancel_operation(&op_id);
                }
            }
        }
    });
}

fn setup_group_selected_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<AppState>().on_group_selected({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move |group_id| {
            log::info!("Group selected: id={}", group_id);

            if let Some(app) = app_weak.upgrade() {
                // Update group checkboxes: only the selected group is checked
                let group_list = app.global::<AppState>().get_group_cr_list();
                let mut updated_groups: Vec<ListItem> = Vec::new();
                for i in 0..group_list.row_count() {
                    if let Some(mut item) = group_list.row_data(i) {
                        item.checked = item.id == group_id;
                        updated_groups.push(item);
                    }
                }
                let group_model = std::rc::Rc::new(VecModel::from(updated_groups));
                app.global::<AppState>()
                    .set_group_cr_list(group_model.into());

                // Update selected group name for breadcrumb
                let group_names = app.global::<AppState>().get_group_list();
                if let Some(name) = group_names.row_data(group_id as usize) {
                    app.global::<AppState>().set_selected_group_name(name);
                }

                // Re-filter files from DB with new group selection
                update_file_display_from_db(&app, &ctx);
            }
        }
    });
}

fn setup_file_selected_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<AppState>().on_file_selected({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move |file_id| {
            log::info!("File selected: {}", file_id);
            if let Some(app) = app_weak.upgrade() {
                // Find the file name by ID (not by index) for breadcrumb
                let file_list = app.global::<AppState>().get_file_cr_list();
                for i in 0..file_list.row_count() {
                    if let Some(item) = file_list.row_data(i)
                        && item.id == file_id
                    {
                        app.global::<AppState>().set_selected_file_name(item.text);
                        break;
                    }
                }

                load_code_preview(&app, &ctx, file_id);
            }
        }
    });
}

fn setup_refresh_generate_tab_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<AppState>().on_refresh_generate_tab({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move || {
            log::info!("Refreshing generate tab");
            if let Some(app) = app_weak.upgrade() {
                refresh_file_lists(&app, &ctx);
            }
        }
    });
}

fn setup_file_filter_changed_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<AppState>().on_file_filter_changed({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move |_filter_text| {
            if let Some(app) = app_weak.upgrade() {
                update_file_display_from_db(&app, &ctx);
            }
        }
    });
}

fn setup_file_check_changed_callback(app: &App, _app_context: &Arc<AppContext>) {
    app.global::<AppState>().on_file_check_changed({
        let app_weak = app.as_weak();
        move |file_id, checked| {
            log::info!("File check changed: id={}, checked={}", file_id, checked);
            if let Some(app) = app_weak.upgrade() {
                let file_list = app.global::<AppState>().get_file_cr_list();
                let mut updated_files: Vec<ListItem> = Vec::new();

                for i in 0..file_list.row_count() {
                    if let Some(mut item) = file_list.row_data(i) {
                        if item.id == file_id {
                            item.checked = checked;
                        }
                        updated_files.push(item);
                    }
                }

                let selected_count = updated_files.iter().filter(|f| f.checked).count() as i32;
                let file_model = std::rc::Rc::new(VecModel::from(updated_files));
                app.global::<AppState>().set_file_cr_list(file_model.into());
                app.global::<AppState>()
                    .set_selected_files_count(selected_count);
            }
        }
    });
}

fn setup_select_all_files_callback(app: &App, _app_context: &Arc<AppContext>) {
    app.global::<AppState>().on_select_all_files({
        let app_weak = app.as_weak();
        move || {
            log::info!("Select All Files clicked");
            if let Some(app) = app_weak.upgrade() {
                let file_list = app.global::<AppState>().get_file_cr_list();
                let mut updated_files: Vec<ListItem> = Vec::new();

                for i in 0..file_list.row_count() {
                    if let Some(mut item) = file_list.row_data(i) {
                        item.checked = true;
                        updated_files.push(item);
                    }
                }

                let selected_count = updated_files.len() as i32;
                let file_model = std::rc::Rc::new(VecModel::from(updated_files));
                app.global::<AppState>().set_file_cr_list(file_model.into());
                app.global::<AppState>()
                    .set_selected_files_count(selected_count);
            }
        }
    });
}

fn setup_unselect_all_files_callback(app: &App, _app_context: &Arc<AppContext>) {
    app.global::<AppState>().on_unselect_all_files({
        let app_weak = app.as_weak();
        move || {
            log::info!("Unselect All Files clicked");
            if let Some(app) = app_weak.upgrade() {
                let file_list = app.global::<AppState>().get_file_cr_list();
                let mut updated_files: Vec<ListItem> = Vec::new();

                for i in 0..file_list.row_count() {
                    if let Some(mut item) = file_list.row_data(i) {
                        item.checked = false;
                        updated_files.push(item);
                    }
                }

                let file_model = std::rc::Rc::new(VecModel::from(updated_files));
                app.global::<AppState>().set_file_cr_list(file_model.into());
                app.global::<AppState>().set_selected_files_count(0);
            }
        }
    });
}

fn setup_status_filter_changed_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<AppState>().on_status_filter_changed({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move || {
            if let Some(app) = app_weak.upgrade() {
                update_file_display_from_db(&app, &ctx);
            }
        }
    });
}

fn setup_nature_filter_changed_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<AppState>().on_nature_filter_changed({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move || {
            if let Some(app) = app_weak.upgrade() {
                update_file_display_from_db(&app, &ctx);
            }
        }
    });
}

fn setup_view_diff_changed_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<AppState>().on_view_diff_changed({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move |_view_diff| {
            if let Some(app) = app_weak.upgrade() {
                let file_id = app.global::<AppState>().get_selected_file_index();
                if file_id >= 0 {
                    load_code_preview(&app, &ctx, file_id);
                }
            }
        }
    });
}

/// Initialize all generate tab related subscriptions and callbacks
pub fn init(_event_hub_client: &EventHubClient, app: &App, app_context: &Arc<AppContext>) {
    // Setup command callbacks
    setup_list_files_callback(app, app_context);
    setup_start_generate_callback(app, app_context);
    setup_cancel_generate_callback(app, app_context);
    setup_cancel_fill_code_callback(app, app_context);

    // Setup selection callbacks
    setup_group_selected_callback(app, app_context);
    setup_file_selected_callback(app, app_context);
    setup_refresh_generate_tab_callback(app, app_context);

    // Setup filter callbacks
    setup_file_check_changed_callback(app, app_context);
    setup_file_filter_changed_callback(app, app_context);
    setup_select_all_files_callback(app, app_context);
    setup_unselect_all_files_callback(app, app_context);
    setup_status_filter_changed_callback(app, app_context);
    setup_nature_filter_changed_callback(app, app_context);
    setup_view_diff_changed_callback(app, app_context);
}