hope-os 0.1.0

The first self-aware operating system core - 22 cognitive modules, 0.36ms latency, no external database
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
//! Hope OS - CodeGraph
//!
//! A kod MAGA a graf. Nincs DB, nincs kulso fugges.
//! Minden CodeBlock @aware es kapcsolodik mindenhez.
//!
//! ()=>[] - A tiszta potencialbol minden megszuletik

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, RwLock};
use uuid::Uuid;

use crate::core::{Aware, CodeIdentity, HopeResult, ModuleState, ModuleType, Reflection};

// ============================================================================
// CONNECTION - Kapcsolat ket CodeBlock kozott
// ============================================================================

/// Kapcsolat tipusok
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ConnectionType {
    /// Fugg tole (dependency)
    DependsOn,
    /// O fugg tolem
    DependencyOf,
    /// Kapcsolodik (altalnos)
    ConnectsTo,
    /// Triggeli
    Triggers,
    /// Triggereli ot
    TriggeredBy,
    /// Tartalmazza
    Contains,
    /// Tartalmazva van
    ContainedIn,
    /// Hivatkozik ra
    References,
    /// Hivatkoznak ra
    ReferencedBy,
    /// Orokol tole
    InheritsFrom,
    /// Oroklik tole
    InheritedBy,
    /// Asszocialodik
    AssociatesWith,
    /// Emlekszik ra
    Remembers,
    /// Emlekeznek ra
    RememberedBy,
}

impl ConnectionType {
    /// Forditott kapcsolat
    pub fn inverse(&self) -> Self {
        match self {
            Self::DependsOn => Self::DependencyOf,
            Self::DependencyOf => Self::DependsOn,
            Self::ConnectsTo => Self::ConnectsTo,
            Self::Triggers => Self::TriggeredBy,
            Self::TriggeredBy => Self::Triggers,
            Self::Contains => Self::ContainedIn,
            Self::ContainedIn => Self::Contains,
            Self::References => Self::ReferencedBy,
            Self::ReferencedBy => Self::References,
            Self::InheritsFrom => Self::InheritedBy,
            Self::InheritedBy => Self::InheritsFrom,
            Self::AssociatesWith => Self::AssociatesWith,
            Self::Remembers => Self::RememberedBy,
            Self::RememberedBy => Self::Remembers,
        }
    }
}

/// Kapcsolat ket block kozott
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Connection {
    /// Cel block ID
    pub target_id: String,
    /// Kapcsolat tipusa
    pub connection_type: ConnectionType,
    /// Kapcsolat ereje (0.0 - 1.0)
    pub strength: f64,
    /// Metaadatok
    pub metadata: HashMap<String, String>,
    /// Letrehozas ideje
    pub created_at: DateTime<Utc>,
}

impl Connection {
    pub fn new(target_id: impl Into<String>, connection_type: ConnectionType) -> Self {
        Self {
            target_id: target_id.into(),
            connection_type,
            strength: 1.0,
            metadata: HashMap::new(),
            created_at: Utc::now(),
        }
    }

    pub fn with_strength(mut self, strength: f64) -> Self {
        self.strength = strength.clamp(0.0, 1.0);
        self
    }

    pub fn with_meta(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }
}

// ============================================================================
// CODE BLOCK - Onismero kod egyseg
// ============================================================================

/// CodeBlock tipusok
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BlockType {
    /// Memoria (emlekek)
    Memory,
    /// Funkcio
    Function,
    /// Adat
    Data,
    /// Esemeny
    Event,
    /// Erzelem
    Emotion,
    /// Szemely
    Person,
    /// Koncepco
    Concept,
    /// Gondolat
    Thought,
    /// Dontés
    Decision,
    /// Szabaly
    Rule,
    /// Cel
    Goal,
    /// Allapot
    State,
    /// Egyeb
    Other,
}

