bat-cli 0.13.2

Blockchain Auditor Toolkit (BAT)
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
use colored::Colorize;
use error_stack::{Report, ResultExt};
use reqwest::header::{AUTHORIZATION, CONTENT_TYPE};
use serde_json::{json, Value};
use std::collections::{HashSet, VecDeque};
use std::{error::Error, fmt};
use tokio::task::JoinSet;

use crate::batbelt::bat_dialoguer::BatDialoguer;
use crate::batbelt::evm::metadata::bat_metadata::{EvmBatMetadata, MiroFrameRef};
use crate::batbelt::miro::connector::create_connector_with_color;
use crate::batbelt::miro::frame::{
    MiroFrame, MIRO_BOARD_COLUMNS, MIRO_FRAME_HEIGHT, MIRO_FRAME_WIDTH, MIRO_INITIAL_X,
    MIRO_INITIAL_Y,
};
use crate::batbelt::miro::MiroConfig;
use crate::batbelt::parser::source_code_parser::{SourceCodeParser, SourceCodeScreenshotOptions};
use crate::config::BatConfig;

#[derive(Debug)]
pub struct EvmMiroError;

impl fmt::Display for EvmMiroError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("EvmMiro error")
    }
}

impl Error for EvmMiroError {}

pub type EvmMiroResult<T> = error_stack::Result<T, EvmMiroError>;

/// Pre-read config needed for Miro API calls.
#[derive(Clone)]
struct MiroApiConfig {
    access_token: String,
    board_id: String,
    board_url: String,
}

/// Result of a single frame deployment.
struct FrameDeployResult {
    entry_point_name: String,
    frame_id: String,
    frame_url: String,
}

/// Deploy code-overhaul frames for all EVM entry points (parallel).
pub async fn deploy_co_frames() -> EvmMiroResult<()> {
    MiroConfig::check_miro_enabled().change_context(EvmMiroError)?;

    // Read config ONCE before spawning any tasks
    let api_config = read_miro_config()?;

    let evm_metadata = EvmBatMetadata::read_metadata().change_context(EvmMiroError)?;
    let ep_names: Vec<String> = evm_metadata
        .entry_points
        .iter()
        .map(|ep| ep.name.clone())
        .collect();

    // Determine which frames need deploying (sequential — only checks existing)
    let mut to_deploy: Vec<(String, usize)> = Vec::new();
    for (idx, ep_name) in ep_names.iter().enumerate() {
        if let Some(existing) = evm_metadata.get_miro_frame_by_ep_name(ep_name) {
            // Verify frame still exists (single sequential check)
            if verify_frame_exists(&api_config, &existing.frame_id).await {
                println!(
                    "  {} already deployed: {}",
                    ep_name.green(),
                    existing.frame_url
                );
                continue;
            }
            println!("  {} stale frame, redeploying...", ep_name.bright_yellow());
        }
        to_deploy.push((ep_name.clone(), idx));
    }

    if to_deploy.is_empty() {
        println!("  All {} frames already deployed", ep_names.len());
        return Ok(());
    }

    println!("  Deploying {} frames in parallel...", to_deploy.len());

    // Launch all deployments in parallel — each task gets a clone of the config
    let mut join_set = JoinSet::new();
    for (ep_name, idx) in to_deploy {
        let config = api_config.clone();
        join_set.spawn(async move {
            let result = deploy_single_frame(&config, &ep_name, idx).await;
            (ep_name, result)
        });
    }

    // Collect results
    let mut deployed_frames: Vec<FrameDeployResult> = Vec::new();
    let mut errors: Vec<String> = Vec::new();

    while let Some(result) = join_set.join_next().await {
        match result {
            Ok((ep_name, Ok((frame_id, frame_url)))) => {
                println!("  {} deployed: {}", ep_name.green(), frame_url);
                deployed_frames.push(FrameDeployResult {
                    entry_point_name: ep_name,
                    frame_id,
                    frame_url,
                });
            }
            Ok((ep_name, Err(e))) => {
                errors.push(ep_name.clone());
                println!("  {} failed: {:?}", ep_name.red(), e);
            }
            Err(e) => {
                errors.push(format!("task panic: {}", e));
            }
        }
    }

    // Write all metadata at once (single atomic write, no race)
    if !deployed_frames.is_empty() {
        EvmBatMetadata::update_metadata(|metadata| {
            for frame in &deployed_frames {
                metadata
                    .miro
                    .frames
                    .retain(|f| f.entry_point_name != frame.entry_point_name);
                metadata.miro.frames.push(MiroFrameRef {
                    entry_point_name: frame.entry_point_name.clone(),
                    frame_id: frame.frame_id.clone(),
                    frame_url: frame.frame_url.clone(),
                    images_deployed: false,
                    entry_point_image_id: String::new(),
                    validations_image_id: String::new(),
                    dependency_image_ids: vec![],
                });
            }
        })
        .change_context(EvmMiroError)?;
    }

    if !errors.is_empty() {
        println!(
            "  {} {} frames failed to deploy",
            "Warning:".bright_yellow(),
            errors.len()
        );
    }

    Ok(())
}

