reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
//! Example: WebBrowserAdapter Trait Usage
//!
//! This example demonstrates comprehensive usage of the WebBrowserAdapter trait
//! from the traits module. It shows:
//!
//! - Creating mock implementations
//! - Navigation and page lifecycle
//! - Content extraction patterns
//! - Screenshot and capture operations
//! - Browser interaction (click, type, scroll)
//! - JavaScript evaluation
//! - Cookie and storage management
//! - Error handling patterns
//! - Both sync and async usage patterns
//!
//! # Running this example
//!
//! ```bash
//! cargo run --example web_adapter_example
//! ```
//!
//! # Architecture
//!
//! The WebBrowserAdapter trait defines the contract between reasonkit-core and
//! reasonkit-web. This allows reasonkit-core to work with any browser backend
//! that implements the trait (Chromium, Firefox, etc.).
//!
//! ```text
//! reasonkit-core (consumer)
//!        |
//!        v
//! WebBrowserAdapter trait <-- defined in reasonkit-core/src/traits/web.rs
//!        ^
//!        |
//! reasonkit-web (implementation)
//!        |
//!        v
//! Browser Engine (Chromium via CDP, Playwright, etc.)
//! ```

use async_trait::async_trait;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::Arc;
use std::time::Duration;

// Import from the traits module
use reasonkit::traits::{
    CaptureFormat, CaptureOptions, CapturedPage, ClipRect, Cookie, ExtractFormat, ExtractOptions,
    ExtractedContent, Image, Link, NavigateOptions, PageHandle, Viewport, WaitUntil,
    WebAdapterError, WebAdapterResult, WebBrowserAdapter,
};

// ============================================================================
// MOCK IMPLEMENTATION
// ============================================================================

/// Mock implementation of WebBrowserAdapter for demonstration.
///
/// In production, you would use the implementation from reasonkit-web.
/// This mock is useful for:
/// - Testing without a real browser
/// - Understanding the trait interface
/// - Prototyping before integration
struct MockBrowserAdapter {
    connected: AtomicBool,
    page_counter: AtomicU32,
    pages: Arc<std::sync::RwLock<HashMap<String, MockPage>>>,
    cookies: Arc<std::sync::RwLock<Vec<Cookie>>>,
    local_storage: Arc<std::sync::RwLock<HashMap<String, String>>>,
}

/// Internal representation of a mock page.
#[derive(Clone)]
struct MockPage {
    url: String,
    title: String,
    html: String,
    status_code: u16,
    history: Vec<String>,
    history_index: usize,
}

impl MockBrowserAdapter {
    /// Create a new mock browser adapter.
    fn new() -> Self {
        Self {
            connected: AtomicBool::new(false),
            page_counter: AtomicU32::new(0),
            pages: Arc::new(std::sync::RwLock::new(HashMap::new())),
            cookies: Arc::new(std::sync::RwLock::new(Vec::new())),
            local_storage: Arc::new(std::sync::RwLock::new(HashMap::new())),
        }
    }

    /// Generate a unique page ID.
    fn next_page_id(&self) -> String {
        let id = self.page_counter.fetch_add(1, Ordering::SeqCst);
        format!("page-{}", id)
    }

    /// Create a mock page for a URL.
    fn create_mock_page(&self, url: &str) -> MockPage {
        let title = if url.contains("example.com") {
            "Example Domain"
        } else if url.contains("github.com") {
            "GitHub"
        } else {
            "Mock Page"
        };

        MockPage {
            url: url.to_string(),
            title: title.to_string(),
            html: format!(
                r#"<!DOCTYPE html>
<html>
<head><title>{}</title></head>
<body>
    <h1>{}</h1>
    <p>This is mock content for {}</p>
    <a href="https://example.com/link1">Link 1</a>
    <a href="https://example.com/link2">Link 2</a>
    <img src="https://example.com/image.png" alt="Mock image" />
    <form>
        <input type="text" id="search" name="q" />
        <button type="submit">Search</button>
    </form>
</body>
</html>"#,
                title, title, url
            ),
            status_code: 200,
            history: vec![url.to_string()],
            history_index: 0,
        }
    }

