use minijinja::Environment;

static TEMPLATES: &[(&str, &str)] = &[
    (
        "async_task_return_type.jinja",
        include_str!("templates/async_task_return_type.jinja"),
    ),
    ("class_header.jinja", include_str!("templates/class_header.jinja")),
    ("doc_line.jinja", include_str!("templates/doc_line.jinja")),
    (
        "doc_comment_block.jinja",
        include_str!("templates/doc_comment_block.jinja"),
    ),
    (
        "enum_custom_converter.jinja",
        include_str!("templates/enum_custom_converter.jinja"),
    ),
    ("enum_header.jinja", include_str!("templates/enum_header.jinja")),
    (
        "untagged_union_wrapper.jinja",
        include_str!("templates/untagged_union_wrapper.jinja"),
    ),
    (
        "sealed_union_converter.jinja",
        include_str!("templates/sealed_union_converter.jinja"),
    ),
    (
        "named_param_handle_from_json_optional.jinja",
        include_str!("templates/named_param_handle_from_json_optional.jinja"),
    ),
    ("error_dispatch.jinja", include_str!("templates/error_dispatch.jinja")),
    (
        "error_helper_method.jinja",
        include_str!("templates/error_helper_method.jinja"),
    ),
    ("exception_class.jinja", include_str!("templates/exception_class.jinja")),
    (
        "exception_return.jinja",
        include_str!("templates/exception_return.jinja"),
    ),
    (
        "ffi_json_extensions.jinja",
        include_str!("templates/ffi_json_extensions.jinja"),
    ),
    (
        "field_definition.jinja",
        include_str!("templates/field_definition.jinja"),
    ),
    (
        "indented_arg_async.jinja",
        include_str!("templates/indented_arg_async.jinja"),
    ),
    (
        "indented_arg_sync.jinja",
        include_str!("templates/indented_arg_sync.jinja"),
    ),
    ("namespace_decl.jinja", include_str!("templates/namespace_decl.jinja")),
    (
        "native_call_start.jinja",
        include_str!("templates/native_call_start.jinja"),
    ),
    ("native_arg_line.jinja", include_str!("templates/native_arg_line.jinja")),
    (
        "native_bytes_len_arg_line.jinja",
        include_str!("templates/native_bytes_len_arg_line.jinja"),
    ),
    (
        "native_methods_header.jinja",
        include_str!("templates/native_methods_header.jinja"),
    ),
    (
        "native_methods_trait_bridges.jinja",
        include_str!("templates/native_methods_trait_bridges.jinja"),
    ),
    (
        "native_methods_visitor.jinja",
        include_str!("templates/native_methods_visitor.jinja"),
    ),
    ("node_context.jinja", include_str!("templates/node_context.jinja")),
    ("visit_result.jinja", include_str!("templates/visit_result.jinja")),
    ("null_check.jinja", include_str!("templates/null_check.jinja")),
    (
        "opaque_class_ctor.jinja",
        include_str!("templates/opaque_class_ctor.jinja"),
    ),
    (
        "opaque_ctor_call.jinja",
        include_str!("templates/opaque_ctor_call.jinja"),
    ),
    (
        "opaque_handle_header.jinja",
        include_str!("templates/opaque_handle_header.jinja"),
    ),
    (
        "opaque_streaming_method.jinja",
        include_str!("templates/opaque_streaming_method.jinja"),
    ),
    (
        "param_decl_optional.jinja",
        include_str!("templates/param_decl_optional.jinja"),
    ),
    (
        "param_decl_required.jinja",
        include_str!("templates/param_decl_required.jinja"),
    ),
    ("param_doc.jinja", include_str!("templates/param_doc.jinja")),
    (
        "public_method_signature.jinja",
        include_str!("templates/public_method_signature.jinja"),
    ),
    (
        "release_handle_method.jinja",
        include_str!("templates/release_handle_method.jinja"),
    ),
    (
        "safe_handle_class.jinja",
        include_str!("templates/safe_handle_class.jinja"),
    ),
    (
        "safe_handle_ctor.jinja",
        include_str!("templates/safe_handle_ctor.jinja"),
    ),
    (
        "safehandle_field.jinja",
        include_str!("templates/safehandle_field.jinja"),
    ),
    (
        "sealed_class_header.jinja",
        include_str!("templates/sealed_class_header.jinja"),
    ),
    (
        "trait_bridge_class.jinja",
        include_str!("templates/trait_bridge_class.jinja"),
    ),
    (
        "trait_bridges_header.jinja",
        include_str!("templates/trait_bridges_header.jinja"),
    ),
    ("trait_interface.jinja", include_str!("templates/trait_interface.jinja")),
    (
        "trait_registry_class.jinja",
        include_str!("templates/trait_registry_class.jinja"),
    ),
    (
        "wrapper_class_header.jinja",
        include_str!("templates/wrapper_class_header.jinja"),
    ),
    (
        "named_param_json_optional.jinja",
        include_str!("templates/named_param_json_optional.jinja"),
    ),
    (
        "named_param_json_serialize.jinja",
        include_str!("templates/named_param_json_serialize.jinja"),
    ),
    (
        "named_param_handle_from_json.jinja",
        include_str!("templates/named_param_handle_from_json.jinja"),
    ),
    (
        "named_param_handle_string.jinja",
        include_str!("templates/named_param_handle_string.jinja"),
    ),
    (
        "named_param_handle_pin.jinja",
        include_str!("templates/named_param_handle_pin.jinja"),
    ),
    (
        "named_param_teardown_free.jinja",
        include_str!("templates/named_param_teardown_free.jinja"),
    ),
    (
        "named_param_teardown_hglobal.jinja",
        include_str!("templates/named_param_teardown_hglobal.jinja"),
    ),
    (
        "named_param_teardown_gchandle.jinja",
        include_str!("templates/named_param_teardown_gchandle.jinja"),
    ),
    (
        "return_string_utf8.jinja",
        include_str!("templates/return_string_utf8.jinja"),
    ),
    (
        "free_native_string.jinja",
        include_str!("templates/free_native_string.jinja"),
    ),
    (
        "return_bool_from_int.jinja",
        include_str!("templates/return_bool_from_int.jinja"),
    ),
    (
        "return_opaque_ctor.jinja",
        include_str!("templates/return_opaque_ctor.jinja"),
    ),
    (
        "native_to_json_ptr.jinja",
        include_str!("templates/native_to_json_ptr.jinja"),
    ),
    ("json_from_ptr.jinja", include_str!("templates/json_from_ptr.jinja")),
    ("free_string_ptr.jinja", include_str!("templates/free_string_ptr.jinja")),
    (
        "free_native_handle.jinja",
        include_str!("templates/free_native_handle.jinja"),
    ),
    (
        "deserialize_json.jinja",
        include_str!("templates/deserialize_json.jinja"),
    ),
    (
        "return_ptr_as_string.jinja",
        include_str!("templates/return_ptr_as_string.jinja"),
    ),
    (
        "return_native_result.jinja",
        include_str!("templates/return_native_result.jinja"),
    ),
    ("return_value.jinja", include_str!("templates/return_value.jinja")),
    (
        "json_polymorphic_attr.jinja",
        include_str!("templates/json_polymorphic_attr.jinja"),
    ),
    (
        "json_derived_type_attr.jinja",
        include_str!("templates/json_derived_type_attr.jinja"),
    ),
    (
        "abstract_record_header.jinja",
        include_str!("templates/abstract_record_header.jinja"),
    ),
    (
        "doc_line_indented.jinja",
        include_str!("templates/doc_line_indented.jinja"),
    ),
    (
        "unit_variant_record.jinja",
        include_str!("templates/unit_variant_record.jinja"),
    ),
    (
        "variant_record_body_header.jinja",
        include_str!("templates/variant_record_body_header.jinja"),
    ),
    (
        "required_value_property.jinja",
        include_str!("templates/required_value_property.jinja"),
    ),
    (
        "variant_record_params_header.jinja",
        include_str!("templates/variant_record_params_header.jinja"),
    ),
    (
        "variant_field_tuple.jinja",
        include_str!("templates/variant_field_tuple.jinja"),
    ),
    (
        "variant_field_json_value.jinja",
        include_str!("templates/variant_field_json_value.jinja"),
    ),
    (
        "variant_field_json_named.jinja",
        include_str!("templates/variant_field_json_named.jinja"),
    ),
    (
        "variant_record_close.jinja",
        include_str!("templates/variant_record_close.jinja"),
    ),
    (
        "variant_accessor_summary.jinja",
        include_str!("templates/variant_accessor_summary.jinja"),
    ),
    (
        "variant_accessor_property.jinja",
        include_str!("templates/variant_accessor_property.jinja"),
    ),
    ("dll_import_attr.jinja", include_str!("templates/dll_import_attr.jinja")),
    ("extern_void_ptr.jinja", include_str!("templates/extern_void_ptr.jinja")),
    (
        "extern_ptr_from_json.jinja",
        include_str!("templates/extern_ptr_from_json.jinja"),
    ),
    (
        "extern_ptr_to_json.jinja",
        include_str!("templates/extern_ptr_to_json.jinja"),
    ),
    ("pinvoke_param.jinja", include_str!("templates/pinvoke_param.jinja")),
    (
        "pinvoke_bytes_len_param.jinja",
        include_str!("templates/pinvoke_bytes_len_param.jinja"),
    ),
    (
        "streaming_pinvoke_declaration.jinja",
        include_str!("templates/streaming_pinvoke_declaration.jinja"),
    ),
    (
        "opaque_method_header.jinja",
        include_str!("templates/opaque_method_header.jinja"),
    ),
    (
        "null_result_throw.jinja",
        include_str!("templates/null_result_throw.jinja"),
    ),
    (
        "record_class_header.jinja",
        include_str!("templates/record_class_header.jinja"),
    ),
    (
        "json_converter_attr.jinja",
        include_str!("templates/json_converter_attr.jinja"),
    ),
    (
        "json_property_name_attr.jinja",
        include_str!("templates/json_property_name_attr.jinja"),
    ),
    (
        "json_extension_data_property.jinja",
        include_str!("templates/json_extension_data_property.jinja"),
    ),
    (
        "visitor_bridge_property.jinja",
        include_str!("templates/visitor_bridge_property.jinja"),
    ),
    (
        "property_with_default.jinja",
        include_str!("templates/property_with_default.jinja"),
    ),
    (
        "property_required_init.jinja",
        include_str!("templates/property_required_init.jinja"),
    ),
    (
        "vtable_slot_comment.jinja",
        include_str!("templates/vtable_slot_comment.jinja"),
    ),
    (
        "vtable_slot_assign.jinja",
        include_str!("templates/vtable_slot_assign.jinja"),
    ),
    (
        "vtable_write_intptr.jinja",
        include_str!("templates/vtable_write_intptr.jinja"),
    ),
    (
        "vtable_method_fn_new.jinja",
        include_str!("templates/vtable_method_fn_new.jinja"),
    ),
    (
        "callback_header_options.jinja",
        include_str!("templates/callback_header_options.jinja"),
    ),
    (
        "callback_header_full.jinja",
        include_str!("templates/callback_header_full.jinja"),
    ),
    (
        "callback_string_param.jinja",
        include_str!("templates/callback_string_param.jinja"),
    ),
    (
        "callback_bytes_param.jinja",
        include_str!("templates/callback_bytes_param.jinja"),
    ),
    (
        "callback_json_from_ptr.jinja",
        include_str!("templates/callback_json_from_ptr.jinja"),
    ),
    (
        "callback_json_deserialize.jinja",
        include_str!("templates/callback_json_deserialize.jinja"),
    ),
    (
        "callback_void_call.jinja",
        include_str!("templates/callback_void_call.jinja"),
    ),
    (
        "callback_result_call.jinja",
        include_str!("templates/callback_result_call.jinja"),
    ),
    (
        "callback_result_serialize.jinja",
        include_str!("templates/callback_result_serialize.jinja"),
    ),
    (
        "bytes_result_call.jinja",
        include_str!("templates/bytes_result_call.jinja"),
    ),
    (
        "opaque_bytes_result_call.jinja",
        include_str!("templates/opaque_bytes_result_call.jinja"),
    ),
    (
        "opaque_streaming_static_wrapper.jinja",
        include_str!("templates/opaque_streaming_static_wrapper.jinja"),
    ),
    (
        "opaque_static_constructor_summary.jinja",
        include_str!("templates/opaque_static_constructor_summary.jinja"),
    ),
    (
        "opaque_static_constructor_signature.jinja",
        include_str!("templates/opaque_static_constructor_signature.jinja"),
    ),
    (
        "opaque_static_constructor_handle.jinja",
        include_str!("templates/opaque_static_constructor_handle.jinja"),
    ),
    (
        "opaque_static_constructor_error_check.jinja",
        include_str!("templates/opaque_static_constructor_error_check.jinja"),
    ),
    (
        "opaque_safehandle_init.jinja",
        include_str!("templates/opaque_safehandle_init.jinja"),
    ),
    (
        "opaque_factory_method.jinja",
        include_str!("templates/opaque_factory_method.jinja"),
    ),
    (
        "record_from_json_method.jinja",
        include_str!("templates/record_from_json_method.jinja"),
    ),
    (
        "record_json_options.jinja",
        include_str!("templates/record_json_options.jinja"),
    ),
    (
        "record_method_doc.jinja",
        include_str!("templates/record_method_doc.jinja"),
    ),
    (
        "record_method_signature.jinja",
        include_str!("templates/record_method_signature.jinja"),
    ),
    (
        "record_self_handle_checked.jinja",
        include_str!("templates/record_self_handle_checked.jinja"),
    ),
    (
        "record_self_handle.jinja",
        include_str!("templates/record_self_handle.jinja"),
    ),
    (
        "record_native_result_checked.jinja",
        include_str!("templates/record_native_result_checked.jinja"),
    ),
    (
        "record_native_result.jinja",
        include_str!("templates/record_native_result.jinja"),
    ),
    (
        "record_json_return.jinja",
        include_str!("templates/record_json_return.jinja"),
    ),
    (
        "record_self_handle_free.jinja",
        include_str!("templates/record_self_handle_free.jinja"),
    ),
    (
        "bridge_field_setup.jinja",
        include_str!("templates/bridge_field_setup.jinja"),
    ),
    (
        "bridge_field_register.jinja",
        include_str!("templates/bridge_field_register.jinja"),
    ),
    (
        "bridge_field_register_visitor.jinja",
        include_str!("templates/bridge_field_register_visitor.jinja"),
    ),
    (
        "bridge_field_unregister_visitor.jinja",
        include_str!("templates/bridge_field_unregister_visitor.jinja"),
    ),
    (
        "bridge_field_inject.jinja",
        include_str!("templates/bridge_field_inject.jinja"),
    ),
    (
        "bridge_field_json_return.jinja",
        include_str!("templates/bridge_field_json_return.jinja"),
    ),
    (
        "bridge_field_unregister.jinja",
        include_str!("templates/bridge_field_unregister.jinja"),
    ),
    (
        "bridge_field_free_options.jinja",
        include_str!("templates/bridge_field_free_options.jinja"),
    ),
    (
        "plugin_string_callback.jinja",
        include_str!("templates/plugin_string_callback.jinja"),
    ),
    (
        "plugin_void_callback.jinja",
        include_str!("templates/plugin_void_callback.jinja"),
    ),
    (
        "callback_header_void.jinja",
        include_str!("templates/callback_header_void.jinja"),
    ),
    (
        "callback_header_primitive.jinja",
        include_str!("templates/callback_header_primitive.jinja"),
    ),
    (
        "callback_registry_acquire.jinja",
        include_str!("templates/callback_registry_acquire.jinja"),
    ),
    (
        "callback_primitive_call.jinja",
        include_str!("templates/callback_primitive_call.jinja"),
    ),
    (
        "free_user_data_callback.jinja",
        include_str!("templates/free_user_data_callback.jinja"),
    ),
    (
        "null_result_return.jinja",
        include_str!("templates/null_result_return.jinja"),
    ),
    (
        "last_error_throw.jinja",
        include_str!("templates/last_error_throw.jinja"),
    ),
    (
        "last_error_context_throw.jinja",
        include_str!("templates/last_error_context_throw.jinja"),
    ),
    (
        "client_constructor_pinvoke.jinja",
        include_str!("templates/client_constructor_pinvoke.jinja"),
    ),
    (
        "named_param_enum_optional.jinja",
        include_str!("templates/named_param_enum_optional.jinja"),
    ),
    (
        "named_param_enum_required.jinja",
        include_str!("templates/named_param_enum_required.jinja"),
    ),
    (
        "bridge_field_param_doc.jinja",
        include_str!("templates/bridge_field_param_doc.jinja"),
    ),
    (
        "async_task_generic.jinja",
        include_str!("templates/async_task_generic.jinja"),
    ),
    (
        "param_decl_inline_optional.jinja",
        include_str!("templates/param_decl_inline_optional.jinja"),
    ),
    (
        "param_decl_inline_required.jinja",
        include_str!("templates/param_decl_inline_required.jinja"),
    ),
    (
        "service_class_header.jinja",
        include_str!("templates/service_class_header.jinja"),
    ),
    (
        "service_configurator_method.jinja",
        include_str!("templates/service_configurator_method.jinja"),
    ),
    (
        "service_constructor.jinja",
        include_str!("templates/service_constructor.jinja"),
    ),
    (
        "service_dispose_method.jinja",
        include_str!("templates/service_dispose_method.jinja"),
    ),
    (
        "service_entrypoint_method.jinja",
        include_str!("templates/service_entrypoint_method.jinja"),
    ),
    (
        "service_handler_trampoline.jinja",
        include_str!("templates/service_handler_trampoline.jinja"),
    ),
    (
        "service_native_ctor_free.jinja",
        include_str!("templates/service_native_ctor_free.jinja"),
    ),
    (
        "service_native_methods_header.jinja",
        include_str!("templates/service_native_methods_header.jinja"),
    ),
    (
        "service_pinvoke_declaration.jinja",
        include_str!("templates/service_pinvoke_declaration.jinja"),
    ),
    (
        "service_registration_method.jinja",
        include_str!("templates/service_registration_method.jinja"),
    ),
    (
        "service_variant_registration_method.jinja",
        include_str!("templates/service_variant_registration_method.jinja"),
    ),
    (
        "streaming_adapter_wrapper.jinja",
        include_str!("templates/streaming_adapter_wrapper.jinja"),
    ),
    (
        "trait_clear_facade.jinja",
        include_str!("templates/trait_clear_facade.jinja"),
    ),
    (
        "trait_register_facade.jinja",
        include_str!("templates/trait_register_facade.jinja"),
    ),
    (
        "trait_unregister_facade.jinja",
        include_str!("templates/trait_unregister_facade.jinja"),
    ),
    (
        "trait_bridge_adapters.csharp.jinja",
        include_str!("templates/trait_bridge_adapters.csharp.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}"))
}