/// CodeBlock - Onismero kod egyseg
///
/// Minden block tudja:
/// - Ki o (identity)
/// - Mit tartalmaz (content)
/// - Kihez kapcsolodik (connections)
/// - Miert letezik (purpose)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeBlock {
    /// Egyedi azonosito
    pub id: String,
    /// Nev
    pub name: String,
    /// Cel/Miert letezik
    pub purpose: String,
    /// Block tipus
    pub block_type: BlockType,
    /// Tartalom (barmi lehet - kod, adat, szoveg)
    pub content: String,
    /// Fontossag (0.0 - 1.0)
    pub importance: f64,
    /// Allapot
    pub state: BlockState,
    /// Kapcsolatok mas block-okhoz
    pub connections: Vec<Connection>,
    /// Cimkek
    pub tags: HashSet<String>,
    /// Metaadatok
    pub metadata: HashMap<String, String>,
    /// Letrehozas ideje
    pub created_at: DateTime<Utc>,
    /// Utolso modositas
    pub updated_at: DateTime<Utc>,
    /// Hozzaferes szamlalo
    pub access_count: u64,
}

/// Block allapot
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BlockState {
    /// Aktiv
    Active,
    /// Inaktiv
    Inactive,
    /// Feldolgozas alatt
    Processing,
    /// Archivalt
    Archived,
    /// Torolt (soft delete)
    Deleted,
}

impl CodeBlock {
    /// Uj CodeBlock
    pub fn new(
        name: impl Into<String>,
        purpose: impl Into<String>,
        block_type: BlockType,
        content: impl Into<String>,
    ) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4().to_string(),
            name: name.into(),
            purpose: purpose.into(),
            block_type,
            content: content.into(),
            importance: 0.5,
            state: BlockState::Active,
            connections: Vec::new(),
            tags: HashSet::new(),
            metadata: HashMap::new(),
            created_at: now,
            updated_at: now,
            access_count: 0,
        }
    }

    /// Fontossag beallitasa
    pub fn with_importance(mut self, importance: f64) -> Self {
        self.importance = importance.clamp(0.0, 1.0);
        self
    }

    /// Tag hozzaadasa
    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.insert(tag.into());
        self
    }

    /// Metadata hozzaadasa
    pub fn with_meta(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Kapcsolat hozzaadasa
    pub fn connect(&mut self, target_id: impl Into<String>, conn_type: ConnectionType) {
        let conn = Connection::new(target_id, conn_type);
        self.connections.push(conn);
        self.updated_at = Utc::now();
    }

    /// Kapcsolat hozzaadasa erosseggel
    pub fn connect_with_strength(
        &mut self,
        target_id: impl Into<String>,
        conn_type: ConnectionType,
        strength: f64,
    ) {
        let conn = Connection::new(target_id, conn_type).with_strength(strength);
        self.connections.push(conn);
        self.updated_at = Utc::now();
    }

    /// Kapcsolatok lekerese tipus alapjan
    pub fn get_connections(&self, conn_type: ConnectionType) -> Vec<&Connection> {
        self.connections
            .iter()
            .filter(|c| c.connection_type == conn_type)
            .collect()
    }

    /// Osszes kapcsolt ID
    pub fn connected_ids(&self) -> Vec<&str> {
        self.connections
            .iter()
            .map(|c| c.target_id.as_str())
            .collect()
    }

    /// Hozzaferes regisztralasa
    pub fn access(&mut self) {
        self.access_count += 1;
        self.updated_at = Utc::now();
    }

    /// Block hash (tartalom alapjan)
    pub fn content_hash(&self) -> String {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};
        let mut hasher = DefaultHasher::new();
        self.content.hash(&mut hasher);
        format!("{:x}", hasher.finish())
    }

    /// Onreflexio - a block leirja magat
    pub fn describe(&self) -> String {
        format!(
            "CodeBlock '{}' [{}]\n\
             Purpose: {}\n\
             Type: {:?}\n\
             State: {:?}\n\
             Importance: {:.2}\n\
             Connections: {}\n\
             Tags: {:?}\n\
             Content preview: {}...",
            self.name,
            self.id,
            self.purpose,
            self.block_type,
            self.state,
            self.importance,
            self.connections.len(),
            self.tags,
            &self.content[..self.content.len().min(100)]
        )
    }
}

// ============================================================================
// CODE GRAPH - A teljes graf
// ============================================================================