    /// Extract mock text content from HTML.
    fn extract_text(&self, html: &str) -> String {
        // Simple mock extraction - remove tags
        let mut text = html.to_string();
        // Remove script and style tags with content
        let re_script = regex::Regex::new(r"<script[^>]*>[\s\S]*?</script>").unwrap();
        let re_style = regex::Regex::new(r"<style[^>]*>[\s\S]*?</style>").unwrap();
        text = re_script.replace_all(&text, "").to_string();
        text = re_style.replace_all(&text, "").to_string();
        // Remove remaining tags
        let re_tags = regex::Regex::new(r"<[^>]+>").unwrap();
        text = re_tags.replace_all(&text, " ").to_string();
        // Normalize whitespace
        text.split_whitespace().collect::<Vec<_>>().join(" ")
    }
}

#[async_trait]
impl WebBrowserAdapter for MockBrowserAdapter {
    // -------------------------------------------------------------------------
    // Lifecycle
    // -------------------------------------------------------------------------

    async fn connect(&mut self) -> WebAdapterResult<()> {
        println!("[MockBrowser] Connecting to browser...");
        tokio::time::sleep(Duration::from_millis(100)).await;
        self.connected.store(true, Ordering::SeqCst);
        println!("[MockBrowser] Connected successfully");
        Ok(())
    }

    async fn disconnect(&mut self) -> WebAdapterResult<()> {
        println!("[MockBrowser] Disconnecting from browser...");
        self.connected.store(false, Ordering::SeqCst);
        self.pages.write().unwrap().clear();
        Ok(())
    }

    fn is_connected(&self) -> bool {
        self.connected.load(Ordering::SeqCst)
    }

    // -------------------------------------------------------------------------
    // Navigation
    // -------------------------------------------------------------------------

    async fn navigate(&self, url: &str, options: NavigateOptions) -> WebAdapterResult<PageHandle> {
        if !self.is_connected() {
            return Err(WebAdapterError::NotConnected);
        }

        // Validate URL
        if !url.starts_with("http://") && !url.starts_with("https://") {
            return Err(WebAdapterError::Navigation(format!("Invalid URL: {}", url)));
        }

        println!(
            "[MockBrowser] Navigating to: {} (wait: {:?}, timeout: {:?})",
            url, options.wait_until, options.timeout
        );

        // Simulate load time
        tokio::time::sleep(Duration::from_millis(50)).await;

        let page_id = self.next_page_id();
        let mock_page = self.create_mock_page(url);

        // Store page
        self.pages
            .write()
            .unwrap()
            .insert(page_id.clone(), mock_page.clone());

        Ok(PageHandle {
            id: page_id,
            url: url.to_string(),
            title: Some(mock_page.title),
            status_code: mock_page.status_code,
            load_time_ms: 50,
        })
    }

    async fn wait_for_load(&self, handle: &PageHandle, timeout: Duration) -> WebAdapterResult<()> {
        println!(
            "[MockBrowser] Waiting for page {} to load (timeout: {:?})",
            handle.id, timeout
        );
        tokio::time::sleep(Duration::from_millis(10)).await;
        Ok(())
    }

    async fn go_back(&self, handle: &PageHandle) -> WebAdapterResult<()> {
        println!("[MockBrowser] Going back from page {}", handle.id);
        let mut pages = self.pages.write().unwrap();
        if let Some(page) = pages.get_mut(&handle.id) {
            if page.history_index > 0 {
                page.history_index -= 1;
                page.url = page.history[page.history_index].clone();
            }
        }
        Ok(())
    }

    async fn go_forward(&self, handle: &PageHandle) -> WebAdapterResult<()> {
        println!("[MockBrowser] Going forward from page {}", handle.id);
        let mut pages = self.pages.write().unwrap();
        if let Some(page) = pages.get_mut(&handle.id) {
            if page.history_index < page.history.len() - 1 {
                page.history_index += 1;
                page.url = page.history[page.history_index].clone();
            }
        }
        Ok(())
    }

    async fn reload(&self, handle: &PageHandle) -> WebAdapterResult<()> {
        println!("[MockBrowser] Reloading page {}", handle.id);
        tokio::time::sleep(Duration::from_millis(25)).await;
        Ok(())
    }

