use minijinja::Environment;

static TEMPLATES: &[(&str, &str)] = &[
    (
        "java_file_header.jinja",
        include_str!("templates/java_file_header.jinja"),
    ),
    ("facade_class.jinja", include_str!("templates/facade_class.jinja")),
    ("facade_file.jinja", include_str!("templates/facade_file.jinja")),
    ("native_lib.jinja", include_str!("templates/native_lib.jinja")),
    ("visitor_bridge.jinja", include_str!("templates/visitor_bridge.jinja")),
    ("trait_interface.jinja", include_str!("templates/trait_interface.jinja")),
    ("trait_bridge.jinja", include_str!("templates/trait_bridge.jinja")),
    (
        "trait_adapter_bridge.jinja",
        include_str!("templates/trait_adapter_bridge.jinja"),
    ),
    (
        "convert_with_visitor.jinja",
        include_str!("templates/convert_with_visitor.jinja"),
    ),
    ("handle_method.jinja", include_str!("templates/handle_method.jinja")),
    (
        "helper_check_last_error.jinja",
        include_str!("templates/helper_check_last_error.jinja"),
    ),
    (
        "helper_object_mapper.jinja",
        include_str!("templates/helper_object_mapper.jinja"),
    ),
    ("javadoc_lines.jinja", include_str!("templates/javadoc_lines.jinja")),
    (
        "helper_read_bytes.jinja",
        include_str!("templates/helper_read_bytes.jinja"),
    ),
    (
        "helper_read_cstring.jinja",
        include_str!("templates/helper_read_cstring.jinja"),
    ),
    (
        "helper_read_json_list.jinja",
        include_str!("templates/helper_read_json_list.jinja"),
    ),
    (
        "native_lib_visitor_handles.jinja",
        include_str!("templates/native_lib_visitor_handles.jinja"),
    ),
    (
        "native_lib_options_set_visitor_handle.jinja",
        include_str!("templates/native_lib_options_set_visitor_handle.jinja"),
    ),
    ("visitor_files.jinja", include_str!("templates/visitor_files.jinja")),
    ("exception_class.jinja", include_str!("templates/exception_class.jinja")),
    ("json_util.jinja", include_str!("templates/json_util.jinja")),
    (
        "infrastructure_exception.jinja",
        include_str!("templates/infrastructure_exception.jinja"),
    ),
    (
        "untagged_union_wrapper.jinja",
        include_str!("templates/untagged_union_wrapper.jinja"),
    ),
    (
        "sealed_union_serializer.jinja",
        include_str!("templates/sealed_union_serializer.jinja"),
    ),
    ("visit_result.jinja", include_str!("templates/visit_result.jinja")),
    (
        "visitor_interface.jinja",
        include_str!("templates/visitor_interface.jinja"),
    ),
    (
        "bridge_class_header.jinja",
        include_str!("templates/bridge_class_header.jinja"),
    ),
    (
        "bridge_constructor_start.jinja",
        include_str!("templates/bridge_constructor_start.jinja"),
    ),
    (
        "bridge_constructor_end.jinja",
        include_str!("templates/bridge_constructor_end.jinja"),
    ),
    (
        "upcall_stub_allocation.jinja",
        include_str!("templates/upcall_stub_allocation.jinja"),
    ),
    ("lifecycle_stub.jinja", include_str!("templates/lifecycle_stub.jinja")),
    (
        "bridge_lifecycle_methods.jinja",
        include_str!("templates/bridge_lifecycle_methods.jinja"),
    ),
    (
        "bridge_method_handler.jinja",
        include_str!("templates/bridge_method_handler.jinja"),
    ),
    (
        "bridge_helper_methods.jinja",
        include_str!("templates/bridge_helper_methods.jinja"),
    ),
    (
        "bridge_register_method.jinja",
        include_str!("templates/bridge_register_method.jinja"),
    ),
    (
        "bridge_unregister_method.jinja",
        include_str!("templates/bridge_unregister_method.jinja"),
    ),
    (
        "ffi_main_class_header.jinja",
        include_str!("templates/ffi_main_class_header.jinja"),
    ),
    (
        "ffi_main_class_footer.jinja",
        include_str!("templates/ffi_main_class_footer.jinja"),
    ),
    (
        "ffi_async_wrapper.jinja",
        include_str!("templates/ffi_async_wrapper.jinja"),
    ),
    (
        "ffi_method_signature.jinja",
        include_str!("templates/ffi_method_signature.jinja"),
    ),
    (
        "ffi_try_finally_block_start.jinja",
        include_str!("templates/ffi_try_finally_block_start.jinja"),
    ),
    ("ffi_imports.jinja", include_str!("templates/ffi_imports.jinja")),
    ("ffi_catch_block.jinja", include_str!("templates/ffi_catch_block.jinja")),
    ("ffi_null_check.jinja", include_str!("templates/ffi_null_check.jinja")),
    (
        "ffi_ffi_ptr_cleanup.jinja",
        include_str!("templates/ffi_ffi_ptr_cleanup.jinja"),
    ),
    (
        "method_handle_nullable.jinja",
        include_str!("templates/method_handle_nullable.jinja"),
    ),
    (
        "method_handle_normal.jinja",
        include_str!("templates/method_handle_normal.jinja"),
    ),
    (
        "method_handle_to_json.jinja",
        include_str!("templates/method_handle_to_json.jinja"),
    ),
    (
        "method_handle_free.jinja",
        include_str!("templates/method_handle_free.jinja"),
    ),
    (
        "method_handle_free_bytes.jinja",
        include_str!("templates/method_handle_free_bytes.jinja"),
    ),
    (
        "bytes_result_call.jinja",
        include_str!("templates/bytes_result_call.jinja"),
    ),
    (
        "method_handle_from_json.jinja",
        include_str!("templates/method_handle_from_json.jinja"),
    ),
    (
        "method_handle_len.jinja",
        include_str!("templates/method_handle_len.jinja"),
    ),
    (
        "method_handle_register.jinja",
        include_str!("templates/method_handle_register.jinja"),
    ),
    (
        "method_handle_unregister.jinja",
        include_str!("templates/method_handle_unregister.jinja"),
    ),
    (
        "method_handle_clear.jinja",
        include_str!("templates/method_handle_clear.jinja"),
    ),
    (
        "bridge_clear_method.jinja",
        include_str!("templates/bridge_clear_method.jinja"),
    ),
    ("marshal_string.jinja", include_str!("templates/marshal_string.jinja")),
    ("marshal_path.jinja", include_str!("templates/marshal_path.jinja")),
    (
        "marshal_opaque_handle.jinja",
        include_str!("templates/marshal_opaque_handle.jinja"),
    ),
    (
        "marshal_named_type.jinja",
        include_str!("templates/marshal_named_type.jinja"),
    ),
    ("marshal_bytes.jinja", include_str!("templates/marshal_bytes.jinja")),
    (
        "marshal_optional_string.jinja",
        include_str!("templates/marshal_optional_string.jinja"),
    ),
    (
        "marshal_optional_path.jinja",
        include_str!("templates/marshal_optional_path.jinja"),
    ),
    (
        "marshal_optional_opaque_handle.jinja",
        include_str!("templates/marshal_optional_opaque_handle.jinja"),
    ),
    (
        "marshal_optional_named_type.jinja",
        include_str!("templates/marshal_optional_named_type.jinja"),
    ),
    (
        "marshal_optional_primitive.jinja",
        include_str!("templates/marshal_optional_primitive.jinja"),
    ),
    ("marshal_vec_map.jinja", include_str!("templates/marshal_vec_map.jinja")),
    (
        "gen_helper_methods_header.jinja",
        include_str!("templates/gen_helper_methods_header.jinja"),
    ),
    (
        "ffi_null_check_with_cleanup.jinja",
        include_str!("templates/ffi_null_check_with_cleanup.jinja"),
    ),
    (
        "ffi_invoke_handle_result.jinja",
        include_str!("templates/ffi_invoke_handle_result.jinja"),
    ),
    (
        "ffi_throw_exception.jinja",
        include_str!("templates/ffi_throw_exception.jinja"),
    ),
    (
        "ffi_return_new_instance.jinja",
        include_str!("templates/ffi_return_new_instance.jinja"),
    ),
    (
        "ffi_invoke_json_ptr.jinja",
        include_str!("templates/ffi_invoke_json_ptr.jinja"),
    ),
    (
        "ffi_invoke_free_string.jinja",
        include_str!("templates/ffi_invoke_free_string.jinja"),
    ),
    (
        "ffi_return_read_json_list_optional.jinja",
        include_str!("templates/ffi_return_read_json_list_optional.jinja"),
    ),
    (
        "ffi_invoke_primitive_result.jinja",
        include_str!("templates/ffi_invoke_primitive_result.jinja"),
    ),
    (
        "ffi_async_method_signature.jinja",
        include_str!("templates/ffi_async_method_signature.jinja"),
    ),
    (
        "convert_with_visitor_signature.jinja",
        include_str!("templates/convert_with_visitor_signature.jinja"),
    ),
    (
        "ffi_conversion_options_invoke.jinja",
        include_str!("templates/ffi_conversion_options_invoke.jinja"),
    ),
    (
        "ffi_visitor_create.jinja",
        include_str!("templates/ffi_visitor_create.jinja"),
    ),
    (
        "ffi_throw_on_null.jinja",
        include_str!("templates/ffi_throw_on_null.jinja"),
    ),
    (
        "ffi_options_set_visitor.jinja",
        include_str!("templates/ffi_options_set_visitor.jinja"),
    ),
    (
        "ffi_convert_invoke.jinja",
        include_str!("templates/ffi_convert_invoke.jinja"),
    ),
    (
        "ffi_options_free_conditional.jinja",
        include_str!("templates/ffi_options_free_conditional.jinja"),
    ),
    (
        "ffi_result_to_json.jinja",
        include_str!("templates/ffi_result_to_json.jinja"),
    ),
    ("ffi_result_free.jinja", include_str!("templates/ffi_result_free.jinja")),
    (
        "ffi_visitor_free.jinja",
        include_str!("templates/ffi_visitor_free.jinja"),
    ),
    (
        "ffi_catch_exception.jinja",
        include_str!("templates/ffi_catch_exception.jinja"),
    ),
    (
        "ffi_options_free.jinja",
        include_str!("templates/ffi_options_free.jinja"),
    ),
    ("ffi_throw_inner.jinja", include_str!("templates/ffi_throw_inner.jinja")),
    ("ffi_throw_outer.jinja", include_str!("templates/ffi_throw_outer.jinja")),
    (
        "ffi_result_ptr_call.jinja",
        include_str!("templates/ffi_result_ptr_call.jinja"),
    ),
    ("ffi_invoke_free.jinja", include_str!("templates/ffi_invoke_free.jinja")),
    ("ffi_invoke_void.jinja", include_str!("templates/ffi_invoke_void.jinja")),
    ("ffi_return_expr.jinja", include_str!("templates/ffi_return_expr.jinja")),
    (
        "ffi_return_optional_expr.jinja",
        include_str!("templates/ffi_return_optional_expr.jinja"),
    ),
    (
        "ffi_return_mapper_read.jinja",
        include_str!("templates/ffi_return_mapper_read.jinja"),
    ),
    (
        "ffi_return_mapper_read_optional.jinja",
        include_str!("templates/ffi_return_mapper_read_optional.jinja"),
    ),
    (
        "ffi_return_read_json_list_plain.jinja",
        include_str!("templates/ffi_return_read_json_list_plain.jinja"),
    ),
    (
        "ffi_return_new_handle.jinja",
        include_str!("templates/ffi_return_new_handle.jinja"),
    ),
    (
        "ffi_primitive_result_call.jinja",
        include_str!("templates/ffi_primitive_result_call.jinja"),
    ),
    (
        "ffi_return_primitive_result.jinja",
        include_str!("templates/ffi_return_primitive_result.jinja"),
    ),
    (
        "stream_method_null_check.jinja",
        include_str!("templates/stream_method_null_check.jinja"),
    ),
    (
        "stream_method_string_param.jinja",
        include_str!("templates/stream_method_string_param.jinja"),
    ),
    (
        "stream_method_optional_string_param.jinja",
        include_str!("templates/stream_method_optional_string_param.jinja"),
    ),
    (
        "stream_method_json_param.jinja",
        include_str!("templates/stream_method_json_param.jinja"),
    ),
    (
        "stream_method_optional_json_param.jinja",
        include_str!("templates/stream_method_optional_json_param.jinja"),
    ),
    (
        "stream_method_optional_named_param.jinja",
        include_str!("templates/stream_method_optional_named_param.jinja"),
    ),
    (
        "stream_method_enum_param.jinja",
        include_str!("templates/stream_method_enum_param.jinja"),
    ),
    (
        "stream_method_named_param.jinja",
        include_str!("templates/stream_method_named_param.jinja"),
    ),
    (
        "stream_method_unsupported_param.jinja",
        include_str!("templates/stream_method_unsupported_param.jinja"),
    ),
    (
        "stream_method_free_named_ptr.jinja",
        include_str!("templates/stream_method_free_named_ptr.jinja"),
    ),
    (
        "stream_method_bytes_result.jinja",
        include_str!("templates/stream_method_bytes_result.jinja"),
    ),
    (
        "stream_method_named_result.jinja",
        include_str!("templates/stream_method_named_result.jinja"),
    ),
    (
        "stream_method_opaque_handle_result.jinja",
        include_str!("templates/stream_method_opaque_handle_result.jinja"),
    ),
    (
        "stream_method_string_result.jinja",
        include_str!("templates/stream_method_string_result.jinja"),
    ),
    (
        "stream_method_optional_string_result.jinja",
        include_str!("templates/stream_method_optional_string_result.jinja"),
    ),
    (
        "stream_method_primitive_result.jinja",
        include_str!("templates/stream_method_primitive_result.jinja"),
    ),
    (
        "stream_method_optional_primitive_result.jinja",
        include_str!("templates/stream_method_optional_primitive_result.jinja"),
    ),
    (
        "stream_method_unit_result.jinja",
        include_str!("templates/stream_method_unit_result.jinja"),
    ),
    (
        "stream_method_unsupported_return.jinja",
        include_str!("templates/stream_method_unsupported_return.jinja"),
    ),
    (
        "streaming_iterator_method.jinja",
        include_str!("templates/streaming_iterator_method.jinja"),
    ),
    (
        "streaming_helpers.jinja",
        include_str!("templates/streaming_helpers.jinja"),
    ),
    (
        "stream_method_catch.jinja",
        include_str!("templates/stream_method_catch.jinja"),
    ),
    (
        "stream_method_catch_unchecked.jinja",
        include_str!("templates/stream_method_catch_unchecked.jinja"),
    ),
    (
        "registration_variant.java.jinja",
        include_str!("templates/registration_variant.java.jinja"),
    ),
    (
        "service_binding_doc_entrypoint.jinja",
        include_str!("templates/service_binding_doc_entrypoint.jinja"),
    ),
    (
        "service_binding_doc_registration.jinja",
        include_str!("templates/service_binding_doc_registration.jinja"),
    ),
    (
        "service_class_header.jinja",
        include_str!("templates/service_class_header.jinja"),
    ),
    (
        "service_constructor.jinja",
        include_str!("templates/service_constructor.jinja"),
    ),
    (
        "service_registration_method.jinja",
        include_str!("templates/service_registration_method.jinja"),
    ),
    (
        "service_entrypoint_method.jinja",
        include_str!("templates/service_entrypoint_method.jinja"),
    ),
    ("service_close.jinja", include_str!("templates/service_close.jinja")),
    (
        "service_config_method.jinja",
        include_str!("templates/service_config_method.jinja"),
    ),
    (
        "service_callable_interface.jinja",
        include_str!("templates/service_callable_interface.jinja"),
    ),
    (
        "service_metadata_param_doc.jinja",
        include_str!("templates/service_metadata_param_doc.jinja"),
    ),
    (
        "service_metadata_signature_param.jinja",
        include_str!("templates/service_metadata_signature_param.jinja"),
    ),
    (
        "service_bool_arg_expr.jinja",
        include_str!("templates/service_bool_arg_expr.jinja"),
    ),
    (
        "record_declaration.jinja",
        include_str!("templates/record_declaration.jinja"),
    ),
    (
        "record_builder_factory.jinja",
        include_str!("templates/record_builder_factory.jinja"),
    ),
    (
        "record_compact_constructor.jinja",
        include_str!("templates/record_compact_constructor.jinja"),
    ),
    (
        "record_unsupported_method.jinja",
        include_str!("templates/record_unsupported_method.jinja"),
    ),
    (
        "simple_enum_class.jinja",
        include_str!("templates/simple_enum_class.jinja"),
    ),
    (
        "opaque_handle_header.jinja",
        include_str!("templates/opaque_handle_header.jinja"),
    ),
    (
        "opaque_handle_close.jinja",
        include_str!("templates/opaque_handle_close.jinja"),
    ),
    (
        "static_factory_return_handle.jinja",
        include_str!("templates/static_factory_return_handle.jinja"),
    ),
    (
        "byte_array_serializer.jinja",
        include_str!("templates/byte_array_serializer.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}"))
}