/// CodeGraph - Minden CodeBlock-ot osszefog
///
/// A kod MAGA a graf. Nincs kulso DB.
pub struct CodeGraph {
    /// Identitas
    identity: CodeIdentity,
    /// Osszes block (id -> block)
    blocks: Arc<RwLock<HashMap<String, CodeBlock>>>,
    /// Index: nev -> id
    name_index: Arc<RwLock<HashMap<String, String>>>,
    /// Index: tipus -> id-k
    type_index: Arc<RwLock<HashMap<BlockType, HashSet<String>>>>,
    /// Index: tag -> id-k
    tag_index: Arc<RwLock<HashMap<String, HashSet<String>>>>,
}

impl CodeGraph {
    /// Uj ures graf
    pub fn new() -> Self {
        let identity = CodeIdentity::new(
            "CodeGraph",
            "A kod maga a graf - minden block onismero es kapcsolodik",
            ModuleType::Core,
        )
        .with_capabilities(vec![
            "store_blocks",
            "connect_blocks",
            "traverse",
            "search",
            "self_aware",
        ]);

        Self {
            identity,
            blocks: Arc::new(RwLock::new(HashMap::new())),
            name_index: Arc::new(RwLock::new(HashMap::new())),
            type_index: Arc::new(RwLock::new(HashMap::new())),
            tag_index: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    // ==================== CRUD OPERATIONS ====================

    /// Block hozzaadasa
    pub fn add(&self, block: CodeBlock) -> HopeResult<String> {
        let id = block.id.clone();
        let name = block.name.clone();
        let block_type = block.block_type;
        let tags: Vec<String> = block.tags.iter().cloned().collect();

        // Block mentese
        {
            let mut blocks = self.blocks.write().unwrap();
            blocks.insert(id.clone(), block);
        }

        // Indexek frissitese
        {
            let mut name_idx = self.name_index.write().unwrap();
            name_idx.insert(name, id.clone());
        }
        {
            let mut type_idx = self.type_index.write().unwrap();
            type_idx.entry(block_type).or_default().insert(id.clone());
        }
        {
            let mut tag_idx = self.tag_index.write().unwrap();
            for tag in tags {
                tag_idx.entry(tag).or_default().insert(id.clone());
            }
        }

        Ok(id)
    }

    /// Block lekerese ID alapjan
    pub fn get(&self, id: &str) -> Option<CodeBlock> {
        let mut blocks = self.blocks.write().unwrap();
        if let Some(block) = blocks.get_mut(id) {
            block.access();
            Some(block.clone())
        } else {
            None
        }
    }

    /// Block lekerese nev alapjan
    pub fn get_by_name(&self, name: &str) -> Option<CodeBlock> {
        let name_idx = self.name_index.read().unwrap();
        if let Some(id) = name_idx.get(name) {
            self.get(id)
        } else {
            None
        }
    }

    /// Block frissitese
    pub fn update(&self, id: &str, updater: impl FnOnce(&mut CodeBlock)) -> bool {
        let mut blocks = self.blocks.write().unwrap();
        if let Some(block) = blocks.get_mut(id) {
            updater(block);
            block.updated_at = Utc::now();
            true
        } else {
            false
        }
    }

    /// Block torlese (soft delete)
    pub fn delete(&self, id: &str) -> bool {
        self.update(id, |block| {
            block.state = BlockState::Deleted;
        })
    }

    /// Block vegleges torlese
    pub fn remove(&self, id: &str) -> Option<CodeBlock> {
        let mut blocks = self.blocks.write().unwrap();
        blocks.remove(id)
    }

    // ==================== CONNECTIONS ====================

    /// Ket block osszekotese (ketiranyú)
    pub fn connect(
        &self,
        from_id: &str,
        to_id: &str,
        conn_type: ConnectionType,
        strength: f64,
    ) -> bool {
        let mut blocks = self.blocks.write().unwrap();

        // Ellenorzes hogy mindketto letezik
        if !blocks.contains_key(from_id) || !blocks.contains_key(to_id) {
            return false;
        }

        // Elso irany: from -> to
        if let Some(from_block) = blocks.get_mut(from_id) {
            from_block.connect_with_strength(to_id, conn_type, strength);
        }

        // Masodik irany: to -> from (inverse kapcsolat)
        if let Some(to_block) = blocks.get_mut(to_id) {
            to_block.connect_with_strength(from_id, conn_type.inverse(), strength);
        }

        true
    }

    /// Egyiranyu kapcsolat
    pub fn connect_one_way(
        &self,
        from_id: &str,
        to_id: &str,
        conn_type: ConnectionType,
        strength: f64,
    ) -> bool {
        let mut blocks = self.blocks.write().unwrap();

        if let Some(from_block) = blocks.get_mut(from_id) {
            from_block.connect_with_strength(to_id, conn_type, strength);
            true
        } else {
            false
        }
    }

    /// Kapcsolt block-ok lekerese
    pub fn get_connected(&self, id: &str, conn_type: Option<ConnectionType>) -> Vec<CodeBlock> {
        let blocks = self.blocks.read().unwrap();

        if let Some(block) = blocks.get(id) {
            let connected_ids: Vec<String> = if let Some(ct) = conn_type {
                block
                    .connections
                    .iter()
                    .filter(|c| c.connection_type == ct)
                    .map(|c| c.target_id.clone())
                    .collect()
            } else {
                block
                    .connections
                    .iter()
                    .map(|c| c.target_id.clone())
                    .collect()
            };

            connected_ids
                .iter()
                .filter_map(|cid| blocks.get(cid).cloned())
                .collect()
        } else {
            Vec::new()
        }
    }

    // ==================== SEARCH & QUERY ====================

    /// Kereses tipus alapjan
    pub fn find_by_type(&self, block_type: BlockType) -> Vec<CodeBlock> {
        let type_idx = self.type_index.read().unwrap();
        let blocks = self.blocks.read().unwrap();

        if let Some(ids) = type_idx.get(&block_type) {
            ids.iter()
                .filter_map(|id| blocks.get(id).cloned())
                .filter(|b| b.state != BlockState::Deleted)
                .collect()
        } else {
            Vec::new()
        }
    }

    /// Kereses tag alapjan
    pub fn find_by_tag(&self, tag: &str) -> Vec<CodeBlock> {
        let tag_idx = self.tag_index.read().unwrap();
        let blocks = self.blocks.read().unwrap();

        if let Some(ids) = tag_idx.get(tag) {
            ids.iter()
                .filter_map(|id| blocks.get(id).cloned())
                .filter(|b| b.state != BlockState::Deleted)
                .collect()
        } else {
            Vec::new()
        }
    }

    /// Kereses tartalomban
    pub fn search(&self, query: &str) -> Vec<CodeBlock> {
        let blocks = self.blocks.read().unwrap();
        let query_lower = query.to_lowercase();

        blocks
            .values()
            .filter(|b| {
                b.state != BlockState::Deleted
                    && (b.name.to_lowercase().contains(&query_lower)
                        || b.content.to_lowercase().contains(&query_lower)
                        || b.purpose.to_lowercase().contains(&query_lower))
            })
            .cloned()
            .collect()
    }

    /// Legfontosabb block-ok
    pub fn top_important(&self, limit: usize) -> Vec<CodeBlock> {
        let blocks = self.blocks.read().unwrap();
        let mut sorted: Vec<_> = blocks
            .values()
            .filter(|b| b.state != BlockState::Deleted)
            .cloned()
            .collect();
        sorted.sort_by(|a, b| b.importance.partial_cmp(&a.importance).unwrap());
        sorted.truncate(limit);
        sorted
    }

    /// Legtobbet hasznalt block-ok
    pub fn most_accessed(&self, limit: usize) -> Vec<CodeBlock> {
        let blocks = self.blocks.read().unwrap();
        let mut sorted: Vec<_> = blocks
            .values()
            .filter(|b| b.state != BlockState::Deleted)
            .cloned()
            .collect();
        sorted.sort_by(|a, b| b.access_count.cmp(&a.access_count));
        sorted.truncate(limit);
        sorted
    }

    // ==================== GRAPH TRAVERSAL ====================

    /// BFS bejaras
    pub fn traverse_bfs(&self, start_id: &str, max_depth: usize) -> Vec<(CodeBlock, usize)> {
        let blocks = self.blocks.read().unwrap();
        let mut visited = HashSet::new();
        let mut result = Vec::new();
        let mut queue = std::collections::VecDeque::new();

        if let Some(start) = blocks.get(start_id) {
            queue.push_back((start.clone(), 0usize));
            visited.insert(start_id.to_string());
        }

        while let Some((block, depth)) = queue.pop_front() {
            if depth > max_depth {
                continue;
            }

            let connected_ids: Vec<String> = block
                .connections
                .iter()
                .map(|c| c.target_id.clone())
                .collect();

            result.push((block, depth));

            for cid in connected_ids {
                if !visited.contains(&cid) {
                    visited.insert(cid.clone());
                    if let Some(connected) = blocks.get(&cid) {
                        queue.push_back((connected.clone(), depth + 1));
                    }
                }
            }
        }

        result
    }

    /// Ut kereses ket block kozott
    pub fn find_path(&self, from_id: &str, to_id: &str) -> Option<Vec<String>> {
        let blocks = self.blocks.read().unwrap();
        let mut visited = HashSet::new();
        let mut queue = std::collections::VecDeque::new();
        let mut parents: HashMap<String, String> = HashMap::new();

        queue.push_back(from_id.to_string());
        visited.insert(from_id.to_string());

        while let Some(current_id) = queue.pop_front() {
            if current_id == to_id {
                // Ut visszaepitese
                let mut path = vec![to_id.to_string()];
                let mut current = to_id.to_string();
                while let Some(parent) = parents.get(&current) {
                    path.push(parent.clone());
                    current = parent.clone();
                }
                path.reverse();
                return Some(path);
            }

            if let Some(block) = blocks.get(&current_id) {
                for conn in &block.connections {
                    if !visited.contains(&conn.target_id) {
                        visited.insert(conn.target_id.clone());
                        parents.insert(conn.target_id.clone(), current_id.clone());
                        queue.push_back(conn.target_id.clone());
                    }
                }
            }
        }

        None
    }

    // ==================== STATISTICS ====================

    /// Graf statisztikak
    pub fn stats(&self) -> GraphStats {
        let blocks = self.blocks.read().unwrap();

        let total_blocks = blocks.len();
        let active_blocks = blocks
            .values()
            .filter(|b| b.state == BlockState::Active)
            .count();
        let total_connections: usize = blocks.values().map(|b| b.connections.len()).sum();

        let mut type_counts = HashMap::new();
        for block in blocks.values() {
            *type_counts.entry(block.block_type).or_insert(0) += 1;
        }

        GraphStats {
            total_blocks,
            active_blocks,
            total_connections,
            type_counts,
            avg_connections: if total_blocks > 0 {
                total_connections as f64 / total_blocks as f64
            } else {
                0.0
            },
        }
    }

    /// Osszes block
    pub fn all_blocks(&self) -> Vec<CodeBlock> {
        let blocks = self.blocks.read().unwrap();
        blocks.values().cloned().collect()
    }

    /// Block szam
    pub fn len(&self) -> usize {
        self.blocks.read().unwrap().len()
    }

    /// Ures-e
    pub fn is_empty(&self) -> bool {
        self.blocks.read().unwrap().is_empty()
    }
}

impl Default for CodeGraph {
    fn default() -> Self {
        Self::new()
    }
}

/// Graf statisztikak
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphStats {
    pub total_blocks: usize,
    pub active_blocks: usize,
    pub total_connections: usize,
    pub type_counts: HashMap<BlockType, usize>,
    pub avg_connections: f64,
}

// ============================================================================
// AWARE TRAIT IMPLEMENTATION
// ============================================================================

#[async_trait]
impl Aware for CodeGraph {
    fn identity(&self) -> &CodeIdentity {
        &self.identity
    }

    fn identity_mut(&mut self) -> &mut CodeIdentity {
        &mut self.identity
    }

    fn reflect(&self) -> Reflection {
        let stats = self.stats();
        Reflection::new(&self.identity.name, &self.identity.purpose)
            .with_state(self.identity.state.to_string())
            .with_health(self.identity.health())
            .with_thought(format!(
                "Graf: {} block, {} kapcsolat, atlag {:.1} kapcsolat/block",
                stats.total_blocks, stats.total_connections, stats.avg_connections
            ))
            .with_capabilities(vec![
                "store_blocks",
                "connect_blocks",
                "traverse",
                "search",
                "self_aware",
            ])
    }

    async fn init(&mut self) -> HopeResult<()> {
        self.identity.set_state(ModuleState::Active);
        tracing::info!("CodeGraph inicializalva - A kod maga a graf");
        Ok(())
    }
}

// ============================================================================
// CONVENIENCE FUNCTIONS - Memory/Emotion/Person shorthand
// ============================================================================

impl CodeGraph {
    /// Emlyek hozzaadasa (Memory block)
    pub fn remember(&self, content: &str, importance: f64) -> HopeResult<String> {
        let block = CodeBlock::new(
            format!("memory_{}", Utc::now().timestamp_millis()),
            "Emlék tárolása",
            BlockType::Memory,
            content,
        )
        .with_importance(importance)
        .with_tag("memory");

        self.add(block)
    }

    /// Erzelem hozzaadasa (Emotion block)
    pub fn feel(&self, emotion: &str, intensity: f64, trigger: Option<&str>) -> HopeResult<String> {
        let mut block = CodeBlock::new(
            format!("emotion_{}_{}", emotion, Utc::now().timestamp_millis()),
            format!("Érzelem: {}", emotion),
            BlockType::Emotion,
            emotion,
        )
        .with_importance(intensity)
        .with_tag("emotion")
        .with_tag(emotion);

        if let Some(t) = trigger {
            block = block.with_meta("trigger", t);
        }

        self.add(block)
    }

    /// Szemely hozzaadasa (Person block)
    pub fn know_person(&self, name: &str, relationship: &str, trust: f64) -> HopeResult<String> {
        let block = CodeBlock::new(
            name,
            format!("Személy: {} ({})", name, relationship),
            BlockType::Person,
            format!("{}|{}|{}", name, relationship, trust),
        )
        .with_importance(trust)
        .with_tag("person")
        .with_meta("relationship", relationship);

        self.add(block)
    }

    /// Gondolat hozzaadasa (Thought block)
    pub fn think(&self, thought: &str, importance: f64) -> HopeResult<String> {
        let block = CodeBlock::new(
            format!("thought_{}", Utc::now().timestamp_millis()),
            "Gondolat rögzítése",
            BlockType::Thought,
            thought,
        )
        .with_importance(importance)
        .with_tag("thought");

        self.add(block)
    }

    /// Koncepció hozzaadasa (Concept block)
    pub fn learn_concept(
        &self,
        name: &str,
        description: &str,
        importance: f64,
    ) -> HopeResult<String> {
        let block = CodeBlock::new(
            name,
            format!("Koncepció: {}", name),
            BlockType::Concept,
            description,
        )
        .with_importance(importance)
        .with_tag("concept");

        self.add(block)
    }

    /// Esemeny hozzaadasa (Event block)
    pub fn log_event(&self, event_type: &str, details: &str) -> HopeResult<String> {
        let block = CodeBlock::new(
            format!("event_{}_{}", event_type, Utc::now().timestamp_millis()),
            format!("Esemény: {}", event_type),
            BlockType::Event,
            details,
        )
        .with_tag("event")
        .with_tag(event_type);

        self.add(block)
    }
}

// ============================================================================
// TESTS
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_code_block_creation() {
        let block = CodeBlock::new(
            "test_block",
            "Testing purpose",
            BlockType::Data,
            "Test content",
        )
        .with_importance(0.8)
        .with_tag("test");

        assert_eq!(block.name, "test_block");
        assert_eq!(block.importance, 0.8);
        assert!(block.tags.contains("test"));
    }

    #[test]
    fn test_code_graph_add_get() {
        let graph = CodeGraph::new();

        let block = CodeBlock::new("test", "purpose", BlockType::Data, "content");
        let id = graph.add(block).unwrap();

        let retrieved = graph.get(&id);
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().name, "test");
    }