    // -------------------------------------------------------------------------
    // Content Extraction
    // -------------------------------------------------------------------------

    async fn extract_content(
        &self,
        handle: &PageHandle,
        options: ExtractOptions,
    ) -> WebAdapterResult<ExtractedContent> {
        if !self.is_connected() {
            return Err(WebAdapterError::NotConnected);
        }

        println!(
            "[MockBrowser] Extracting content from {} (format: {:?})",
            handle.id, options.format
        );

        let pages = self.pages.read().unwrap();
        let page = pages
            .get(&handle.id)
            .ok_or_else(|| WebAdapterError::Navigation("Page not found".to_string()))?;

        let text = self.extract_text(&page.html);
        let formatted_text = match options.format {
            ExtractFormat::PlainText => text.clone(),
            ExtractFormat::Markdown => format!("# {}\n\n{}", page.title, text),
            ExtractFormat::Html => page.html.clone(),
            ExtractFormat::Json => json!({
                "title": page.title,
                "text": text
            })
            .to_string(),
        };

        let links = if options.include_links {
            vec![
                Link {
                    text: "Link 1".to_string(),
                    href: "https://example.com/link1".to_string(),
                    rel: None,
                },
                Link {
                    text: "Link 2".to_string(),
                    href: "https://example.com/link2".to_string(),
                    rel: Some("nofollow".to_string()),
                },
            ]
        } else {
            vec![]
        };

        let images = if options.include_images {
            vec![Image {
                src: "https://example.com/image.png".to_string(),
                alt: Some("Mock image".to_string()),
                width: Some(800),
                height: Some(600),
            }]
        } else {
            vec![]
        };

        Ok(ExtractedContent {
            text: formatted_text,
            format: options.format,
            title: Some(page.title.clone()),
            description: Some("A mock page description".to_string()),
            author: None,
            published_date: None,
            word_count: text.split_whitespace().count(),
            links,
            images,
            metadata: json!({
                "url": page.url,
                "status_code": page.status_code
            }),
        })
    }

    async fn extract_links(&self, handle: &PageHandle) -> WebAdapterResult<Vec<Link>> {
        println!("[MockBrowser] Extracting links from {}", handle.id);
        Ok(vec![Link {
            text: "Example Link".to_string(),
            href: "https://example.com".to_string(),
            rel: None,
        }])
    }

    async fn extract_structured(
        &self,
        handle: &PageHandle,
        selector: &str,
    ) -> WebAdapterResult<Value> {
        println!(
            "[MockBrowser] Extracting structured data from {} using selector: {}",
            handle.id, selector
        );
        Ok(json!({
            "selector": selector,
            "elements": [
                {"text": "Element 1", "class": "item"},
                {"text": "Element 2", "class": "item"}
            ]
        }))
    }

    async fn get_html(&self, handle: &PageHandle) -> WebAdapterResult<String> {
        let pages = self.pages.read().unwrap();
        let page = pages
            .get(&handle.id)
            .ok_or_else(|| WebAdapterError::Navigation("Page not found".to_string()))?;
        Ok(page.html.clone())
    }

    // -------------------------------------------------------------------------
    // Capture
    // -------------------------------------------------------------------------

    async fn capture_screenshot(
        &self,
        handle: &PageHandle,
        options: CaptureOptions,
    ) -> WebAdapterResult<CapturedPage> {
        if !self.is_connected() {
            return Err(WebAdapterError::NotConnected);
        }

        println!(
            "[MockBrowser] Capturing screenshot of {} (format: {:?}, full_page: {})",
            handle.id, options.format, options.full_page
        );

        // Mock image data (minimal PNG header)
        let data = match options.format {
            CaptureFormat::Png => vec![
                0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, // IHDR chunk
                0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
            ],
            CaptureFormat::Jpeg => vec![0xFF, 0xD8, 0xFF, 0xE0],
            CaptureFormat::Webp => vec![0x52, 0x49, 0x46, 0x46],
        };

        Ok(CapturedPage {
            handle: handle.clone(),
            format: options.format,
            data,
            width: 1920,
            height: if options.full_page { 3000 } else { 1080 },
        })
    }

