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
//! # Client Network Account Management
//!
//! This module provides comprehensive client account management functionality for the Citadel Protocol.
//! It handles both personal and impersonal connection modes, secure credential management, and peer relationships
//! within HyperLAN and HyperWAN networks.
//!
//! ## Features
//!
//! * **Connection Modes**
//!   - Personal mode with full authentication and encryption
//!   - Impersonal mode for temporary or anonymous connections
//!
//! * **Security**
//!   - Secure credential storage and validation
//!   - Ratchet-based cryptographic state management
//!   - Immutable critical security fields
//!
//! * **Network Management**
//!   - HyperLAN and HyperWAN peer relationship handling
//!   - Peer list synchronization
//!   - P2P connection support
//!   - Connection endpoint configuration
//!
//! * **Thread Safety**
//!   - All operations are thread-safe through RwLock
//!   - Concurrent access to shared resources
//!
//! ## Important Notes
//!
//! 1. Always use strong passwords and proper credential management
//! 2. Keep cryptographic state synchronized between peers
//! 3. Handle connection errors and implement proper retry logic
//! 4. Regularly clean up stale peer connections
//! 5. Monitor connection quality and implement appropriate fallbacks
//!
//! ## Related Components
//!
//! * `NetworkMode` - Defines the network operation mode
//! * `PeerConnection` - Manages individual peer connections
//! * `SecurityState` - Handles cryptographic state
//! * `EndpointConfig` - Configures connection endpoints
//!
//! Manages individual client connections within the Citadel Protocol network. Each ClientNetworkAccount
//! represents a unique connection endpoint, handling authentication, peer relationships, and cryptographic
//! state for both personal and impersonal connection modes.
//!
//! ## Features
//!
//! * **Connection Management**:
//!   - Personal and impersonal connection modes
//!   - HyperLAN and HyperWAN peer management
//!   - Connection state tracking
//!   - Network endpoint configuration
//!
//! * **Authentication**:
//!   - Secure credential management
//!   - Password-based authentication
//!   - Passwordless authentication support
//!   - Credential validation and generation
//!
//! * **Cryptographic Operations**:
//!   - Ratchet-based key management
//!   - Session crypto state handling
//!   - Static and dynamic key management
//!   - Forward secrecy support
//!
//! * **Peer Management**:
//!   - HyperLAN peer registration
//!   - Peer list synchronization
//!   - Mutual peer relationship tracking
//!   - P2P connection support
//!
//!
//! ## Important Notes
//!
//! * Each account represents a unique connection endpoint
//! * Thread-safe operations through RwLock protection
//! * Supports both personal and impersonal connection modes
//! * Automatic peer list synchronization with HyperLAN server
//! * Critical fields (cid, adjacent_nid, is_personal) are immutable
//!
//! ## Related Components
//!
//! * `auth`: Authentication and credential management
//! * `network_account`: Network node configuration
//! * `hypernode_account`: Base account functionality
//! * `citadel_crypt`: Cryptographic operations
//!

use serde::{Deserialize, Serialize};
use std::sync::Arc;

use crate::misc::{get_present_formatted_timestamp, AccountError, CNACMetadata};
use crate::prelude::ConnectionInfo;
use multimap::MultiMap;

use citadel_crypt::endpoint_crypto_container::PeerSessionCrypto;
use citadel_crypt::ratchets::Ratchet;
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::fmt::Formatter;

use crate::auth::proposed_credentials::ProposedCredentials;
use crate::auth::DeclaredAuthenticationMode;
use crate::serialization::SyncIO;
use citadel_crypt::prelude::Toolset;
use citadel_crypt::ratchets::mono::MonoRatchet;
use citadel_crypt::ratchets::stacked::StackedRatchet;
use citadel_types::crypto::SecBuffer;
use citadel_types::user::MutualPeer;
use std::collections::HashMap;
use std::marker::PhantomData;

/// The default index for denoting a HyperLAN connection (relative to THIS cnac)
pub const HYPERLAN_IDX: u64 = 0;

