use minijinja::Environment;

static TEMPLATES: &[(&str, &str)] = &[
    (
        "doc_emission/phpdoc.jinja",
        include_str!("templates/doc_emission/phpdoc.jinja"),
    ),
    (
        "doc_emission/csharp_doc_summary.jinja",
        include_str!("templates/doc_emission/csharp_doc_summary.jinja"),
    ),
    (
        "doc_emission/csharp_doc_sections.jinja",
        include_str!("templates/doc_emission/csharp_doc_sections.jinja"),
    ),
    (
        "doc_emission/elixir_doc.jinja",
        include_str!("templates/doc_emission/elixir_doc.jinja"),
    ),
    (
        "doc_emission/rustdoc.jinja",
        include_str!("templates/doc_emission/rustdoc.jinja"),
    ),
    (
        "doc_emission/roxygen.jinja",
        include_str!("templates/doc_emission/roxygen.jinja"),
    ),
    (
        "doc_emission/swift_doc.jinja",
        include_str!("templates/doc_emission/swift_doc.jinja"),
    ),
    (
        "doc_emission/javadoc.jinja",
        include_str!("templates/doc_emission/javadoc.jinja"),
    ),
    (
        "doc_emission/kdoc.jinja",
        include_str!("templates/doc_emission/kdoc.jinja"),
    ),
    (
        "doc_emission/dartdoc.jinja",
        include_str!("templates/doc_emission/dartdoc.jinja"),
    ),
    (
        "doc_emission/gleam_doc.jinja",
        include_str!("templates/doc_emission/gleam_doc.jinja"),
    ),
    (
        "doc_emission/zig_doc.jinja",
        include_str!("templates/doc_emission/zig_doc.jinja"),
    ),
    (
        "binding_helpers/arc_wrap.jinja",
        include_str!("templates/binding_helpers/arc_wrap.jinja"),
    ),
    (
        "binding_helpers/async_body_pyo3.jinja",
        include_str!("templates/binding_helpers/async_body_pyo3.jinja"),
    ),
    (
        "binding_helpers/async_body_wasm.jinja",
        include_str!("templates/binding_helpers/async_body_wasm.jinja"),
    ),
    (
        "binding_helpers/async_body_napi.jinja",
        include_str!("templates/binding_helpers/async_body_napi.jinja"),
    ),
    (
        "binding_helpers/async_body_tokio.jinja",
        include_str!("templates/binding_helpers/async_body_tokio.jinja"),
    ),
    (
        "binding_helpers/return_newtype_unwrap.jinja",
        include_str!("templates/binding_helpers/return_newtype_unwrap.jinja"),
    ),
    (
        "binding_helpers/named_let_binding_simple.jinja",
        include_str!("templates/binding_helpers/named_let_binding_simple.jinja"),
    ),
    (
        "binding_helpers/named_let_binding_optional.jinja",
        include_str!("templates/binding_helpers/named_let_binding_optional.jinja"),
    ),
    (
        "binding_helpers/named_let_binding_promoted.jinja",
        include_str!("templates/binding_helpers/named_let_binding_promoted.jinja"),
    ),
    (
        "binding_helpers/named_let_binding_optional_ref.jinja",
        include_str!("templates/binding_helpers/named_let_binding_optional_ref.jinja"),
    ),
    (
        "binding_helpers/vec_named_let_binding_optional.jinja",
        include_str!("templates/binding_helpers/vec_named_let_binding_optional.jinja"),
    ),
    (
        "binding_helpers/vec_named_let_binding_optional_no_ref.jinja",
        include_str!("templates/binding_helpers/vec_named_let_binding_optional_no_ref.jinja"),
    ),
    (
        "binding_helpers/vec_named_let_binding_promoted.jinja",
        include_str!("templates/binding_helpers/vec_named_let_binding_promoted.jinja"),
    ),
    (
        "binding_helpers/vec_named_let_binding_simple.jinja",
        include_str!("templates/binding_helpers/vec_named_let_binding_simple.jinja"),
    ),
    (
        "binding_helpers/vec_string_refs_binding_optional.jinja",
        include_str!("templates/binding_helpers/vec_string_refs_binding_optional.jinja"),
    ),
    (
        "binding_helpers/vec_string_refs_binding_simple.jinja",
        include_str!("templates/binding_helpers/vec_string_refs_binding_simple.jinja"),
    ),
    (
        "binding_helpers/named_let_binding_by_ref_simple.jinja",
        include_str!("templates/binding_helpers/named_let_binding_by_ref_simple.jinja"),
    ),
    (
        "binding_helpers/named_let_binding_by_ref_optional.jinja",
        include_str!("templates/binding_helpers/named_let_binding_by_ref_optional.jinja"),
    ),
    (
        "binding_helpers/named_let_binding_by_ref_promoted.jinja",
        include_str!("templates/binding_helpers/named_let_binding_by_ref_promoted.jinja"),
    ),
    (
        "binding_helpers/vec_named_let_binding_by_ref_optional.jinja",
        include_str!("templates/binding_helpers/vec_named_let_binding_by_ref_optional.jinja"),
    ),
    (
        "binding_helpers/vec_named_let_binding_by_ref_promoted.jinja",
        include_str!("templates/binding_helpers/vec_named_let_binding_by_ref_promoted.jinja"),
    ),
    (
        "binding_helpers/vec_named_let_binding_by_ref_simple.jinja",
        include_str!("templates/binding_helpers/vec_named_let_binding_by_ref_simple.jinja"),
    ),
    (
        "binding_helpers/struct_field_line.jinja",
        include_str!("templates/binding_helpers/struct_field_line.jinja"),
    ),
    (
        "binding_helpers/struct_field_default.jinja",
        include_str!("templates/binding_helpers/struct_field_default.jinja"),
    ),
    (
        "binding_helpers/serde_named_let_binding_optional.jinja",
        include_str!("templates/binding_helpers/serde_named_let_binding_optional.jinja"),
    ),
    (
        "binding_helpers/serde_named_let_binding_promoted.jinja",
        include_str!("templates/binding_helpers/serde_named_let_binding_promoted.jinja"),
    ),
    (
        "binding_helpers/serde_named_let_binding_simple.jinja",
        include_str!("templates/binding_helpers/serde_named_let_binding_simple.jinja"),
    ),
    (
        "binding_helpers/serde_vec_named_optional.jinja",
        include_str!("templates/binding_helpers/serde_vec_named_optional.jinja"),
    ),
    (
        "binding_helpers/serde_vec_named_simple.jinja",
        include_str!("templates/binding_helpers/serde_vec_named_simple.jinja"),
    ),
    (
        "binding_helpers/sanitized_vec_string_filter_optional.jinja",
        include_str!("templates/binding_helpers/sanitized_vec_string_filter_optional.jinja"),
    ),
    (
        "binding_helpers/sanitized_vec_string_filter_simple.jinja",
        include_str!("templates/binding_helpers/sanitized_vec_string_filter_simple.jinja"),
    ),
    (
        "binding_helpers/serde_sanitized_vec_string_optional.jinja",
        include_str!("templates/binding_helpers/serde_sanitized_vec_string_optional.jinja"),
    ),
    (
        "binding_helpers/serde_sanitized_vec_string_simple.jinja",
        include_str!("templates/binding_helpers/serde_sanitized_vec_string_simple.jinja"),
    ),
    (
        "conversions/enum_from_binding_to_core",
        include_str!("templates/conversions/enum_from_binding_to_core.jinja"),
    ),
    (
        "conversions/enum_from_core_to_binding",
        include_str!("templates/conversions/enum_from_core_to_binding.jinja"),
    ),
    (
        "conversions/core_to_binding_impl",
        include_str!("templates/conversions/core_to_binding_impl.jinja"),
    ),
    (
        "conversions/binding_to_core_impl",
        include_str!("templates/conversions/binding_to_core_impl.jinja"),
    ),
    (
        "conversions/private_field_from_impl",
        include_str!("templates/conversions/private_field_from_impl.jinja"),
    ),
    (
        "structs/default_impl.jinja",
        include_str!("templates/generators/structs/default_impl.jinja"),
    ),
    (
        "structs/opaque_struct.jinja",
        include_str!("templates/generators/structs/opaque_struct.jinja"),
    ),
    (
        "error_gen/pyo3_error_types.jinja",
        include_str!("templates/error_gen/pyo3_error_types.jinja"),
    ),
    (
        "error_gen/pyo3_error_converter.jinja",
        include_str!("templates/error_gen/pyo3_error_converter.jinja"),
    ),
    (
        "error_gen/napi_error_types.jinja",
        include_str!("templates/error_gen/napi_error_types.jinja"),
    ),
    (
        "error_gen/napi_error_converter.jinja",
        include_str!("templates/error_gen/napi_error_converter.jinja"),
    ),
    (
        "error_gen/wasm_error_code_fn.jinja",
        include_str!("templates/error_gen/wasm_error_code_fn.jinja"),
    ),
    (
        "error_gen/wasm_error_converter.jinja",
        include_str!("templates/error_gen/wasm_error_converter.jinja"),
    ),
    (
        "error_gen/php_error_converter.jinja",
        include_str!("templates/error_gen/php_error_converter.jinja"),
    ),
    (
        "error_gen/magnus_error_converter.jinja",
        include_str!("templates/error_gen/magnus_error_converter.jinja"),
    ),
    (
        "error_gen/rustler_error_converter.jinja",
        include_str!("templates/error_gen/rustler_error_converter.jinja"),
    ),
    (
        "error_gen/ffi_error_codes.jinja",
        include_str!("templates/error_gen/ffi_error_codes.jinja"),
    ),
    (
        "error_gen/go_sentinel_errors.jinja",
        include_str!("templates/error_gen/go_sentinel_errors.jinja"),
    ),
    (
        "error_gen/go_error_struct.jinja",
        include_str!("templates/error_gen/go_error_struct.jinja"),
    ),
    (
        "error_gen/java_error_base.jinja",
        include_str!("templates/error_gen/java_error_base.jinja"),
    ),
    (
        "error_gen/java_error_variant.jinja",
        include_str!("templates/error_gen/java_error_variant.jinja"),
    ),
    (
        "error_gen/csharp_error_base.jinja",
        include_str!("templates/error_gen/csharp_error_base.jinja"),
    ),
    (
        "error_gen/csharp_error_variant.jinja",
        include_str!("templates/error_gen/csharp_error_variant.jinja"),
    ),
    (
        "generators/methods/constructor.jinja",
        include_str!("templates/generators/methods/constructor.jinja"),
    ),
    (
        "generators/methods/method_signature.jinja",
        include_str!("templates/generators/methods/method_signature.jinja"),
    ),
    (
        "generators/methods/impl_block.jinja",
        include_str!("templates/generators/methods/impl_block.jinja"),
    ),
    (
        "generators/enums/pyo3_data_enum.jinja",
        include_str!("templates/generators/enums/pyo3_data_enum.jinja"),
    ),
    (
        "generators/enums/pyo3_factory_method.jinja",
        include_str!("templates/generators/enums/pyo3_factory_method.jinja"),
    ),
    (
        "generators/enums/pyo3_variant_constructor_body.jinja",
        include_str!("templates/generators/enums/pyo3_variant_constructor_body.jinja"),
    ),
    (
        "generators/enums/enum_definition.jinja",
        include_str!("templates/generators/enums/enum_definition.jinja"),
    ),
    (
        "generators/enums/enum_string_methods.jinja",
        include_str!("templates/generators/enums/enum_string_methods.jinja"),
    ),
    (
        "generators/trait_bridge/wrapper_struct.jinja",
        include_str!("templates/generators/trait_bridge/wrapper_struct.jinja"),
    ),
    (
        "generators/trait_bridge/debug_impl.jinja",
        include_str!("templates/generators/trait_bridge/debug_impl.jinja"),
    ),
    (
        "generators/trait_bridge/plugin_impl.jinja",
        include_str!("templates/generators/trait_bridge/plugin_impl.jinja"),
    ),
    (
        "generators/trait_bridge/trait_impl.jinja",
        include_str!("templates/generators/trait_bridge/trait_impl.jinja"),
    ),
    (
        "generators/functions/function_definition.jinja",
        include_str!("templates/generators/functions/function_definition.jinja"),
    ),
    (
        "config_gen/pyo3_kwargs_constructor.jinja",
        include_str!("templates/config_gen/pyo3_kwargs_constructor.jinja"),
    ),
    (
        "config_gen/napi_defaults_constructor.jinja",
        include_str!("templates/config_gen/napi_defaults_constructor.jinja"),
    ),
    (
        "config_gen/go_functional_options.jinja",
        include_str!("templates/config_gen/go_functional_options.jinja"),
    ),
    (
        "config_gen/java_builder.jinja",
        include_str!("templates/config_gen/java_builder.jinja"),
    ),
    (
        "config_gen/csharp_record.jinja",
        include_str!("templates/config_gen/csharp_record.jinja"),
    ),
    (
        "config_gen/magnus_hash_constructor.jinja",
        include_str!("templates/config_gen/magnus_hash_constructor.jinja"),
    ),
    (
        "config_gen/php_kwargs_constructor.jinja",
        include_str!("templates/config_gen/php_kwargs_constructor.jinja"),
    ),
    (
        "config_gen/rustler_kwargs_constructor.jinja",
        include_str!("templates/config_gen/rustler_kwargs_constructor.jinja"),
    ),
    (
        "config_gen/extendr_kwargs_constructor.jinja",
        include_str!("templates/config_gen/extendr_kwargs_constructor.jinja"),
    ),
    (
        "builders/inner_attribute.jinja",
        include_str!("templates/builders/inner_attribute.jinja"),
    ),
    (
        "builders/use_import.jinja",
        include_str!("templates/builders/use_import.jinja"),
    ),
    (
        "builders/derive_attr.jinja",
        include_str!("templates/builders/derive_attr.jinja"),
    ),
    (
        "builders/generic_attr.jinja",
        include_str!("templates/builders/generic_attr.jinja"),
    ),
    (
        "builders/struct_header.jinja",
        include_str!("templates/builders/struct_header.jinja"),
    ),
    (
        "builders/doc_line.jinja",
        include_str!("templates/builders/doc_line.jinja"),
    ),
    (
        "builders/struct_field.jinja",
        include_str!("templates/builders/struct_field.jinja"),
    ),
    (
        "builders/impl_header.jinja",
        include_str!("templates/builders/impl_header.jinja"),
    ),
    (
        "builders/indented_line.jinja",
        include_str!("templates/builders/indented_line.jinja"),
    ),
    (
        "binding_helpers/default_field.jinja",
        include_str!("templates/binding_helpers/default_field.jinja"),
    ),
    (
        "generators/enums/getter_accessor.jinja",
        include_str!("templates/generators/enums/getter_accessor.jinja"),
    ),
    (
        "generators/enums/match_variant.jinja",
        include_str!("templates/generators/enums/match_variant.jinja"),
    ),
    (
        "generators/enums/py_dict_getter.jinja",
        include_str!("templates/generators/enums/py_dict_getter.jinja"),
    ),
    (
        "generators/enums/tag_field_check.jinja",
        include_str!("templates/generators/enums/tag_field_check.jinja"),
    ),
    (
        "generators/enums/variant_tag_match.jinja",
        include_str!("templates/generators/enums/variant_tag_match.jinja"),
    ),
    (
        "generators/enums/tag_getter_header.jinja",
        include_str!("templates/generators/enums/tag_getter_header.jinja"),
    ),
    (
        "generators/enums/json_get_field.jinja",
        include_str!("templates/generators/enums/json_get_field.jinja"),
    ),
    (
        "generators/enums/json_get_error.jinja",
        include_str!("templates/generators/enums/json_get_error.jinja"),
    ),
    (
        "generators/methods/static_attr.jinja",
        include_str!("templates/generators/methods/static_attr.jinja"),
    ),
    (
        "generators/methods/signature_attr.jinja",
        include_str!("templates/generators/methods/signature_attr.jinja"),
    ),
    (
        "generators/methods/method_body.jinja",
        include_str!("templates/generators/methods/method_body.jinja"),
    ),
    (
        "generators/trait_bridge/trait_method.jinja",
        include_str!("templates/generators/trait_bridge/trait_method.jinja"),
    ),
    ("doc_jsdoc_param.jinja", include_str!("templates/doc_jsdoc_param.jinja")),
    (
        "doc_jsdoc_param_desc.jinja",
        include_str!("templates/doc_jsdoc_param_desc.jinja"),
    ),
    (
        "doc_jsdoc_returns.jinja",
        include_str!("templates/doc_jsdoc_returns.jinja"),
    ),
    (
        "doc_jsdoc_throws.jinja",
        include_str!("templates/doc_jsdoc_throws.jinja"),
    ),
    (
        "doc_javadoc_param.jinja",
        include_str!("templates/doc_javadoc_param.jinja"),
    ),
    (
        "doc_javadoc_param_desc.jinja",
        include_str!("templates/doc_javadoc_param_desc.jinja"),
    ),
    (
        "doc_javadoc_return.jinja",
        include_str!("templates/doc_javadoc_return.jinja"),
    ),
    (
        "doc_javadoc_throws.jinja",
        include_str!("templates/doc_javadoc_throws.jinja"),
    ),
    (
        "doc_csharp_param.jinja",
        include_str!("templates/doc_csharp_param.jinja"),
    ),
    (
        "doc_csharp_param_desc.jinja",
        include_str!("templates/doc_csharp_param_desc.jinja"),
    ),
    (
        "doc_csharp_returns.jinja",
        include_str!("templates/doc_csharp_returns.jinja"),
    ),
    (
        "doc_csharp_exception.jinja",
        include_str!("templates/doc_csharp_exception.jinja"),
    ),
    (
        "doc_phpdoc_param.jinja",
        include_str!("templates/doc_phpdoc_param.jinja"),
    ),
    (
        "doc_phpdoc_param_desc.jinja",
        include_str!("templates/doc_phpdoc_param_desc.jinja"),
    ),
    (
        "doc_phpdoc_return.jinja",
        include_str!("templates/doc_phpdoc_return.jinja"),
    ),
    (
        "doc_phpdoc_throws.jinja",
        include_str!("templates/doc_phpdoc_throws.jinja"),
    ),
    (
        "doc_doxygen_param.jinja",
        include_str!("templates/doc_doxygen_param.jinja"),
    ),
    (
        "doc_doxygen_param_desc.jinja",
        include_str!("templates/doc_doxygen_param_desc.jinja"),
    ),
    (
        "doc_doxygen_return.jinja",
        include_str!("templates/doc_doxygen_return.jinja"),
    ),
    (
        "doc_doxygen_errors.jinja",
        include_str!("templates/doc_doxygen_errors.jinja"),
    ),
];

pub(crate) fn make_env() -> Environment<'static> {
    let mut env = Environment::new();
    env.set_trim_blocks(true);
    env.set_lstrip_blocks(true);
    env.set_keep_trailing_newline(true);
    for (name, src) in TEMPLATES {
        env.add_template(name, src).expect("built-in template is valid");
    }
    env
}

pub(crate) fn render(template_name: &str, ctx: minijinja::Value) -> String {
    make_env()
        .get_template(template_name)
        .unwrap_or_else(|_| panic!("template {template_name} not found"))
        .render(ctx)
        .unwrap_or_else(|e| panic!("template {template_name} failed to render: {e}"))
}