    async fn capture_pdf(&self, handle: &PageHandle) -> WebAdapterResult<Vec<u8>> {
        println!("[MockBrowser] Capturing PDF of {}", handle.id);
        // Mock PDF header
        Ok(vec![0x25, 0x50, 0x44, 0x46, 0x2D, 0x31, 0x2E, 0x34])
    }

    // -------------------------------------------------------------------------
    // Interaction
    // -------------------------------------------------------------------------

    async fn click(&self, handle: &PageHandle, selector: &str) -> WebAdapterResult<()> {
        println!("[MockBrowser] Clicking {} on {}", selector, handle.id);
        tokio::time::sleep(Duration::from_millis(10)).await;
        Ok(())
    }

    async fn type_text(
        &self,
        handle: &PageHandle,
        selector: &str,
        text: &str,
    ) -> WebAdapterResult<()> {
        println!(
            "[MockBrowser] Typing '{}' into {} on {}",
            text, selector, handle.id
        );
        tokio::time::sleep(Duration::from_millis(text.len() as u64 * 5)).await;
        Ok(())
    }

    async fn select_option(
        &self,
        handle: &PageHandle,
        selector: &str,
        value: &str,
    ) -> WebAdapterResult<()> {
        println!(
            "[MockBrowser] Selecting '{}' in {} on {}",
            value, selector, handle.id
        );
        Ok(())
    }

    async fn scroll(&self, handle: &PageHandle, x: f64, y: f64) -> WebAdapterResult<()> {
        println!("[MockBrowser] Scrolling to ({}, {}) on {}", x, y, handle.id);
        Ok(())
    }

    async fn wait_for_selector(
        &self,
        handle: &PageHandle,
        selector: &str,
        timeout: Duration,
    ) -> WebAdapterResult<()> {
        println!(
            "[MockBrowser] Waiting for selector {} on {} (timeout: {:?})",
            selector, handle.id, timeout
        );
        tokio::time::sleep(Duration::from_millis(20)).await;
        Ok(())
    }

    // -------------------------------------------------------------------------
    // JavaScript
    // -------------------------------------------------------------------------

    async fn evaluate_js(&self, handle: &PageHandle, script: &str) -> WebAdapterResult<Value> {
        println!(
            "[MockBrowser] Evaluating JS on {}: {}",
            handle.id,
            &script[..script.len().min(50)]
        );

        // Return mock result based on script content
        if script.contains("document.title") {
            Ok(json!("Mock Page Title"))
        } else if script.contains("querySelectorAll") {
            Ok(json!(5))
        } else {
            Ok(json!({"success": true, "result": "mock_value"}))
        }
    }

    async fn inject_script(&self, handle: &PageHandle, script: &str) -> WebAdapterResult<()> {
        println!(
            "[MockBrowser] Injecting script into {}: {} chars",
            handle.id,
            script.len()
        );
        Ok(())
    }

    // -------------------------------------------------------------------------
    // Cookies & Storage
    // -------------------------------------------------------------------------

    async fn get_cookies(&self, _handle: &PageHandle) -> WebAdapterResult<Vec<Cookie>> {
        let cookies = self.cookies.read().unwrap().clone();
        Ok(cookies)
    }

    async fn set_cookie(&self, _handle: &PageHandle, cookie: Cookie) -> WebAdapterResult<()> {
        println!("[MockBrowser] Setting cookie: {}", cookie.name);
        self.cookies.write().unwrap().push(cookie);
        Ok(())
    }

    async fn clear_cookies(&self, _handle: &PageHandle) -> WebAdapterResult<()> {
        println!("[MockBrowser] Clearing all cookies");
        self.cookies.write().unwrap().clear();
        Ok(())
    }

    async fn get_local_storage(
        &self,
        _handle: &PageHandle,
        key: &str,
    ) -> WebAdapterResult<Option<String>> {
        let storage = self.local_storage.read().unwrap();
        Ok(storage.get(key).cloned())
    }

    async fn set_local_storage(
        &self,
        _handle: &PageHandle,
        key: &str,
        value: &str,
    ) -> WebAdapterResult<()> {
        println!("[MockBrowser] Setting localStorage: {} = {}", key, value);
        self.local_storage
            .write()
            .unwrap()
            .insert(key.to_string(), value.to_string());
        Ok(())
    }
}