///use futures::{TryFutureExt, TryStreamExt};
#[derive(Serialize, Deserialize)]
/// Inner device
pub struct AccountState {
    /// RTDB config for client-side communications
    #[cfg(feature = "google-services")]
    pub client_rtdb_config: Option<crate::external_services::rtdb::RtdbClientConfig>,
    #[cfg(not(feature = "google-services"))]
    pub client_rtdb_config: Option<()>,
    /// For impersonal mode:
    /// input: iCID (the central server of the CID), output: MutualPeer(iCID, CID) where iCID == iCID.
    /// It maps the iCID to the CID. The iCID is zero if the peer client is within the HyperLAN (impersonal mode ONLY). Each CNAC
    /// is implied to be tethered to a HyperLAN Server, and as such, we use ZERO to imply the connection is within the
    /// HyperLAN
    ///
    /// For personal mode:
    /// input: iCID, output: (equal iCID or HyperWAN iCID, CID).
    /// if iCID == 0, then that implies a personal HyperLAN Client
    /// Suppose we input key k to retrieve tuple (i, j). If k == i, then the peer j is in k. If k != i, then j is in i (i.e., a HyperWAN client).
    pub mutuals: MultiMap<u64, MutualPeer>,
    /// peer id -> key -> sub_key -> bytes
    pub byte_map: HashMap<u64, HashMap<String, HashMap<String, Vec<u8>>>>,
}

/// A thread-safe handle for sharing data across threads and applications
///
/// SAFETY: The `cid`, `adjacent_nid`, and `is_personal` is private. These values
/// should NEVER be edited within this source file
#[derive(Serialize, Deserialize)]
pub struct ClientNetworkAccount<R: Ratchet = StackedRatchet, Fcm: Ratchet = MonoRatchet> {
    /// The inner thread-safe device
    #[serde(bound = "")]
    inner: Arc<ClientNetworkAccountInner<R, Fcm>>,
}

#[derive(Serialize, Deserialize)]
struct ClientNetworkAccountInner<R: Ratchet = StackedRatchet, Fcm: Ratchet = MonoRatchet> {
    /// The client identification number
    cid: u64,
    is_personal: bool,
    is_transient: bool,
    pub creation_date: String,
    pub adjacent_nac: ConnectionInfo,
    #[serde(bound = "")]
    pub crypto_session_state: Option<PeerSessionCrypto<R>>,
    /// For storing critical ID information for this CNAC
    pub auth_store: DeclaredAuthenticationMode,
    peer_state: RwLock<AccountState>,
    // For future use cases
    _phantom: PhantomData<Fcm>,
}

impl<R: Ratchet, Fcm: Ratchet> ClientNetworkAccount<R, Fcm> {
    /// Note: This should ONLY be called from a server node.
    #[allow(unused_results)]
    pub async fn new(
        valid_cid: u64,
        is_personal: bool,
        adjacent_nac: ConnectionInfo,
        auth_store: DeclaredAuthenticationMode,
        crypto_session_state: Option<PeerSessionCrypto<R>>,
    ) -> Result<Self, AccountError> {
        if valid_cid == 0 && crypto_session_state.is_some() {
            return Err(citadel_io::error!(citadel_io::ErrorCode::CnacCidZero));
        }

        log::trace!(target: "citadel", "Creating CNAC w/valid cid: {valid_cid:?}");
        let creation_date = get_present_formatted_timestamp();

        let peer_state = AccountState {
            mutuals: MultiMap::new(),
            byte_map: HashMap::default(),
            client_rtdb_config: None,
        };

        let is_transient = auth_store.is_transient();

        Ok(Self {
            inner: Arc::new(ClientNetworkAccountInner {
                creation_date,
                cid: valid_cid,
                auth_store,
                adjacent_nac,
                is_personal,
                is_transient,
                crypto_session_state,
                peer_state: RwLock::new(peer_state),
                _phantom: PhantomData,
            }),
        })
    }

    pub fn get_session_crypto(&self) -> &PeerSessionCrypto<R> {
        self.inner.crypto_session_state.as_ref().expect("Unauthorized access to the zero CID. Raising to panic. Access to the zero CID is prohibited.")
    }