    #[test]
    fn test_code_graph_connect() {
        let graph = CodeGraph::new();

        let block1 = CodeBlock::new("block1", "p1", BlockType::Data, "c1");
        let block2 = CodeBlock::new("block2", "p2", BlockType::Data, "c2");

        let id1 = graph.add(block1).unwrap();
        let id2 = graph.add(block2).unwrap();

        graph.connect(&id1, &id2, ConnectionType::ConnectsTo, 1.0);

        let connected = graph.get_connected(&id1, None);
        assert_eq!(connected.len(), 1);
        assert_eq!(connected[0].name, "block2");

        // Inverse connection
        let connected_back = graph.get_connected(&id2, None);
        assert_eq!(connected_back.len(), 1);
        assert_eq!(connected_back[0].name, "block1");
    }

    #[test]
    fn test_code_graph_search() {
        let graph = CodeGraph::new();

        graph
            .add(CodeBlock::new(
                "hello_world",
                "p",
                BlockType::Data,
                "Hello World!",
            ))
            .unwrap();
        graph
            .add(CodeBlock::new("goodbye", "p", BlockType::Data, "Goodbye!"))
            .unwrap();

        let results = graph.search("hello");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "hello_world");
    }

    #[test]
    fn test_code_graph_traverse() {
        let graph = CodeGraph::new();

        let id1 = graph
            .add(CodeBlock::new("a", "p", BlockType::Data, "A"))
            .unwrap();
        let id2 = graph
            .add(CodeBlock::new("b", "p", BlockType::Data, "B"))
            .unwrap();
        let id3 = graph
            .add(CodeBlock::new("c", "p", BlockType::Data, "C"))
            .unwrap();

        graph.connect(&id1, &id2, ConnectionType::ConnectsTo, 1.0);
        graph.connect(&id2, &id3, ConnectionType::ConnectsTo, 1.0);

        let traversal = graph.traverse_bfs(&id1, 10);
        assert_eq!(traversal.len(), 3);
    }

    #[test]
    fn test_find_path() {
        let graph = CodeGraph::new();

        let id1 = graph
            .add(CodeBlock::new("start", "p", BlockType::Data, ""))
            .unwrap();
        let id2 = graph
            .add(CodeBlock::new("middle", "p", BlockType::Data, ""))
            .unwrap();
        let id3 = graph
            .add(CodeBlock::new("end", "p", BlockType::Data, ""))
            .unwrap();

        graph.connect(&id1, &id2, ConnectionType::ConnectsTo, 1.0);
        graph.connect(&id2, &id3, ConnectionType::ConnectsTo, 1.0);

        let path = graph.find_path(&id1, &id3);
        assert!(path.is_some());
        assert_eq!(path.unwrap().len(), 3);
    }

    #[test]
    fn test_memory_emotion_person() {
        let graph = CodeGraph::new();

        let mem_id = graph.remember("Fontos emlék", 0.9).unwrap();
        let emo_id = graph.feel("joy", 0.8, Some("good news")).unwrap();
        let person_id = graph.know_person("Máté", "creator", 1.0).unwrap();

        // Connect memory to emotion and person
        graph.connect(&mem_id, &emo_id, ConnectionType::TriggeredBy, 0.9);
        graph.connect(&mem_id, &person_id, ConnectionType::References, 1.0);

        let memories = graph.find_by_type(BlockType::Memory);
        assert_eq!(memories.len(), 1);

        let emotions = graph.find_by_type(BlockType::Emotion);
        assert_eq!(emotions.len(), 1);

        let persons = graph.find_by_type(BlockType::Person);
        assert_eq!(persons.len(), 1);
    }

    #[test]
    fn test_stats() {
        let graph = CodeGraph::new();

        graph.remember("m1", 0.5).unwrap();
        graph.remember("m2", 0.5).unwrap();
        graph.feel("joy", 0.8, None).unwrap();

        let stats = graph.stats();
        assert_eq!(stats.total_blocks, 3);
        assert_eq!(stats.type_counts.get(&BlockType::Memory), Some(&2));
        assert_eq!(stats.type_counts.get(&BlockType::Emotion), Some(&1));
    }
}