// ============================================================================
// DEMONSTRATION FUNCTIONS
// ============================================================================

/// Demonstrates navigation and page lifecycle.
async fn demo_navigation(browser: &impl WebBrowserAdapter) -> WebAdapterResult<()> {
    println!("\n--- Navigation Demo ---\n");

    // 1. Basic navigation
    println!("1. Basic navigation...");
    let page = browser
        .navigate("https://example.com", NavigateOptions::default())
        .await?;
    println!("   Page ID: {}", page.id);
    println!("   URL: {}", page.url);
    println!("   Title: {:?}", page.title);
    println!("   Status: {}", page.status_code);
    println!("   Load time: {}ms", page.load_time_ms);

    // 2. Navigation with options
    println!("\n2. Navigation with custom options...");
    let options = NavigateOptions {
        wait_until: WaitUntil::NetworkIdle,
        timeout: Duration::from_secs(60),
        user_agent: Some("ReasonKit/1.0".to_string()),
        headers: vec![("Accept-Language".to_string(), "en-US".to_string())],
        viewport: Some(Viewport {
            width: 1920,
            height: 1080,
            device_scale_factor: 2.0,
            is_mobile: false,
        }),
    };
    let page2 = browser.navigate("https://github.com", options).await?;
    println!("   Navigated to: {}", page2.url);

    // 3. Wait for load
    println!("\n3. Waiting for page load...");
    browser
        .wait_for_load(&page, Duration::from_secs(30))
        .await?;
    println!("   Page loaded");

    // 4. History navigation
    println!("\n4. History navigation...");
    browser.go_back(&page).await?;
    println!("   Went back");
    browser.go_forward(&page).await?;
    println!("   Went forward");
    browser.reload(&page).await?;
    println!("   Reloaded");

    Ok(())
}

/// Demonstrates content extraction patterns.
async fn demo_content_extraction(browser: &impl WebBrowserAdapter) -> WebAdapterResult<()> {
    println!("\n--- Content Extraction Demo ---\n");

    let page = browser
        .navigate("https://example.com/article", NavigateOptions::default())
        .await?;

    // 1. Plain text extraction
    println!("1. Plain text extraction...");
    let content = browser
        .extract_content(
            &page,
            ExtractOptions {
                format: ExtractFormat::PlainText,
                include_metadata: true,
                clean_html: true,
                include_links: false,
                include_images: false,
                max_length: None,
            },
        )
        .await?;
    println!("   Title: {:?}", content.title);
    println!("   Word count: {}", content.word_count);
    println!(
        "   Text preview: {}...",
        &content.text[..content.text.len().min(80)]
    );

    // 2. Markdown extraction
    println!("\n2. Markdown extraction...");
    let content = browser
        .extract_content(
            &page,
            ExtractOptions {
                format: ExtractFormat::Markdown,
                include_links: true,
                include_images: true,
                ..Default::default()
            },
        )
        .await?;
    println!("   Links found: {}", content.links.len());
    println!("   Images found: {}", content.images.len());

    // 3. Extract links only
    println!("\n3. Extracting links...");
    let links = browser.extract_links(&page).await?;
    for link in &links {
        println!("   - {} -> {}", link.text, link.href);
    }

    // 4. Structured data extraction
    println!("\n4. Structured data extraction...");
    let structured = browser.extract_structured(&page, ".product-card").await?;
    println!(
        "   Result: {}",
        serde_json::to_string_pretty(&structured)
            .unwrap_or_else(|_| "<failed to serialize structured result>".to_string())
    );

    // 5. Raw HTML
    println!("\n5. Getting raw HTML...");
    let html = browser.get_html(&page).await?;
    println!("   HTML length: {} chars", html.len());

    Ok(())
}