    /// Resets the toolset, if necessary. If the CNAC was freshly serialized, the hyper ratchet
    /// is not updated. In either case, returns the static aux hyper ratchet
    #[allow(unused_results)]
    pub fn refresh_static_ratchet(&self) -> R {
        self.get_session_crypto().refresh_state();
        // Use write to enforce one accessor
        let write = self.get_session_crypto().toolset().write();
        write.verify_init_state();
        write.get_static_auxiliary_ratchet().clone()
    }

    /// Stores the rtdb config
    #[cfg(feature = "google-services")]
    pub fn store_rtdb_config(&self, cfg: crate::external_services::rtdb::RtdbClientConfig) {
        self.write().client_rtdb_config = Some(cfg);
    }

    pub fn auth_store(&self) -> &DeclaredAuthenticationMode {
        &self.inner.auth_store
    }

    /// Returns true if the NAC is a personal type
    pub fn is_personal(&self) -> bool {
        self.inner.is_personal
    }

    /// Towards the end of the registration phase, the [`AccountState`] gets transmitted to Alice.
    pub async fn new_from_network_personal(
        valid_cid: u64,
        session_crypto_state: Option<PeerSessionCrypto<R>>,
        auth_store: DeclaredAuthenticationMode,
        conn_info: ConnectionInfo,
    ) -> Result<Self, AccountError> {
        const IS_PERSONAL: bool = true;
        // We supply none to the valid cid
        Self::new(
            valid_cid,
            IS_PERSONAL,
            conn_info,
            auth_store,
            session_crypto_state,
        )
        .await
    }

    /// Returns the username of this client
    pub fn get_username(&self) -> String {
        self.inner.auth_store.username().to_string()
    }

    /// Checks the credentials for validity. Used for the login process.
    pub async fn validate_credentials(
        &self,
        creds: ProposedCredentials,
    ) -> Result<(), AccountError> {
        let argon_container = {
            let username = self.inner.auth_store.username();

            if !creds.compare_username(username.as_bytes()) {
                return Err(AccountError::account_invalid_username());
            }

            match &self.inner.auth_store {
                DeclaredAuthenticationMode::Argon { argon, .. } => argon.clone(),
                DeclaredAuthenticationMode::Transient { .. } => return Ok(()),
            }
        };

        creds.validate_credentials(argon_container).await
    }

    /// This should be called on the client before passing a connect request to the protocol
    pub async fn generate_connect_credentials(
        &self,
        password_raw: SecBuffer,
    ) -> Result<ProposedCredentials, AccountError> {
        let (settings, full_name, username) = {
            match &self.inner.auth_store {
                DeclaredAuthenticationMode::Argon {
                    argon,
                    full_name,
                    username,
                } => (
                    argon.settings().clone(),
                    full_name.clone(),
                    username.clone(),
                ),
                DeclaredAuthenticationMode::Transient { username, .. } => {
                    return Ok(ProposedCredentials::transient(username.clone()))
                }
            }
        };

        ProposedCredentials::new_connect(full_name, username, password_raw, settings).await
    }

    /// Replaces the internal toolset and resets version tracking.
    /// This should ONLY be called during the PRE_CONNECT stage for session initialization.
    /// Properly resets latest_usable_version and declared_next_version to match the new toolset,
    /// preventing version mismatch bugs on reconnection.
    pub fn on_session_init(&self, toolset: Toolset<R>) {
        self.get_session_crypto().replace_toolset_and_reset(toolset);
    }

    /// This should ONLY be used for recovery mode
    pub fn get_static_auxiliary_ratchet(&self) -> R {
        self.get_session_crypto()
            .toolset()
            .read()
            .get_static_auxiliary_ratchet()
            .clone()
    }