/// Read Miro config once (access_token, board_id, board_url).
fn read_miro_config() -> EvmMiroResult<MiroApiConfig> {
    let bat_config = BatConfig::get_config().change_context(EvmMiroError)?;
    let bat_auditor_config =
        crate::config::BatAuditorConfig::get_config().change_context(EvmMiroError)?;
    let board_url = bat_config.miro_board_url.clone();
    let board_id = extract_board_id(&board_url)?;

    Ok(MiroApiConfig {
        access_token: bat_auditor_config.miro_oauth_access_token,
        board_id,
        board_url,
    })
}

/// Extract board ID from Miro board URL.
fn extract_board_id(board_url: &str) -> EvmMiroResult<String> {
    // URL format: https://miro.com/app/board/BOARD_ID=/
    board_url
        .split("/board/")
        .nth(1)
        .and_then(|s| s.split('/').next())
        .map(|s| s.trim_end_matches('=').to_string() + "=")
        .ok_or_else(|| {
            Report::new(EvmMiroError)
                .attach_printable(format!("Cannot extract board_id from URL: {}", board_url))
        })
}

/// Build frame URL from board URL and frame ID.
fn build_frame_url(board_url: &str, frame_id: &str) -> String {
    format!("{}/?moveToWidget={}", board_url, frame_id)
}

/// Verify a frame still exists on the board.
async fn verify_frame_exists(config: &MiroApiConfig, frame_id: &str) -> bool {
    let client = reqwest::Client::new();
    let resp = client
        .get(format!(
            "https://api.miro.com/v2/boards/{}/items/{}",
            config.board_id, frame_id
        ))
        .header(AUTHORIZATION, format!("Bearer {}", config.access_token))
        .send()
        .await;
    matches!(resp, Ok(r) if r.status().is_success())
}