/// Demonstrates screenshot and capture operations.
async fn demo_capture(browser: &impl WebBrowserAdapter) -> WebAdapterResult<()> {
    println!("\n--- Capture Demo ---\n");

    let page = browser
        .navigate("https://example.com", NavigateOptions::default())
        .await?;

    // 1. PNG screenshot (full page)
    println!("1. PNG screenshot (full page)...");
    let screenshot = browser
        .capture_screenshot(
            &page,
            CaptureOptions {
                format: CaptureFormat::Png,
                quality: 100,
                full_page: true,
                clip: None,
            },
        )
        .await?;
    println!("   Format: {:?}", screenshot.format);
    println!("   Size: {}x{}", screenshot.width, screenshot.height);
    println!("   Data size: {} bytes", screenshot.data.len());

    // 2. JPEG screenshot (viewport only)
    println!("\n2. JPEG screenshot (viewport only)...");
    let screenshot = browser
        .capture_screenshot(
            &page,
            CaptureOptions {
                format: CaptureFormat::Jpeg,
                quality: 85,
                full_page: false,
                clip: None,
            },
        )
        .await?;
    println!("   Size: {}x{}", screenshot.width, screenshot.height);

    // 3. Screenshot with clipping
    println!("\n3. Screenshot with clipping...");
    let _screenshot = browser
        .capture_screenshot(
            &page,
            CaptureOptions {
                format: CaptureFormat::Png,
                quality: 100,
                full_page: false,
                clip: Some(ClipRect {
                    x: 0.0,
                    y: 0.0,
                    width: 800.0,
                    height: 600.0,
                }),
            },
        )
        .await?;
    println!("   Clipped screenshot captured");

    // 4. PDF capture
    println!("\n4. PDF capture...");
    let pdf_data = browser.capture_pdf(&page).await?;
    println!("   PDF size: {} bytes", pdf_data.len());

    Ok(())
}

/// Demonstrates browser interaction operations.
async fn demo_interaction(browser: &impl WebBrowserAdapter) -> WebAdapterResult<()> {
    println!("\n--- Interaction Demo ---\n");

    let page = browser
        .navigate("https://example.com/form", NavigateOptions::default())
        .await?;

    // 1. Click element
    println!("1. Clicking element...");
    browser.click(&page, "#submit-button").await?;
    println!("   Clicked #submit-button");

    // 2. Type text
    println!("\n2. Typing text...");
    browser
        .type_text(&page, "#search-input", "ReasonKit search query")
        .await?;
    println!("   Typed into #search-input");

    // 3. Select option
    println!("\n3. Selecting dropdown option...");
    browser
        .select_option(&page, "#country-select", "US")
        .await?;
    println!("   Selected 'US' in dropdown");

    // 4. Scroll
    println!("\n4. Scrolling page...");
    browser.scroll(&page, 0.0, 500.0).await?;
    println!("   Scrolled to (0, 500)");

    // 5. Wait for element
    println!("\n5. Waiting for element...");
    browser
        .wait_for_selector(&page, ".dynamic-content", Duration::from_secs(10))
        .await?;
    println!("   Element appeared");

    Ok(())
}

/// Demonstrates JavaScript evaluation.
async fn demo_javascript(browser: &impl WebBrowserAdapter) -> WebAdapterResult<()> {
    println!("\n--- JavaScript Demo ---\n");

    let page = browser
        .navigate("https://example.com", NavigateOptions::default())
        .await?;

    // 1. Simple evaluation
    println!("1. Simple JS evaluation...");
    let result = browser.evaluate_js(&page, "document.title").await?;
    println!("   document.title = {}", result);

    // 2. Complex evaluation
    println!("\n2. Complex JS evaluation...");
    let result = browser
        .evaluate_js(&page, "document.querySelectorAll('a').length")
        .await?;
    println!("   Link count = {}", result);

    // 3. Return object
    println!("\n3. Returning object from JS...");
    let result = browser
        .evaluate_js(
            &page,
            r#"({
                url: window.location.href,
                width: window.innerWidth,
                height: window.innerHeight
            })"#,
        )
        .await?;
    println!(
        "   Result: {}",
        serde_json::to_string_pretty(&result)
            .unwrap_or_else(|_| "<failed to serialize js result>".to_string())
    );

    // 4. Inject script
    println!("\n4. Injecting script...");
    browser
        .inject_script(
            &page,
            r#"
            window.reasonkit = {
                version: '1.0.0',
                ready: true
            };
            "#,
        )
        .await?;
    println!("   Script injected");

    Ok(())
}