    /// Allows shared interior access
    pub fn read(&self) -> RwLockReadGuard<'_, AccountState> {
        self.inner.peer_state.read()
    }

    /// Allows exclusive interior access
    pub fn write(&self) -> RwLockWriteGuard<'_, AccountState> {
        self.inner.peer_state.write()
    }

    /*
           Start of the mutual peer-related functions
    */

    /// Returns a set of hyperlan peers
    pub(crate) fn get_hyperlan_peer_list(&self) -> Option<Vec<u64>> {
        let this = self.read();
        let hyperlan_peers = this.mutuals.get_vec(&HYPERLAN_IDX)?;
        Some(
            hyperlan_peers
                .iter()
                .map(|peer| peer.cid)
                .collect::<Vec<u64>>(),
        )
    }

    /// Returns a set of hyperlan peers
    pub(crate) fn get_hyperlan_peer_mutuals(&self) -> Option<Vec<MutualPeer>> {
        let this = self.read();
        this.mutuals.get_vec(&HYPERLAN_IDX).cloned()
    }

    /// Returns a set of hyperlan peers
    #[allow(dead_code)]
    pub(crate) fn get_hyperwan_peer_list(&self, icid: u64) -> Option<Vec<u64>> {
        let this = self.read();
        let hyperwan_peers = this.mutuals.get_vec(&icid)?;
        Some(
            hyperwan_peers
                .iter()
                .map(|peer| peer.cid)
                .collect::<Vec<u64>>(),
        )
    }

    /// Gets the desired HyperLAN peer by CID (clones)
    pub(crate) fn get_hyperlan_peer(&self, cid: u64) -> Option<MutualPeer> {
        let read = self.read();
        let hyperlan_peers = read.mutuals.get_vec(&HYPERLAN_IDX)?;
        hyperlan_peers.iter().find(|peer| peer.cid == cid).cloned()
    }

    /// Returns the wanted peers
    pub(crate) fn get_hyperlan_peers(&self, peers: impl AsRef<[u64]>) -> Option<Vec<MutualPeer>> {
        let read = self.read();
        let peers = peers.as_ref();
        let hyperlan_peers = read.mutuals.get_vec(&HYPERLAN_IDX)?;
        Some(
            peers
                .iter()
                .filter_map(|peer_wanted| {
                    hyperlan_peers
                        .iter()
                        .find(|peer| peer.cid == *peer_wanted)
                        .cloned()
                })
                .collect(),
        )
    }

    /// This function handles the registration for BOTH CNACs. Then, it synchronizes both to
    #[allow(unused_results)]
    pub(crate) fn register_hyperlan_p2p_as_server(
        &self,
        other_orig: &ClientNetworkAccount<R, Fcm>,
    ) -> Result<(), AccountError> {
        let this_cid = self.inner.cid;
        let other_cid = other_orig.inner.cid;

        let this_username = self.inner.auth_store.username().to_string();
        let other_username = other_orig.inner.auth_store.username().to_string();

        let mut this = self.write();
        let mut other = other_orig.write();

        this.mutuals.insert(
            HYPERLAN_IDX,
            MutualPeer {
                parent_icid: HYPERLAN_IDX,
                cid: other_cid,
                username: Some(other_username),
            },
        );

        other.mutuals.insert(
            HYPERLAN_IDX,
            MutualPeer {
                parent_icid: HYPERLAN_IDX,
                cid: this_cid,
                username: Some(this_username),
            },
        );

        Ok(())
    }

    /// Deregisters two peers as server
    #[allow(unused_results)]
    pub(crate) fn deregister_hyperlan_p2p_as_server(
        &self,
        other: &ClientNetworkAccount<R, Fcm>,
    ) -> Result<(), AccountError> {
        self.remove_hyperlan_peer(other.get_cid())
            .ok_or_else(|| AccountError::account_client_non_exists(other.get_cid()))?;
        other
            .remove_hyperlan_peer(self.get_cid())
            .ok_or_else(|| citadel_io::error!(citadel_io::ErrorCode::PeerRemoveSelfFailed))?;

        Ok(())
    }

    /// Returns a set of registration statuses (true/false) for each co-responding peer. True if registered, false otherwise
    pub(crate) fn hyperlan_peers_exist(&self, peers: impl AsRef<[u64]>) -> Vec<bool> {
        let read = self.read();
        let peers = peers.as_ref();
        if let Some(hyperlan_peers) = read.mutuals.get_vec(&HYPERLAN_IDX) {
            peers
                .iter()
                .map(|peer| {
                    hyperlan_peers
                        .iter()
                        .any(|hyperlan_peer| hyperlan_peer.cid == *peer)
                })
                .collect()
        } else {
            log::warn!(target: "citadel", "Attempted to check hyperlan list, but it does not exists");
            peers.iter().map(|_| false).collect()
        }
    }

    /// Removes any inputs from the internal map that are not present in `peers`. The set `peers` should be
    /// obtained from the HyperLAN Server
    ///
    /// Returns true if the data was mutated
    pub(crate) fn synchronize_hyperlan_peer_list(&self, peers: Vec<MutualPeer>) {
        let mut this = self.write();
        let AccountState { mutuals, .. } = &mut *this;

        let _ = mutuals.remove(&HYPERLAN_IDX);
        mutuals.insert_many(HYPERLAN_IDX, peers);
    }

    /// ONLY run this after you're sure the peer doesn't already exist
    pub(crate) fn insert_hyperlan_peer<T: Into<String>>(&self, cid: u64, username: T) {
        let mut write = self.write();
        let username = Some(username.into());

        write.mutuals.insert(
            HYPERLAN_IDX,
            MutualPeer {
                username,
                parent_icid: HYPERLAN_IDX,
                cid,
            },
        );
    }

    /// Returns Some if success, None otherwise
    #[allow(unused_results)]
    pub(crate) fn remove_hyperlan_peer(&self, cid: u64) -> Option<MutualPeer> {
        log::trace!(target: "citadel", "[remove peer] session_cid: {} | peer_cid: {}", self.get_cid(), cid);
        let mut write = self.write();
        if let Some(hyperlan_peers) = write.mutuals.get_vec_mut(&HYPERLAN_IDX) {
            if let Some(idx) = hyperlan_peers.iter().position(|peer| peer.cid == cid) {
                let removed_peer = hyperlan_peers.remove(idx);
                return Some(removed_peer);
            } else {
                log::warn!(target: "citadel", "Peer {} not found within cnac {}", cid, self.inner.cid);
            }
        }

        None
    }

    /*
        End of the mutual peer-related functions
    */

    /// Generates the serialized bytes
    pub fn generate_proper_bytes(&self) -> Result<Vec<u8>, AccountError>
    where
        Self: SyncIO,
    {
        self.serialize_to_vector()
    }

    /// Returns the metadata for this CNAC
    pub(crate) fn get_metadata(&self) -> CNACMetadata {
        let read = &self.inner;
        let cid = read.cid;
        let username = read.auth_store.username().to_string();
        let full_name = read.auth_store.full_name().to_string();
        let is_personal = read.is_personal;
        let creation_date = read.creation_date.clone();
        CNACMetadata {
            cid,
            username,
            full_name,
            is_personal,
            creation_date,
        }
    }

    /// Returns the information related to the network endpoints (e.g., socket addrs)
    pub fn get_connect_info(&self) -> ConnectionInfo {
        self.inner.adjacent_nac.clone()
    }

    /// Returns the CID
    pub fn get_cid(&self) -> u64 {
        self.inner.cid
    }

    /// Returns true if passwordless
    pub fn is_transient(&self) -> bool {
        self.inner.is_transient
    }
}

impl<R: Ratchet, Fcm: Ratchet> std::fmt::Debug for ClientNetworkAccount<R, Fcm> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "CNAC | CID: {}", self.inner.cid)
    }
}

impl<R: Ratchet, Fcm: Ratchet> std::fmt::Display for ClientNetworkAccount<R, Fcm> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(
            f,
            "{}\t\t{}\t\t{}\t\t{}",
            self.inner.cid,
            self.inner.auth_store.username(),
            self.inner.auth_store.full_name(),
            self.inner.is_personal
        )
    }
}

impl<R: Ratchet, Fcm: Ratchet> Clone for ClientNetworkAccount<R, Fcm> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}