use crate::get_test_file;
use anyhow::Context;
use std::io::Write;

pub fn generate_fragment_tests() -> anyhow::Result<()> {
    let mut test_file = get_test_file("fragment_tests.rs")?;

    for (line, fragment) in include_str!("../resources/tests/parsers/multi_char/fragment/valid.txt").lines().enumerate()
    {
        write!(
            test_file,
            include_str!("../resources/tests/parsers/multi_char/fragment/template.txt"),
            number = format!("{:03}", line),
            fragment = fragment
        )
        .context(format!("failed to write test fragment::case_{}", line))?;
    }

    Ok(())
}

pub fn generate_param_tests() -> anyhow::Result<()> {
    let mut test_file = get_test_file("param_tests.rs")?;

    for (line, param) in include_str!("../resources/tests/parsers/multi_char/param/valid.txt").lines().enumerate() {
        write!(
            test_file,
            include_str!("../resources/tests/parsers/multi_char/param/template.txt"),
            number = format!("{:03}", line),
            param = param
        )
        .context(format!("failed to write test param::case_{}", line))?;
    }

    Ok(())
}

pub fn generate_port_tests() -> anyhow::Result<()> {
    let mut test_file = get_test_file("port_tests.rs")?;

    for port in include_str!("../resources/tests/parsers/multi_char/port/valid.txt").lines() {
        write!(test_file, include_str!("../resources/tests/parsers/multi_char/port/template.txt"), port = port)
            .context(format!("failed to write test port::port_{}", port))?;
    }

    Ok(())
}

pub fn generate_query_tests() -> anyhow::Result<()> {
    let mut test_file = get_test_file("query_tests.rs")?;

    for (line, query) in include_str!("../resources/tests/parsers/multi_char/fragment/valid.txt").lines().enumerate() {
        write!(
            test_file,
            include_str!("../resources/tests/parsers/multi_char/query/template.txt"),
            number = format!("{:03}", line),
            query = query
        )
        .context(format!("failed to write test query::case_{}", line))?;
    }

    Ok(())
}

pub fn generate_reg_name_tests() -> anyhow::Result<()> {
    let mut test_file = get_test_file("reg_name_tests.rs")?;

    for (line, name) in include_str!("../resources/tests/parsers/multi_char/reg_name/valid.txt").lines().enumerate() {
        write!(
            test_file,
            include_str!("../resources/tests/parsers/multi_char/reg_name/template.txt"),
            number = format!("{:03}", line),
            name = name
        )
        .context(format!("failed to write test reg_name::case_{}", line))?;
    }

    Ok(())
}

pub fn generate_rel_segment_tests() -> anyhow::Result<()> {
    let mut test_file = get_test_file("rel_segment_tests.rs")?;

    for (line, segment) in
        include_str!("../resources/tests/parsers/multi_char/rel_segment/valid.txt").lines().enumerate()
    {
        write!(
            test_file,
            include_str!("../resources/tests/parsers/multi_char/rel_segment/template.txt"),
            number = format!("{:03}", line),
            segment = segment
        )
        .context(format!("failed to write test rel_segment::case_{}", line))?;
    }

    Ok(())
}

pub fn generate_user_info_tests() -> anyhow::Result<()> {
    let mut test_file = get_test_file("user_info_tests.rs")?;

    for (line, info) in include_str!("../resources/tests/parsers/multi_char/user_info/valid.txt").lines().enumerate() {
        write!(
            test_file,
            include_str!("../resources/tests/parsers/multi_char/user_info/template.txt"),
            number = format!("{:03}", line),
            info = info
        )
        .context(format!("failed to write test user_info::case_{}", line))?;
    }

    Ok(())
}