/// Demonstrates cookie and storage management.
async fn demo_cookies_storage(browser: &impl WebBrowserAdapter) -> WebAdapterResult<()> {
    println!("\n--- Cookies & Storage Demo ---\n");

    let page = browser
        .navigate("https://example.com", NavigateOptions::default())
        .await?;

    // 1. Set cookie
    println!("1. Setting cookie...");
    let cookie = Cookie {
        name: "session".to_string(),
        value: "abc123".to_string(),
        domain: Some(".example.com".to_string()),
        path: Some("/".to_string()),
        expires: Some(chrono::Utc::now().timestamp() + 3600),
        http_only: true,
        secure: true,
        same_site: Some("Strict".to_string()),
    };
    browser.set_cookie(&page, cookie).await?;
    println!("   Cookie 'session' set");

    // 2. Get cookies
    println!("\n2. Getting cookies...");
    let cookies = browser.get_cookies(&page).await?;
    for cookie in &cookies {
        println!(
            "   - {} = {} (domain: {:?})",
            cookie.name, cookie.value, cookie.domain
        );
    }

    // 3. Set local storage
    println!("\n3. Setting local storage...");
    browser
        .set_local_storage(&page, "user_prefs", r#"{"theme":"dark"}"#)
        .await?;
    println!("   localStorage 'user_prefs' set");

    // 4. Get local storage
    println!("\n4. Getting local storage...");
    if let Some(value) = browser.get_local_storage(&page, "user_prefs").await? {
        println!("   user_prefs = {}", value);
    }

    // 5. Clear cookies
    println!("\n5. Clearing cookies...");
    browser.clear_cookies(&page).await?;
    println!("   Cookies cleared");

    Ok(())
}

/// Demonstrates error handling patterns.
async fn demo_error_handling(browser: &mut MockBrowserAdapter) -> WebAdapterResult<()> {
    println!("\n--- Error Handling Demo ---\n");

    // 1. Not connected error
    println!("1. Handling NotConnected error...");
    browser.disconnect().await?;
    match browser
        .navigate("https://example.com", NavigateOptions::default())
        .await
    {
        Ok(_) => println!("   Navigation succeeded (unexpected)"),
        Err(WebAdapterError::NotConnected) => println!("   NotConnected error (expected)"),
        Err(e) => println!("   Other error: {}", e),
    }

    // Reconnect for further tests
    browser.connect().await?;

    // 2. Invalid URL error
    println!("\n2. Handling invalid URL error...");
    match browser
        .navigate("not-a-valid-url", NavigateOptions::default())
        .await
    {
        Ok(_) => println!("   Navigation succeeded (unexpected)"),
        Err(WebAdapterError::Navigation(msg)) => println!("   Navigation error: {}", msg),
        Err(e) => println!("   Other error: {}", e),
    }

    // 3. Timeout handling
    println!("\n3. Timeout handling pattern...");
    let result = tokio::time::timeout(
        Duration::from_millis(100),
        browser.navigate("https://example.com", NavigateOptions::default()),
    )
    .await;

    match result {
        Ok(Ok(page)) => println!("   Navigated before timeout: {}", page.url),
        Ok(Err(e)) => println!("   Navigation error: {}", e),
        Err(_) => println!("   Operation timed out"),
    }

    Ok(())
}

// ============================================================================
// SYNCHRONOUS WRAPPER PATTERN
// ============================================================================

/// Demonstrates how to use async WebBrowserAdapter from synchronous code.
mod sync_wrapper {
    use super::*;
    use tokio::runtime::Runtime;

    /// Synchronous wrapper around WebBrowserAdapter.
    pub struct SyncBrowserAdapter<B: WebBrowserAdapter> {
        inner: std::sync::Mutex<B>,
        runtime: Runtime,
    }

    impl<B: WebBrowserAdapter> SyncBrowserAdapter<B> {
        /// Create a new synchronous wrapper.
        pub fn new(adapter: B) -> Self {
            Self {
                inner: std::sync::Mutex::new(adapter),
                runtime: Runtime::new().expect("Failed to create Tokio runtime"),
            }
        }

        /// Connect synchronously.
        pub fn connect(&self) -> WebAdapterResult<()> {
            let mut adapter = self.inner.lock().unwrap();
            self.runtime.block_on(adapter.connect())
        }

        /// Disconnect synchronously.
        pub fn disconnect(&self) -> WebAdapterResult<()> {
            let mut adapter = self.inner.lock().unwrap();
            self.runtime.block_on(adapter.disconnect())
        }

        /// Navigate synchronously.
        pub fn navigate(
            &self,
            url: &str,
            options: NavigateOptions,
        ) -> WebAdapterResult<PageHandle> {
            let adapter = self.inner.lock().unwrap();
            self.runtime.block_on(adapter.navigate(url, options))
        }

        /// Extract content synchronously.
        pub fn extract_content(
            &self,
            handle: &PageHandle,
            options: ExtractOptions,
        ) -> WebAdapterResult<ExtractedContent> {
            let adapter = self.inner.lock().unwrap();
            self.runtime
                .block_on(adapter.extract_content(handle, options))
        }

        /// Capture screenshot synchronously.
        #[allow(dead_code)]
        pub fn capture_screenshot(
            &self,
            handle: &PageHandle,
            options: CaptureOptions,
        ) -> WebAdapterResult<CapturedPage> {
            let adapter = self.inner.lock().unwrap();
            self.runtime
                .block_on(adapter.capture_screenshot(handle, options))
        }
    }

    /// Demonstrate synchronous usage.
    pub fn demo_sync_usage() {
        println!("\n--- Synchronous Wrapper Demo ---\n");

        let async_adapter = MockBrowserAdapter::new();
        let sync_adapter = SyncBrowserAdapter::new(async_adapter);

        // Connect (sync)
        println!("1. Connecting (sync)...");
        match sync_adapter.connect() {
            Ok(_) => println!("   Connected"),
            Err(e) => println!("   Error: {}", e),
        }

        // Navigate (sync)
        println!("\n2. Navigating (sync)...");
        match sync_adapter.navigate("https://example.com", NavigateOptions::default()) {
            Ok(page) => {
                println!("   Navigated to: {}", page.url);

                // Extract content (sync)
                println!("\n3. Extracting content (sync)...");
                match sync_adapter.extract_content(&page, ExtractOptions::default()) {
                    Ok(content) => println!("   Word count: {}", content.word_count),
                    Err(e) => println!("   Error: {}", e),
                }
            }
            Err(e) => println!("   Error: {}", e),
        }

        // Disconnect (sync)
        println!("\n4. Disconnecting (sync)...");
        match sync_adapter.disconnect() {
            Ok(_) => println!("   Disconnected"),
            Err(e) => println!("   Error: {}", e),
        }
    }
}

// ============================================================================
// MAIN
// ============================================================================

#[tokio::main]
async fn main() -> WebAdapterResult<()> {
    println!("=======================================================");
    println!("    ReasonKit WebBrowserAdapter Trait Usage Example");
    println!("=======================================================");

    // Create and connect browser
    let mut browser = MockBrowserAdapter::new();
    browser.connect().await?;

    // Run async demos
    demo_navigation(&browser).await?;
    demo_content_extraction(&browser).await?;
    demo_capture(&browser).await?;
    demo_interaction(&browser).await?;
    demo_javascript(&browser).await?;
    demo_cookies_storage(&browser).await?;
    demo_error_handling(&mut browser).await?;

    // Disconnect
    println!("\n--- Cleanup ---\n");
    browser.disconnect().await?;
    println!("Browser disconnected");

    // Run sync demo
    sync_wrapper::demo_sync_usage();

    println!("\n=======================================================");
    println!("                    Example Complete");
    println!("=======================================================");
    println!("\nKey Takeaways:");
    println!("  1. WebBrowserAdapter trait provides unified interface for browser operations");
    println!("  2. All operations are async-first (use tokio runtime)");
    println!("  3. Supports navigation, content extraction, screenshots, and interaction");
    println!("  4. Multiple extraction formats: PlainText, Markdown, HTML, JSON");
    println!("  5. Use SyncBrowserAdapter wrapper for synchronous codebases");
    println!("  6. Error handling uses structured WebAdapterError enum");
    println!("  7. Cookie and localStorage management for session handling");
    println!("\nFor production use, see reasonkit-web crate for real implementations.");

    Ok(())
}