#![cfg(test)]

use super::*;

pub use crate as ghost_governance;
use frame_support::{
    derive_impl, parameter_types,
    traits::{ConstU32, ConstU128},
};
use frame_system::EnsureRoot;
use sp_runtime::BuildStorage;

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
    type RuntimeOrigin = RuntimeOrigin;
    type RuntimeCall = RuntimeCall;
    type Block = Block;
    type RuntimeEvent = RuntimeEvent;
    type AccountData = pallet_balances::AccountData<u128>;
    type MaxConsumers = frame_support::traits::ConstU32<16>;

    type AccountId = sp_runtime::AccountId32;
    type Lookup = sp_runtime::traits::IdentityLookup<Self::AccountId>;
}

#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for Test {
    type ExistentialDeposit = frame_support::traits::ConstU128<1>;
    type AccountStore = System;
    type Balance = u128;
}

parameter_types! {
    pub const MaxNetworks: u32 = 5;
}

impl ghost_networks::Config for Test {
    type RuntimeEvent = RuntimeEvent;
    type Currency = Balances;
    type NetworkId = u64;
    type RegisterOrigin = EnsureRoot<Self::AccountId>;
    type UpdateOrigin = EnsureRoot<Self::AccountId>;
    type RemoveOrigin = EnsureRoot<Self::AccountId>;
    type MaxNetworks = MaxNetworks;
    type WeightInfo = ();
}

impl Config for Test {
    type RuntimeEvent = RuntimeEvent;

    type Currency = Balances;
    type NetworkDataHandler = Networks;

    type MinimumDonation = ConstU128<69>;
    type MaxProofDepth = ConstU32<5>;

    type WeightInfo = ();
}

type Block = frame_system::mocking::MockBlock<Test>;

frame_support::construct_runtime!(
    pub enum Test
    {
        System: frame_system,
        Balances: pallet_balances,
        Networks: ghost_networks,
        Governance: ghost_governance,
    }
);

pub fn new_test_ext() -> sp_io::TestExternalities {
    let mut t = frame_system::GenesisConfig::<Test>::default()
        .build_storage()
        .unwrap();

    ghost_governance::GenesisConfig::<Test> {
        network_claims: Default::default(),
    }
    .assimilate_storage(&mut t)
    .unwrap();

    let mut ext = sp_io::TestExternalities::new(t);
    ext.execute_with(|| {
        System::set_block_number(1);
    });

    ext
}