/// Deploy a single frame and return (frame_id, frame_url).
async fn deploy_single_frame(
    config: &MiroApiConfig,
    entry_point_name: &str,
    index: usize,
) -> EvmMiroResult<(String, String)> {
    let frame_name = format!("co: {}", entry_point_name);
    let client = reqwest::Client::new();

    // Create frame at origin
    let response = client
        .post(format!(
            "https://api.miro.com/v2/boards/{}/frames",
            config.board_id
        ))
        .header(CONTENT_TYPE, "application/json")
        .header(AUTHORIZATION, format!("Bearer {}", config.access_token))
        .body(
            json!({
                "data": {
                    "format": "custom",
                    "title": frame_name,
                    "type": "freeform"
                },
                "position": {
                    "origin": "center",
                    "x": 0,
                    "y": 0
                },
                "geometry": {
                    "width": MIRO_FRAME_WIDTH,
                    "height": MIRO_FRAME_HEIGHT
                }
            })
            .to_string(),
        )
        .send()
        .await
        .map_err(|e| {
            Report::new(EvmMiroError).attach_printable(format!("HTTP error creating frame: {}", e))
        })?;

    let body = response.text().await.map_err(|e| {
        Report::new(EvmMiroError).attach_printable(format!("Cannot read response body: {}", e))
    })?;

    let value: Value = serde_json::from_str(&body).map_err(|e| {
        Report::new(EvmMiroError).attach_printable(format!("Cannot parse Miro response: {}", e))
    })?;

    let frame_id = value["id"]
        .as_str()
        .ok_or_else(|| Report::new(EvmMiroError).attach_printable("No 'id' in Miro response"))?
        .to_string();

    // Calculate grid position
    let x_modifier = index as i64 % MIRO_BOARD_COLUMNS;
    let y_modifier = index as i64 / MIRO_BOARD_COLUMNS;
    let x_position = MIRO_INITIAL_X + (MIRO_FRAME_WIDTH as i64 + 200) * x_modifier;
    let y_position = MIRO_INITIAL_Y + (MIRO_FRAME_HEIGHT as i64 + 400) * y_modifier;

    // Update position
    client
        .patch(format!(
            "https://api.miro.com/v2/boards/{}/frames/{}",
            config.board_id, frame_id
        ))
        .header(CONTENT_TYPE, "application/json")
        .header(AUTHORIZATION, format!("Bearer {}", config.access_token))
        .body(
            json!({
                "position": {
                    "x": x_position,
                    "y": y_position,
                    "origin": "center"
                }
            })
            .to_string(),
        )
        .send()
        .await
        .map_err(|e| {
            Report::new(EvmMiroError)
                .attach_printable(format!("HTTP error updating position: {}", e))
        })?;

    let frame_url = build_frame_url(&config.board_url, &frame_id);

    Ok((frame_id, frame_url))
}

/// Get sorted entry point names from EVM metadata (for selection prompts).
pub fn get_entry_point_names() -> EvmMiroResult<Vec<String>> {
    let evm_metadata = EvmBatMetadata::read_metadata().change_context(EvmMiroError)?;
    let mut names: Vec<String> = evm_metadata
        .entry_points
        .iter()
        .map(|ep| ep.name.clone())
        .collect();
    names.sort();
    Ok(names)
}

/// Find the closing brace of a Solidity function starting at `start_line` (1-based).
/// Uses brace-depth counting to handle nested blocks.
fn find_function_end_line(file_path: &str, start_line: usize) -> usize {
    let content = std::fs::read_to_string(file_path).unwrap_or_default();
    let lines: Vec<&str> = content.lines().collect();
    let total = lines.len();
    let start_idx = if start_line > 0 { start_line - 1 } else { 0 };

    let mut depth: i32 = 0;
    let mut found_open = false;

    for i in start_idx..total {
        for ch in lines[i].chars() {
            if ch == '{' {
                depth += 1;
                found_open = true;
            } else if ch == '}' {
                depth -= 1;
                if found_open && depth == 0 {
                    return i + 1; // 1-based
                }
            }
        }
    }
    // fallback: start + 20
    (start_line + 20).min(total)
}

/// Collect all contracts in the inheritance chain (self + base_contracts, recursively).
fn collect_inheritance_chain<'a>(
    evm_metadata: &'a EvmBatMetadata,
    contract_name: &str,
) -> Vec<&'a crate::batbelt::evm::metadata::bat_metadata::ContractMetadata> {
    let mut chain = Vec::new();
    let mut visited: HashSet<String> = HashSet::new();
    let mut queue: VecDeque<String> = VecDeque::new();
    queue.push_back(contract_name.to_string());

    while let Some(name) = queue.pop_front() {
        if visited.contains(&name) {
            continue;
        }
        visited.insert(name.clone());
        if let Some(c) = evm_metadata.get_contract_by_name(&name) {
            chain.push(c);
            for base in &c.base_contracts {
                queue.push_back(base.clone());
            }
        }
    }
    chain
}

