//   Copyright 2023 The Tari Project
//   SPDX-License-Identifier: BSD-3-Clause

use std::{collections::HashSet, str::FromStr};

use tari_engine_types::substate::SubstateId;
use tari_ootle_common_types::{VersionedSubstateIdRef, optional::Optional};
use tari_ootle_wallet_sdk::storage::{CommittableStore, WalletStoreReader, WalletStoreWriter, WriteableWalletStore};
use tari_ootle_wallet_storage_sqlite::SqliteWalletStore;

#[test]
fn get_and_insert_substates() {
    let example_addr = "component_1f019e4d434cbf2b99c0af89ee212f422af86de7280a169d2e392dfbffffffff"
        .parse()
        .unwrap();

    let db = SqliteWalletStore::try_open(":memory:").unwrap();
    db.run_migrations().unwrap();
    let mut tx = db.create_write_tx().unwrap();
    let substate = tx.substates_get(&example_addr).optional().unwrap();
    assert!(substate.is_none());
    let address =
        SubstateId::from_str("component_1f019e4d434cbf2b99c0af89ee212f422af86de7280a169d2e392dfbffffffff").unwrap();
    tx.substates_upsert_root(VersionedSubstateIdRef::new(&address, 0), HashSet::new(), None, None)
        .unwrap();

    let child_address =
        SubstateId::from_str("component_d9e4a7ce7dbaa73ce10aabf309dd702054756a813f454ef13564f298ffffffff").unwrap();
    tx.substates_upsert_child(&address, VersionedSubstateIdRef::new(&child_address, 0), HashSet::new())
        .unwrap();

    tx.commit().unwrap();

    let returned = tx.substates_get(&address).unwrap();
    assert!(returned.parent_address.is_none());
    assert_eq!(*returned.substate_id.substate_id(), address);
    assert_eq!(returned.substate_id.version(), 0);

    let returned = tx.substates_get(&child_address).unwrap();
    assert_eq!(returned.parent_address, Some(address));
    assert_eq!(*returned.substate_id.substate_id(), child_address);
    assert_eq!(returned.substate_id.version(), 0);
}
