use rand::distributions::uniform::SampleUniform;
use rand::distributions::{Alphanumeric, DistString};
use rand::Rng;

pub fn random_range<T>(min: T, max: T) -> T
where
    T: SampleUniform + PartialOrd + Copy,
{
    rand::thread_rng().gen_range(min..=max)
}

pub fn random_string(len: usize) -> String {
    Alphanumeric.sample_string(&mut rand::thread_rng(), len)
}

pub fn random_identifier(len: usize) -> String {
    let mut rng = rand::thread_rng();
    let first = rng.gen_range(b'a'..=b'z') as char;
    let rest = Alphanumeric.sample_string(&mut rng, len.saturating_sub(1));
    let mut out = String::with_capacity(len.max(1));
    out.push(first);
    out.push_str(&rest);
    out
}

pub fn random_arch_name() -> String {
    let syllables = [
        "za", "xi", "qo", "vu", "re", "ka", "tri", "mo", "lu", "pha", "nex", "zor", "ish", "ul",
        "vek", "tor", "ryn", "qua", "oth", "gar",
    ];
    let mut rng = rand::thread_rng();
    let parts = rng.gen_range(2usize..=4usize);
    let mut out = String::new();
    for _ in 0..parts {
        let idx = rng.gen_range(0..syllables.len());
        out.push_str(syllables[idx]);
    }
    let mut chars = out.chars();
    let first = chars.next().expect("No first char");
    let mut capped = String::with_capacity(out.len());
    capped.push(first.to_ascii_uppercase());
    capped.push_str(chars.as_str());
    capped
}

pub fn main_entry() {
    let small_u32 = random_range(1u32, 100u32);
    let signed_i32 = random_range(-50i32, 50i32);
    let word = random_string(12);
    let ident = random_identifier(8);
    let arch = random_arch_name();

    println!("random_range(1u32, 100u32) = {small_u32}");
    println!("random_range(-50i32, 50i32) = {signed_i32}");
    println!("random_string(12) = {word}");
    println!("random_identifier(8) = {ident}");
    println!("random_arch_name() = {arch}");
}