/// Resolve direct dependencies for a function using pre-computed metadata from sonar
/// + modifier resolution from the inheritance chain.
/// Returns (metadata_id, name, file_path, line, end_line).
fn resolve_evm_function_deps(
    evm_metadata: &EvmBatMetadata,
    contract_name: &str,
    func_metadata_id: &str,
    func_modifiers: &[String],
    _func_lines: &[String],
    _contract_file_path: &str,
) -> Vec<(String, String, String, usize, usize)> {
    let mut deps: Vec<(String, String, String, usize, usize)> = Vec::new();
    let mut seen_names: HashSet<String> = HashSet::new();
    let mut seen_ids: HashSet<String> = HashSet::new();

    // Collect all contracts in the inheritance chain
    let chain = collect_inheritance_chain(evm_metadata, contract_name);

    // 1. Resolve modifiers as dependencies (search whole inheritance chain)
    for mod_name in func_modifiers {
        for c in &chain {
            if let Some(mod_def) = c.modifiers.iter().find(|m| m.name == *mod_name) {
                let mod_id = format!("modifier_{}_{}", c.name, mod_name);
                if !seen_ids.contains(&mod_id) {
                    seen_ids.insert(mod_id.clone());
                    seen_names.insert(mod_name.clone());
                    let end = find_function_end_line(&c.file_path, mod_def.line);
                    deps.push((
                        mod_id,
                        format!("modifier {}", mod_name),
                        c.file_path.clone(),
                        mod_def.line,
                        end,
                    ));
                }
                break;
            }
        }
    }

    // 2. Read pre-computed function dependencies from metadata (resolved during sonar)
    let called_names: HashSet<String> = evm_metadata
        .function_dependencies
        .iter()
        .find(|d| d.function_metadata_id == func_metadata_id)
        .map(|d| d.callees.iter().cloned().collect())
        .unwrap_or_default();

    for c in &chain {
        for func_meta in &c.functions {
            if func_meta.metadata_id == func_metadata_id {
                continue;
            }
            if seen_names.contains(&func_meta.name) {
                continue;
            }
            if called_names.contains(&func_meta.name) && !seen_ids.contains(&func_meta.metadata_id)
            {
                seen_ids.insert(func_meta.metadata_id.clone());
                seen_names.insert(func_meta.name.clone());
                let end = if func_meta.end_line > 0 {
                    func_meta.end_line
                } else {
                    find_function_end_line(&c.file_path, func_meta.line)
                };
                deps.push((
                    func_meta.metadata_id.clone(),
                    func_meta.name.clone(),
                    c.file_path.clone(),
                    func_meta.line,
                    end,
                ));
            }
        }
    }

    deps
}

/// Find the start of NatSpec/documentation comments above a function definition.
/// Walks backwards from `func_start_line` (1-based) to include `/** ... */` or `///` blocks.
fn find_doc_start_line(file_path: &str, func_start_line: usize) -> usize {
    let content = std::fs::read_to_string(file_path).unwrap_or_default();
    let lines: Vec<&str> = content.lines().collect();
    if func_start_line <= 1 {
        return func_start_line;
    }

    let mut doc_start = func_start_line; // 1-based
    let mut i = func_start_line - 2; // 0-based index of line above function

    // Skip blank lines immediately above the function
    while i < lines.len() {
        let trimmed = lines[i].trim();
        if trimmed.is_empty() {
            if i == 0 {
                break;
            }
            i -= 1;
        } else {
            break;
        }
    }

    if i >= lines.len() {
        return doc_start;
    }

    let trimmed = lines[i].trim();

    // Check for `*/` ending a block comment
    if trimmed.ends_with("*/") {
        // Walk backwards to find the `/**` or `/*`
        loop {
            doc_start = i + 1; // 1-based
            if lines[i].trim().starts_with("/**") || lines[i].trim().starts_with("/*") {
                break;
            }
            if i == 0 {
                break;
            }
            i -= 1;
        }
    } else if trimmed.starts_with("///") {
        // Walk backwards through consecutive `///` lines
        doc_start = i + 1;
        while i > 0 {
            i -= 1;
            if lines[i].trim().starts_with("///") {
                doc_start = i + 1;
            } else {
                break;
            }
        }
    }

    doc_start
}

