use minijinja::Environment;

static TEMPLATES: &[(&str, &str)] = &[
    ("cbindgen_toml.jinja", include_str!("templates/cbindgen_toml.jinja")),
    ("build_rs.jinja", include_str!("templates/build_rs.jinja")),
    ("last_error.jinja", include_str!("templates/last_error.jinja")),
    ("free_string.jinja", include_str!("templates/free_string.jinja")),
    ("version_fn.jinja", include_str!("templates/version_fn.jinja")),
    (
        "ffi_tokio_runtime.jinja",
        include_str!("templates/ffi_tokio_runtime.jinja"),
    ),
    ("type_from_json.jinja", include_str!("templates/type_from_json.jinja")),
    ("type_to_json.jinja", include_str!("templates/type_to_json.jinja")),
    ("type_free.jinja", include_str!("templates/type_free.jinja")),
    ("type_new.jinja", include_str!("templates/type_new.jinja")),
    (
        "field_accessor_header.jinja",
        include_str!("templates/field_accessor_header.jinja"),
    ),
    ("enum_from_i32.jinja", include_str!("templates/enum_from_i32.jinja")),
    ("enum_to_i32.jinja", include_str!("templates/enum_to_i32.jinja")),
    ("enum_free.jinja", include_str!("templates/enum_free.jinja")),
    ("enum_to_json.jinja", include_str!("templates/enum_to_json.jinja")),
    ("enum_to_string.jinja", include_str!("templates/enum_to_string.jinja")),
    ("enum_from_json.jinja", include_str!("templates/enum_from_json.jinja")),
    (
        "ffi_set_out_error_helper.jinja",
        include_str!("templates/ffi_set_out_error_helper.jinja"),
    ),
    (
        "vtable_ref_delegation_method.jinja",
        include_str!("templates/vtable_ref_delegation_method.jinja"),
    ),
    (
        "vtable_struct_header.jinja",
        include_str!("templates/vtable_struct_header.jinja"),
    ),
    (
        "vtable_free_user_data.jinja",
        include_str!("templates/vtable_free_user_data.jinja"),
    ),
    ("bridge_struct.jinja", include_str!("templates/bridge_struct.jinja")),
    ("bridge_drop.jinja", include_str!("templates/bridge_drop.jinja")),
    (
        "constructor_impl.jinja",
        include_str!("templates/constructor_impl.jinja"),
    ),
    (
        "constructor_slice_cache_init.jinja",
        include_str!("templates/constructor_slice_cache_init.jinja"),
    ),
    (
        "constructor_impl_with_cache.jinja",
        include_str!("templates/constructor_impl_with_cache.jinja"),
    ),
    (
        "constructor_field_init.jinja",
        include_str!("templates/constructor_field_init.jinja"),
    ),
    (
        "plugin_impl_header.jinja",
        include_str!("templates/plugin_impl_header.jinja"),
    ),
    (
        "plugin_impl_version.jinja",
        include_str!("templates/plugin_impl_version.jinja"),
    ),
    (
        "plugin_impl_initialize.jinja",
        include_str!("templates/plugin_impl_initialize.jinja"),
    ),
    (
        "plugin_impl_shutdown.jinja",
        include_str!("templates/plugin_impl_shutdown.jinja"),
    ),
    (
        "register_fn_header.jinja",
        include_str!("templates/register_fn_header.jinja"),
    ),
    (
        "register_fn_vtable_check.jinja",
        include_str!("templates/register_fn_vtable_check.jinja"),
    ),
    (
        "register_fn_body.jinja",
        include_str!("templates/register_fn_body.jinja"),
    ),
    (
        "c_string_conversion.jinja",
        include_str!("templates/c_string_conversion.jinja"),
    ),
    ("unsafe_fn_sig.jinja", include_str!("templates/unsafe_fn_sig.jinja")),
    (
        "value_to_c_conversion.jinja",
        include_str!("templates/value_to_c_conversion.jinja"),
    ),
    ("unregister_fn.jinja", include_str!("templates/unregister_fn.jinja")),
    ("clear_fn.jinja", include_str!("templates/clear_fn.jinja")),
    (
        "vtable_delegation_method.jinja",
        include_str!("templates/vtable_delegation_method.jinja"),
    ),
    (
        "vtable_super_trait_methods.jinja",
        include_str!("templates/vtable_super_trait_methods.jinja"),
    ),
    (
        "doc_comment_lines.jinja",
        include_str!("templates/doc_comment_lines.jinja"),
    ),
    (
        "doc_comment_lines_indented.jinja",
        include_str!("templates/doc_comment_lines_indented.jinja"),
    ),
    (
        "method_safety_doc.jinja",
        include_str!("templates/method_safety_doc.jinja"),
    ),
    (
        "streaming_method_wrapper.jinja",
        include_str!("templates/streaming_method_wrapper.jinja"),
    ),
    (
        "method_wrapper_header.jinja",
        include_str!("templates/method_wrapper_header.jinja"),
    ),
    (
        "free_function_header.jinja",
        include_str!("templates/free_function_header.jinja"),
    ),
    (
        "null_check_self_ref.jinja",
        include_str!("templates/null_check_self_ref.jinja"),
    ),
    (
        "null_check_self_mut.jinja",
        include_str!("templates/null_check_self_mut.jinja"),
    ),
    (
        "null_check_self_owned.jinja",
        include_str!("templates/null_check_self_owned.jinja"),
    ),
    (
        "error_match_void.jinja",
        include_str!("templates/error_match_void.jinja"),
    ),
    (
        "error_match_non_void.jinja",
        include_str!("templates/error_match_non_void.jinja"),
    ),
    (
        "param_optional_string_conversion.jinja",
        include_str!("templates/param_optional_string_conversion.jinja"),
    ),
    (
        "param_non_optional_string_conversion.jinja",
        include_str!("templates/param_non_optional_string_conversion.jinja"),
    ),
    (
        "param_optional_json_conversion.jinja",
        include_str!("templates/param_optional_json_conversion.jinja"),
    ),
    (
        "param_non_optional_json_conversion.jinja",
        include_str!("templates/param_non_optional_json_conversion.jinja"),
    ),
    (
        "param_optional_bool_conversion.jinja",
        include_str!("templates/param_optional_bool_conversion.jinja"),
    ),
    (
        "param_optional_numeric_conversion.jinja",
        include_str!("templates/param_optional_numeric_conversion.jinja"),
    ),
    (
        "param_optional_vec_map_conversion.jinja",
        include_str!("templates/param_optional_vec_map_conversion.jinja"),
    ),
    (
        "vtable_method_doc_line.jinja",
        include_str!("templates/vtable_method_doc_line.jinja"),
    ),
    (
        "vtable_method_doc_lines.jinja",
        include_str!("templates/vtable_method_doc_lines.jinja"),
    ),
    (
        "vtable_method_field.jinja",
        include_str!("templates/vtable_method_field.jinja"),
    ),
    ("code_line.jinja", include_str!("templates/code_line.jinja")),
    (
        "match_field_start.jinja",
        include_str!("templates/match_field_start.jinja"),
    ),
    ("match_arm_value.jinja", include_str!("templates/match_arm_value.jinja")),
    (
        "bytes_field_access.jinja",
        include_str!("templates/bytes_field_access.jinja"),
    ),
    (
        "emitted_code_block.jinja",
        include_str!("templates/emitted_code_block.jinja"),
    ),
    (
        "owned_value_to_c_bool.jinja",
        include_str!("templates/owned_value_to_c_bool.jinja"),
    ),
    (
        "method_call_inline.jinja",
        include_str!("templates/method_call_inline.jinja"),
    ),
    (
        "method_call_result.jinja",
        include_str!("templates/method_call_result.jinja"),
    ),
    (
        "param_path_conversion.jinja",
        include_str!("templates/param_path_conversion.jinja"),
    ),
    ("vec_string_refs.jinja", include_str!("templates/vec_string_refs.jinja")),
    ("call_inline.jinja", include_str!("templates/call_inline.jinja")),
    (
        "call_with_result.jinja",
        include_str!("templates/call_with_result.jinja"),
    ),
    (
        "static_method_call.jinja",
        include_str!("templates/static_method_call.jinja"),
    ),
    (
        "static_method_call_result.jinja",
        include_str!("templates/static_method_call_result.jinja"),
    ),
    (
        "instance_method_call.jinja",
        include_str!("templates/instance_method_call.jinja"),
    ),
    (
        "instance_method_call_result.jinja",
        include_str!("templates/instance_method_call_result.jinja"),
    ),
    (
        "param_optional_named_conversion.jinja",
        include_str!("templates/param_optional_named_conversion.jinja"),
    ),
    (
        "param_non_optional_path_conversion.jinja",
        include_str!("templates/param_non_optional_path_conversion.jinja"),
    ),
    (
        "param_primitive_bool.jinja",
        include_str!("templates/param_primitive_bool.jinja"),
    ),
    (
        "param_primitive_newtype.jinja",
        include_str!("templates/param_primitive_newtype.jinja"),
    ),
    (
        "param_primitive_passthrough.jinja",
        include_str!("templates/param_primitive_passthrough.jinja"),
    ),
    (
        "param_non_optional_named_conversion.jinja",
        include_str!("templates/param_non_optional_named_conversion.jinja"),
    ),
    (
        "param_non_optional_bytes_conversion.jinja",
        include_str!("templates/param_non_optional_bytes_conversion.jinja"),
    ),
    (
        "param_optional_fallback.jinja",
        include_str!("templates/param_optional_fallback.jinja"),
    ),
    (
        "param_optional_passthrough.jinja",
        include_str!("templates/param_optional_passthrough.jinja"),
    ),
    (
        "param_duration_conversion.jinja",
        include_str!("templates/param_duration_conversion.jinja"),
    ),
    ("formatted_line.jinja", include_str!("templates/formatted_line.jinja")),
    (
        "ffi_async_body_indent.jinja",
        include_str!("templates/ffi_async_body_indent.jinja"),
    ),
    (
        "ffi_async_capture_param.jinja",
        include_str!("templates/ffi_async_capture_param.jinja"),
    ),
    (
        "ffi_async_cached_name_init.jinja",
        include_str!("templates/ffi_async_cached_name_init.jinja"),
    ),
    (
        "ffi_async_map_err_method.jinja",
        include_str!("templates/ffi_async_map_err_method.jinja"),
    ),
    (
        "ffi_async_box_error_map.jinja",
        include_str!("templates/ffi_async_box_error_map.jinja"),
    ),
    (
        "ffi_vtable_extract.jinja",
        include_str!("templates/ffi_vtable_extract.jinja"),
    ),
    (
        "ffi_return_default_4.jinja",
        include_str!("templates/ffi_return_default_4.jinja"),
    ),
    (
        "ffi_return_default_8.jinja",
        include_str!("templates/ffi_return_default_8.jinja"),
    ),
    (
        "ffi_opt_str_storage_and_ptr.jinja",
        include_str!("templates/ffi_opt_str_storage_and_ptr.jinja"),
    ),
    (
        "ffi_opt_json_storage_open.jinja",
        include_str!("templates/ffi_opt_json_storage_open.jinja"),
    ),
    (
        "ffi_opt_nullable_ptr.jinja",
        include_str!("templates/ffi_opt_nullable_ptr.jinja"),
    ),
    (
        "ffi_cs_match_open.jinja",
        include_str!("templates/ffi_cs_match_open.jinja"),
    ),
    (
        "ffi_nul_byte_param_err.jinja",
        include_str!("templates/ffi_nul_byte_param_err.jinja"),
    ),
    ("ffi_cs_as_ptr.jinja", include_str!("templates/ffi_cs_as_ptr.jinja")),
    (
        "ffi_json_to_string.jinja",
        include_str!("templates/ffi_json_to_string.jinja"),
    ),
    (
        "ffi_json_cs_match_open.jinja",
        include_str!("templates/ffi_json_cs_match_open.jinja"),
    ),
    (
        "ffi_nul_byte_json_err.jinja",
        include_str!("templates/ffi_nul_byte_json_err.jinja"),
    ),
    (
        "ffi_nul_byte_json_param_msg.jinja",
        include_str!("templates/ffi_nul_byte_json_param_msg.jinja"),
    ),
    (
        "ffi_nul_byte_param_msg.jinja",
        include_str!("templates/ffi_nul_byte_param_msg.jinja"),
    ),
    (
        "ffi_unsafe_fp_call.jinja",
        include_str!("templates/ffi_unsafe_fp_call.jinja"),
    ),
    (
        "ffi_serde_from_str_err.jinja",
        include_str!("templates/ffi_serde_from_str_err.jinja"),
    ),
    (
        "ffi_sync_serde_from_str_err.jinja",
        include_str!("templates/ffi_sync_serde_from_str_err.jinja"),
    ),
    ("ffi_ok_rc_as.jinja", include_str!("templates/ffi_ok_rc_as.jinja")),
    (
        "ffi_serde_from_str_default.jinja",
        include_str!("templates/ffi_serde_from_str_default.jinja"),
    ),
    (
        "ffi_decode_string_result.jinja",
        include_str!("templates/ffi_decode_string_result.jinja"),
    ),
    (
        "ffi_decode_string_value.jinja",
        include_str!("templates/ffi_decode_string_value.jinja"),
    ),
    (
        "ffi_vtable_error_check.jinja",
        include_str!("templates/ffi_vtable_error_check.jinja"),
    ),
    (
        "ffi_vtable_not_initialised_msg.jinja",
        include_str!("templates/ffi_vtable_not_initialised_msg.jinja"),
    ),
    (
        "ffi_vtable_null_out_result_msg.jinja",
        include_str!("templates/ffi_vtable_null_out_result_msg.jinja"),
    ),
    (
        "ffi_unsafe_fp_tail.jinja",
        include_str!("templates/ffi_unsafe_fp_tail.jinja"),
    ),
    (
        "bytes_result_null_check.jinja",
        include_str!("templates/bytes_result_null_check.jinja"),
    ),
    (
        "bytes_result_match.jinja",
        include_str!("templates/bytes_result_match.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}"))
}