/// Deploy source code screenshots for EVM projects with contract-based selection.
///
/// Flow: lib/src → contract → items (functions, vars, events, modifiers) → screenshot deploy.
pub async fn deploy_source_code_screenshots(selected_miro_frame: MiroFrame) -> EvmMiroResult<()> {
    let evm_metadata = EvmBatMetadata::read_metadata().change_context(EvmMiroError)?;

    let mut continue_selection = true;

    while continue_selection {
        // Step 1: lib or src
        let source_options = vec![
            "src (project contracts)".to_string(),
            "lib (external dependencies)".to_string(),
        ];
        let prompt_text = format!("Select {}", "source location".green());
        let source_selection =
            crate::batbelt::bat_dialoguer::select(&prompt_text, source_options, None).unwrap();
        let is_external = source_selection == 1;

        // Step 2: Filter contracts and select one
        let mut filtered_contracts: Vec<_> = evm_metadata
            .contracts
            .iter()
            .filter(|c| c.external == is_external)
            .collect();
        filtered_contracts.sort_by(|a, b| a.name.cmp(&b.name));

        if filtered_contracts.is_empty() {
            println!(
                "  {} No contracts found in {}",
                "Warning:".bright_yellow(),
                if is_external { "lib" } else { "src" }
            );
            continue_selection = crate::batbelt::bat_dialoguer::select_yes_or_no(&format!(
                "Do you want to {} in the {} frame?",
                "continue creating screenshots".yellow(),
                selected_miro_frame.title.yellow()
            ))
            .unwrap();
            continue;
        }

        let contract_names: Vec<String> = filtered_contracts
            .iter()
            .map(|c| format!("{} ({})", c.name, c.file_path))
            .collect();
        let prompt_text = format!("Select {}", "contract".green());
        let contract_selection =
            BatDialoguer::fuzzy_select(prompt_text, contract_names).change_context(EvmMiroError)?;
        let selected_contract = filtered_contracts[contract_selection];

        // Step 3: Build item list from the selected contract
        let mut item_labels: Vec<String> = Vec::new();

        // Track item type and index for later SourceCodeParser construction
        enum ContractItem {
            Function(usize),
            StorageVar(usize),
            Event(usize),
            Modifier(usize),
        }
        let mut item_refs: Vec<ContractItem> = Vec::new();

        for (i, func) in selected_contract.functions.iter().enumerate() {
            if func.is_constructor {
                continue;
            }
            item_labels.push(format!("[fn] {}", func.name));
            item_refs.push(ContractItem::Function(i));
        }
        for (i, var) in selected_contract.state_variables.iter().enumerate() {
            item_labels.push(format!("[var] {}", var.name));
            item_refs.push(ContractItem::StorageVar(i));
        }
        for (i, event) in selected_contract.events.iter().enumerate() {
            item_labels.push(format!("[event] {}", event.name));
            item_refs.push(ContractItem::Event(i));
        }
        for (i, modifier) in selected_contract.modifiers.iter().enumerate() {
            item_labels.push(format!("[mod] {}", modifier.name));
            item_refs.push(ContractItem::Modifier(i));
        }

        if item_labels.is_empty() {
            println!(
                "  {} No items found in contract {}",
                "Warning:".bright_yellow(),
                selected_contract.name
            );
            continue_selection = crate::batbelt::bat_dialoguer::select_yes_or_no(&format!(
                "Do you want to {} in the {} frame?",
                "continue creating screenshots".yellow(),
                selected_miro_frame.title.yellow()
            ))
            .unwrap();
            continue;
        }

        let prompt_text = format!(
            "Select {} from {}",
            "items to screenshot".green(),
            selected_contract.name.green()
        );
        let selections = BatDialoguer::multiselect(
            prompt_text,
            item_labels.clone(),
            Some(&vec![false; item_labels.len()]),
            true,
        )
        .unwrap();

        if selections.is_empty() {
            continue_selection = crate::batbelt::bat_dialoguer::select_yes_or_no(&format!(
                "Do you want to {} in the {} frame?",
                "continue creating screenshots".yellow(),
                selected_miro_frame.title.yellow()
            ))
            .unwrap();
            continue;
        }

        // Screenshot options (EVM defaults)
        let screenshot_options = SourceCodeScreenshotOptions {
            include_path: true,
            offset_to_start_line: true,
            filter_comments: false,
            font_size: None,
            filters: None,
            show_line_number: true,
        };

        // Step 5: Deploy screenshots
        for &sel_idx in &selections {
            let item = &item_refs[sel_idx];
            let (name, start_line, end_line) = match item {
                ContractItem::Function(i) => {
                    let f = &selected_contract.functions[*i];
                    let end = if f.end_line > 0 {
                        f.end_line
                    } else {
                        find_function_end_line(&selected_contract.file_path, f.line)
                    };
                    (
                        format!("{}.{}.js", selected_contract.name, f.name),
                        f.line,
                        end,
                    )
                }
                ContractItem::StorageVar(i) => {
                    let v = &selected_contract.state_variables[*i];
                    (
                        format!("{}.{}.js", selected_contract.name, v.name),
                        v.line,
                        v.line,
                    )
                }
                ContractItem::Event(i) => {
                    let e = &selected_contract.events[*i];
                    (
                        format!("{}.{}.js", selected_contract.name, e.name),
                        e.line,
                        e.line,
                    )
                }
                ContractItem::Modifier(i) => {
                    let m = &selected_contract.modifiers[*i];
                    let end = if m.end_line > 0 {
                        m.end_line
                    } else {
                        m.line + m.body_source.lines().count()
                    };
                    (
                        format!("{}.{}.js", selected_contract.name, m.name),
                        m.line,
                        end,
                    )
                }
            };

            // Strip .js from name, append frame tag, then add .js at the end
            // so silicon detects the extension correctly for syntax highlighting
            let base_name = name.trim_end_matches(".js");
            let screenshot_name = format!(
                "{}::frame={}.js",
                base_name,
                selected_miro_frame
                    .title
                    .replace([' ', '-'], "_")
                    .to_uppercase()
            );

            let sc = SourceCodeParser::new(
                screenshot_name,
                selected_contract.file_path.clone(),
                start_line,
                end_line,
            );

            sc.deploy_screenshot_to_miro_frame(
                selected_miro_frame.clone(),
                0,
                selected_miro_frame.height as i64,
                screenshot_options.clone(),
            )
            .await
            .change_context(EvmMiroError)?;
        }

        // Step 6: Continue?
        let prompt_text = format!(
            "Do you want to {} in the {} frame?",
            "continue creating screenshots".yellow(),
            selected_miro_frame.title.yellow()
        );
        continue_selection = crate::batbelt::bat_dialoguer::select_yes_or_no(&prompt_text).unwrap();
    }

    Ok(())
}

/// Deploy code-overhaul screenshots for a single EVM entry point into its Miro frame.
///
/// Deploys: entry point screenshot, validations screenshot (with header),
/// and BFS dependency screenshots (modifiers + internal calls) with connectors.
pub async fn deploy_co_screenshots(entry_point_name: &str) -> EvmMiroResult<()> {
    MiroConfig::check_miro_enabled().change_context(EvmMiroError)?;

    let evm_metadata = EvmBatMetadata::read_metadata().change_context(EvmMiroError)?;

    // Find frame ref
    let frame_ref = evm_metadata
        .get_miro_frame_by_ep_name(entry_point_name)
        .ok_or_else(|| {
            Report::new(EvmMiroError).attach_printable(format!(
                "No Miro frame found for '{}'. Run miro code-overhaul-frames first.",
                entry_point_name
            ))
        })?
        .clone();

    if frame_ref.images_deployed {
        println!(
            "  Screenshots already deployed for {}",
            entry_point_name.green()
        );
        return Ok(());
    }

    // Get the MiroFrame object
    let co_miro_frame = MiroFrame::new_from_item_id(&frame_ref.frame_id)
        .await
        .change_context(EvmMiroError)?;

    println!(
        "Deploying screenshots for {} to frame {}",
        entry_point_name.green(),
        co_miro_frame.title.green()
    );

    // Look up entry point and function metadata
    let ep = evm_metadata
        .get_entry_point_by_name(entry_point_name)
        .ok_or_else(|| {
            Report::new(EvmMiroError)
                .attach_printable(format!("Entry point '{}' not found", entry_point_name))
        })?
        .clone();

    let contract = evm_metadata
        .get_contract_by_name(&ep.contract_name)
        .ok_or_else(|| {
            Report::new(EvmMiroError)
                .attach_printable(format!("Contract '{}' not found", ep.contract_name))
        })?
        .clone();

    let func = evm_metadata
        .get_function_by_id(&ep.function_metadata_id)
        .ok_or_else(|| {
            Report::new(EvmMiroError)
                .attach_printable(format!("Function '{}' not found", ep.function_metadata_id))
        })?
        .clone();

    // 1. Deploy entry point function screenshot at (1680, 260)
    // Include NatSpec documentation above the function
    let ep_start_line = find_doc_start_line(&contract.file_path, func.line);
    let ep_end_line = if func.end_line > 0 {
        func.end_line
    } else {
        find_function_end_line(&contract.file_path, func.line)
    };

    // Use .js extension so silicon renders with JavaScript syntax highlighting
    // (Dracula + JS gives the best color contrast for Solidity code)
    let ep_sc = SourceCodeParser::new(
        format!("ep_{}.js", func.name),
        contract.file_path.clone(),
        ep_start_line,
        ep_end_line,
    );

    let ep_image = ep_sc
        .deploy_screenshot_to_miro_frame(
            co_miro_frame.clone(),
            1200,
            600,
            SourceCodeScreenshotOptions {
                include_path: true,
                offset_to_start_line: true,
                filter_comments: false,
                font_size: Some(28),
                filters: None,
                show_line_number: true,
            },
        )
        .await
        .change_context(EvmMiroError)?;

    let entry_point_image_id = ep_image.item_id.clone();

    // 2. BFS deployment of dependency screenshots
    // Resolve deps from function body + modifiers (not from function_dependencies which may be empty)
    // Track both IDs and names to prevent deploying virtual parent versions of overridden functions
    let mut deployed_function_ids: HashSet<String> = HashSet::new();
    deployed_function_ids.insert(ep.function_metadata_id.clone());
    let mut deployed_function_names: HashSet<String> = HashSet::new();
    deployed_function_names.insert(func.name.clone());

    let mut id_to_image: std::collections::HashMap<String, String> =
        std::collections::HashMap::new();
    id_to_image.insert(
        ep.function_metadata_id.clone(),
        entry_point_image_id.clone(),
    );

    let mut bfs_queue: VecDeque<(String, String, Vec<String>, Vec<String>, String)> =
        VecDeque::new();

    // Read body lines for the entry point function
    let ep_body_lines: Vec<String> = {
        let file_content = std::fs::read_to_string(&contract.file_path).unwrap_or_default();
        let lines: Vec<&str> = file_content.lines().collect();
        let start = if func.line > 0 { func.line - 1 } else { 0 };
        let end = ep_end_line.min(lines.len());
        lines[start..end].iter().map(|s| s.to_string()).collect()
    };

    bfs_queue.push_back((
        ep.function_metadata_id.clone(),
        func.name.clone(),
        func.modifiers.clone(),
        ep_body_lines,
        contract.file_path.clone(),
    ));

    let mut dependency_image_ids: Vec<String> = Vec::new();

    const CASCADE_START_X: i64 = 2400;
    const CASCADE_START_Y: i64 = 900;
    const CASCADE_STEP: i64 = 60;

    const DEP_ARROW_COLORS: &[(&str, &str)] = &[
        ("#ff0000", "red"),
        ("#0000ff", "blue"),
        ("#00aa00", "green"),
        ("#ff8800", "orange"),
        ("#aa00ff", "purple"),
        ("#00cccc", "teal"),
        ("#ff00aa", "pink"),
        ("#888800", "olive"),
    ];
    let mut color_index: usize = 0;

    while let Some((caller_id, caller_name, caller_modifiers, caller_body, caller_file)) =
        bfs_queue.pop_front()
    {
        // Resolve deps dynamically from body + modifiers
        let new_dep_functions = resolve_evm_function_deps(
            &evm_metadata,
            &ep.contract_name,
            &caller_id,
            &caller_modifiers,
            &caller_body,
            &caller_file,
        );

        // Filter already deployed (by ID or by name to prevent virtual/override duplicates)
        let new_deps: Vec<_> = new_dep_functions
            .into_iter()
            .filter(|(id, name, _, _, _)| {
                !deployed_function_ids.contains(id) && !deployed_function_names.contains(name)
            })
            .collect();

        if new_deps.is_empty() {
            continue;
        }

        let (arrow_hex, arrow_name) = DEP_ARROW_COLORS[color_index % DEP_ARROW_COLORS.len()];
        color_index += 1;

        let prompt = format!(
            "Press Enter to deploy {} dependencies of `{}` (arrow color: {})",
            new_deps.len(),
            caller_name,
            arrow_name
        );
        BatDialoguer::input_with_default(prompt, "".to_string()).change_context(EvmMiroError)?;

        for (idx, (dep_id, dep_name, dep_path, dep_line, dep_end)) in new_deps.iter().enumerate() {
            // .js extension → JavaScript syntax highlighting in silicon (best for Solidity)
            let dep_sc = SourceCodeParser::new(
                format!("dep_{}.js", dep_name),
                dep_path.clone(),
                *dep_line,
                *dep_end,
            );

            let cascade_x = CASCADE_START_X + (idx as i64) * CASCADE_STEP;
            let cascade_y = CASCADE_START_Y + (idx as i64) * CASCADE_STEP;

            let dep_image = dep_sc
                .deploy_screenshot_to_miro_frame(
                    co_miro_frame.clone(),
                    cascade_x,
                    cascade_y,
                    SourceCodeScreenshotOptions {
                        include_path: true,
                        offset_to_start_line: true,
                        filter_comments: false,
                        font_size: Some(16),
                        filters: None,
                        show_line_number: true,
                    },
                )
                .await
                .change_context(EvmMiroError)?;

            // Arrow from dep to caller
            if let Some(caller_image_id) = id_to_image.get(&caller_id) {
                create_connector_with_color(
                    &dep_image.item_id,
                    caller_image_id,
                    None,
                    Some(arrow_hex),
                )
                .await
                .change_context(EvmMiroError)?;
            }

            id_to_image.insert(dep_id.clone(), dep_image.item_id.clone());
            deployed_function_ids.insert(dep_id.clone());
            // Store the base name (strip "modifier " prefix) so virtual/override
            // and modifier-as-function duplicates are caught
            let base_name = dep_name.strip_prefix("modifier ").unwrap_or(dep_name);
            deployed_function_names.insert(base_name.to_string());
            dependency_image_ids.push(dep_image.item_id.clone());

            // Read body of dep for further BFS (only for non-modifier deps)
            let dep_body: Vec<String> = {
                let fc = std::fs::read_to_string(dep_path).unwrap_or_default();
                let lines: Vec<&str> = fc.lines().collect();
                let s = if *dep_line > 0 { dep_line - 1 } else { 0 };
                let e = (*dep_end).min(lines.len());
                lines[s..e].iter().map(|l| l.to_string()).collect()
            };
            // Dep functions don't have modifiers in this context (we'd need to look them up)
            bfs_queue.push_back((
                dep_id.clone(),
                dep_name.clone(),
                vec![],
                dep_body,
                dep_path.clone(),
            ));
        }
    }

    // Update metadata with deployed image IDs
    EvmBatMetadata::update_metadata(|metadata| {
        if let Some(frame) = metadata
            .miro
            .frames
            .iter_mut()
            .find(|f| f.entry_point_name == entry_point_name)
        {
            frame.images_deployed = true;
            frame.entry_point_image_id = entry_point_image_id.clone();
            frame.validations_image_id = String::new();
            frame.dependency_image_ids = dependency_image_ids.clone();
        }
    })
    .change_context(EvmMiroError)?;

    // Update CO file with Miro frame URL
    if let Some(ref frame_url) = co_miro_frame.frame_url {
        let co_file_name = format!("{}.md", entry_point_name);
        let co_bat_file = crate::batbelt::path::BatFile::CodeOverhaulStarted {
            file_name: co_file_name,
            program_name: None,
        };
        if let Ok(content) = co_bat_file.read_content(true) {
            let placeholder = "`COMPLETE_WITH_MIRO_FRAME_URL`";
            if content.contains(placeholder) {
                let new_content = content.replace(placeholder, frame_url);
                let _ = co_bat_file.write_content(true, &new_content);
            }
        }
    }

    println!("  Screenshots deployed for {}", entry_point_name.green());

    Ok(())
}