use minijinja::Environment;

static TEMPLATES: &[(&str, &str)] = &[
    // trait_bridge templates
    (
        "trait_bridge/import_item.jinja",
        include_str!("templates/trait_bridge/import_item.jinja"),
    ),
    (
        "trait_bridge/import_block_single_line.jinja",
        include_str!("templates/trait_bridge/import_block_single_line.jinja"),
    ),
    (
        "trait_bridge/import_block_multi_line_header.jinja",
        include_str!("templates/trait_bridge/import_block_multi_line_header.jinja"),
    ),
    (
        "trait_bridge/dataclass_field_no_default.jinja",
        include_str!("templates/trait_bridge/dataclass_field_no_default.jinja"),
    ),
    (
        "trait_bridge/dataclass_field_with_default.jinja",
        include_str!("templates/trait_bridge/dataclass_field_with_default.jinja"),
    ),
    (
        "trait_bridge/indented_import_item.jinja",
        include_str!("templates/trait_bridge/indented_import_item.jinja"),
    ),
    (
        "trait_bridge/single_line.jinja",
        include_str!("templates/trait_bridge/single_line.jinja"),
    ),
    (
        "trait_bridge/python_docstring.jinja",
        include_str!("templates/trait_bridge/python_docstring.jinja"),
    ),
    (
        "trait_bridge/sync_method_unit_return.jinja",
        include_str!("templates/trait_bridge/sync_method_unit_return.jinja"),
    ),
    (
        "trait_bridge/sync_method_non_unit_return.jinja",
        include_str!("templates/trait_bridge/sync_method_non_unit_return.jinja"),
    ),
    (
        "trait_bridge/async_method_named_return.jinja",
        include_str!("templates/trait_bridge/async_method_named_return.jinja"),
    ),
    (
        "trait_bridge/async_method_unit_return.jinja",
        include_str!("templates/trait_bridge/async_method_unit_return.jinja"),
    ),
    (
        "trait_bridge/async_method_non_unit_return.jinja",
        include_str!("templates/trait_bridge/async_method_non_unit_return.jinja"),
    ),
    (
        "trait_bridge/async_param_cloning.jinja",
        include_str!("templates/trait_bridge/async_param_cloning.jinja"),
    ),
    (
        "trait_bridge/constructor.jinja",
        include_str!("templates/trait_bridge/constructor.jinja"),
    ),
    (
        "trait_bridge/unregistration_fn.jinja",
        include_str!("templates/trait_bridge/unregistration_fn.jinja"),
    ),
    (
        "trait_bridge/clear_fn.jinja",
        include_str!("templates/trait_bridge/clear_fn.jinja"),
    ),
    (
        "trait_bridge/registration_fn.jinja",
        include_str!("templates/trait_bridge/registration_fn.jinja"),
    ),
    (
        "trait_bridge/nodecontext_to_py_dict.jinja",
        include_str!("templates/trait_bridge/nodecontext_to_py_dict.jinja"),
    ),
    (
        "trait_bridge/visitor_struct.jinja",
        include_str!("templates/trait_bridge/visitor_struct.jinja"),
    ),
    (
        "trait_bridge/visitor_impl.jinja",
        include_str!("templates/trait_bridge/visitor_impl.jinja"),
    ),
    (
        "trait_bridge/visitor_method.jinja",
        include_str!("templates/trait_bridge/visitor_method.jinja"),
    ),
    (
        "trait_bridge/bridge_function.jinja",
        include_str!("templates/trait_bridge/bridge_function.jinja"),
    ),
    (
        "trait_bridge/function_wrapper.jinja",
        include_str!("templates/trait_bridge/function_wrapper.jinja"),
    ),
    (
        "trait_bridge/impl_header.jinja",
        include_str!("templates/trait_bridge/impl_header.jinja"),
    ),
    (
        "exception_base_class.jinja",
        include_str!("templates/exception_base_class.jinja"),
    ),
    (
        "exception_variant_class.jinja",
        include_str!("templates/exception_variant_class.jinja"),
    ),
    (
        "exception_property_stub.jinja",
        include_str!("templates/exception_property_stub.jinja"),
    ),
    (
        "import_single_line.jinja",
        include_str!("templates/import_single_line.jinja"),
    ),
    (
        "import_multi_line.jinja",
        include_str!("templates/import_multi_line.jinja"),
    ),
    ("init_all_entry.jinja", include_str!("templates/init_all_entry.jinja")),
    (
        "version_declaration.jinja",
        include_str!("templates/version_declaration.jinja"),
    ),
    ("init_header.jinja", include_str!("templates/init_header.jinja")),
    ("stub_param.jinja", include_str!("templates/stub_param.jinja")),
    ("stub_return_sig.jinja", include_str!("templates/stub_return_sig.jinja")),
    (
        "import_as_module.jinja",
        include_str!("templates/import_as_module.jinja"),
    ),
    (
        "import_from_module.jinja",
        include_str!("templates/import_from_module.jinja"),
    ),
    (
        "import_from_absolute_module.jinja",
        include_str!("templates/import_from_absolute_module.jinja"),
    ),
    (
        "import_from_options.jinja",
        include_str!("templates/import_from_options.jinja"),
    ),
    (
        "converters/signature_with_visitor.jinja",
        include_str!("templates/converters/signature_with_visitor.jinja"),
    ),
    (
        "converters/signature_multiline.jinja",
        include_str!("templates/converters/signature_multiline.jinja"),
    ),
    (
        "converters/signature_singleline.jinja",
        include_str!("templates/converters/signature_singleline.jinja"),
    ),
    (
        "converters/docstring.jinja",
        include_str!("templates/converters/docstring.jinja"),
    ),
    (
        "converters/dict_coercer_header.jinja",
        include_str!("templates/converters/dict_coercer_header.jinja"),
    ),
    (
        "converters/dict_coercer_docstring.jinja",
        include_str!("templates/converters/dict_coercer_docstring.jinja"),
    ),
    (
        "converters/struct_coercion_entry.jinja",
        include_str!("templates/converters/struct_coercion_entry.jinja"),
    ),
    (
        "converters/enum_coercion_entry.jinja",
        include_str!("templates/converters/enum_coercion_entry.jinja"),
    ),
    (
        "converters/return_coerced_type.jinja",
        include_str!("templates/converters/return_coerced_type.jinja"),
    ),
    (
        "converters/call_dict_helper.jinja",
        include_str!("templates/converters/call_dict_helper.jinja"),
    ),
    (
        "converters/inline_enum_coerce.jinja",
        include_str!("templates/converters/inline_enum_coerce.jinja"),
    ),
    (
        "converters/inline_struct_coerce.jinja",
        include_str!("templates/converters/inline_struct_coerce.jinja"),
    ),
    (
        "converters/inline_data_enum_coerce.jinja",
        include_str!("templates/converters/inline_data_enum_coerce.jinja"),
    ),
    (
        "converters/construct_type.jinja",
        include_str!("templates/converters/construct_type.jinja"),
    ),
    (
        "converters/return_constructed.jinja",
        include_str!("templates/converters/return_constructed.jinja"),
    ),
    (
        "converters/field_accessor.jinja",
        include_str!("templates/converters/field_accessor.jinja"),
    ),
    (
        "converters/overload_none.jinja",
        include_str!("templates/converters/overload_none.jinja"),
    ),
    (
        "converters/overload_some.jinja",
        include_str!("templates/converters/overload_some.jinja"),
    ),
    (
        "converters/cast_value.jinja",
        include_str!("templates/converters/cast_value.jinja"),
    ),
    (
        "converters/serde_alias.jinja",
        include_str!("templates/converters/serde_alias.jinja"),
    ),
    (
        "data_enum_vec_coerce.jinja",
        include_str!("templates/data_enum_vec_coerce.jinja"),
    ),
    (
        "simple_enum_vec_coerce.jinja",
        include_str!("templates/simple_enum_vec_coerce.jinja"),
    ),
    ("typing_import.jinja", include_str!("templates/typing_import.jinja")),
    (
        "import_from_module_header.jinja",
        include_str!("templates/import_from_module_header.jinja"),
    ),
    (
        "import_from_relative_module_header.jinja",
        include_str!("templates/import_from_relative_module_header.jinja"),
    ),
    ("import_item.jinja", include_str!("templates/import_item.jinja")),
    (
        "type_checking_import_header.jinja",
        include_str!("templates/type_checking_import_header.jinja"),
    ),
    (
        "type_checking_import_item.jinja",
        include_str!("templates/type_checking_import_item.jinja"),
    ),
    ("enum_setattr.jinja", include_str!("templates/enum_setattr.jinja")),
    (
        "enum_direct_assign.jinja",
        include_str!("templates/enum_direct_assign.jinja"),
    ),
    (
        "str_enum_class_header.jinja",
        include_str!("templates/str_enum_class_header.jinja"),
    ),
    ("enum_docstring.jinja", include_str!("templates/enum_docstring.jinja")),
    ("enum_variant.jinja", include_str!("templates/enum_variant.jinja")),
    (
        "dataclass_header.jinja",
        include_str!("templates/dataclass_header.jinja"),
    ),
    ("class_docstring.jinja", include_str!("templates/class_docstring.jinja")),
    (
        "data_enum_comment.jinja",
        include_str!("templates/data_enum_comment.jinja"),
    ),
    (
        "data_enum_single_line.jinja",
        include_str!("templates/data_enum_single_line.jinja"),
    ),
    (
        "data_enum_multi_line_start.jinja",
        include_str!("templates/data_enum_multi_line_start.jinja"),
    ),
    (
        "data_enum_member.jinja",
        include_str!("templates/data_enum_member.jinja"),
    ),
    (
        "pyo3_capsule_signature.jinja",
        include_str!("templates/pyo3_capsule_signature.jinja"),
    ),
    (
        "pyo3_capsule_function_header.jinja",
        include_str!("templates/pyo3_capsule_function_header.jinja"),
    ),
    (
        "pyo3_capsule_input_const.jinja",
        include_str!("templates/pyo3_capsule_input_const.jinja"),
    ),
    (
        "pyo3_capsule_input_optional.jinja",
        include_str!("templates/pyo3_capsule_input_optional.jinja"),
    ),
    (
        "pyo3_capsule_input_required.jinja",
        include_str!("templates/pyo3_capsule_input_required.jinja"),
    ),
    (
        "pyo3_capsule_call_result_err.jinja",
        include_str!("templates/pyo3_capsule_call_result_err.jinja"),
    ),
    (
        "pyo3_capsule_call_result.jinja",
        include_str!("templates/pyo3_capsule_call_result.jinja"),
    ),
    (
        "pyo3_capsule_into_raw.jinja",
        include_str!("templates/pyo3_capsule_into_raw.jinja"),
    ),
    (
        "pyo3_capsule_ptr_from_raw.jinja",
        include_str!("templates/pyo3_capsule_ptr_from_raw.jinja"),
    ),
    (
        "pyo3_capsule_construct_comment.jinja",
        include_str!("templates/pyo3_capsule_construct_comment.jinja"),
    ),
    (
        "pyo3_capsule_construct_with_module.jinja",
        include_str!("templates/pyo3_capsule_construct_with_module.jinja"),
    ),
    (
        "pyo3_capsule_construct_with_builtin.jinja",
        include_str!("templates/pyo3_capsule_construct_with_builtin.jinja"),
    ),
    (
        "pyo3_capsule_missing_dependency.jinja",
        include_str!("templates/pyo3_capsule_missing_dependency.jinja"),
    ),
    (
        "pyo3_capsule_call_result_err_inline.jinja",
        include_str!("templates/pyo3_capsule_call_result_err_inline.jinja"),
    ),
    (
        "pyo3_capsule_call_no_capsule_return.jinja",
        include_str!("templates/pyo3_capsule_call_no_capsule_return.jinja"),
    ),
    (
        "pyo3_capsule_param_extract.jinja",
        include_str!("templates/pyo3_capsule_param_extract.jinja"),
    ),
    (
        "typeddict_header.jinja",
        include_str!("templates/typeddict_header.jinja"),
    ),
    ("typeddict_field.jinja", include_str!("templates/typeddict_field.jinja")),
    (
        "typeddict_field_docstring.jinja",
        include_str!("templates/typeddict_field_docstring.jinja"),
    ),
    (
        "stub_param_wrapped_noqa.jinja",
        include_str!("templates/stub_param_wrapped_noqa.jinja"),
    ),
    (
        "stub_param_wrapped.jinja",
        include_str!("templates/stub_param_wrapped.jinja"),
    ),
    (
        "stub_param_method_wrapped_noqa.jinja",
        include_str!("templates/stub_param_method_wrapped_noqa.jinja"),
    ),
    (
        "stub_param_method_wrapped.jinja",
        include_str!("templates/stub_param_method_wrapped.jinja"),
    ),
    (
        "stub_method_signature_end.jinja",
        include_str!("templates/stub_method_signature_end.jinja"),
    ),
    (
        "stub_wrapped_param_line.jinja",
        include_str!("templates/stub_wrapped_param_line.jinja"),
    ),
    (
        "param_conversion_optional.jinja",
        include_str!("templates/param_conversion_optional.jinja"),
    ),
    (
        "param_conversion.jinja",
        include_str!("templates/param_conversion.jinja"),
    ),
    (
        "function_signature_single_line.jinja",
        include_str!("templates/function_signature_single_line.jinja"),
    ),
    (
        "function_signature_multiline_start.jinja",
        include_str!("templates/function_signature_multiline_start.jinja"),
    ),
    (
        "function_signature_multiline_param.jinja",
        include_str!("templates/function_signature_multiline_param.jinja"),
    ),
    (
        "function_signature_multiline_param_noqa.jinja",
        include_str!("templates/function_signature_multiline_param_noqa.jinja"),
    ),
    (
        "function_signature_multiline_end.jinja",
        include_str!("templates/function_signature_multiline_end.jinja"),
    ),
    ("function_call.jinja", include_str!("templates/function_call.jinja")),
    (
        "function_call_statement.jinja",
        include_str!("templates/function_call_statement.jinja"),
    ),
    (
        "function_cast_return.jinja",
        include_str!("templates/function_cast_return.jinja"),
    ),
    (
        "function_docstring.jinja",
        include_str!("templates/function_docstring.jinja"),
    ),
    (
        "adapter_streaming_wrapper.jinja",
        include_str!("templates/adapter_streaming_wrapper.jinja"),
    ),
    (
        "adapter_async_wrapper.jinja",
        include_str!("templates/adapter_async_wrapper.jinja"),
    ),
    ("field_kwarg.jinja", include_str!("templates/field_kwarg.jinja")),
    (
        "field_kwarg_optional_default.jinja",
        include_str!("templates/field_kwarg_optional_default.jinja"),
    ),
    (
        "simple_enum_dict_coerce.jinja",
        include_str!("templates/simple_enum_dict_coerce.jinja"),
    ),
    (
        "simple_enum_dict_coerce_optional_default.jinja",
        include_str!("templates/simple_enum_dict_coerce_optional_default.jinja"),
    ),
    (
        "visitor_override_param.jinja",
        include_str!("templates/visitor_override_param.jinja"),
    ),
    (
        "visitor_override_none_case.jinja",
        include_str!("templates/visitor_override_none_case.jinja"),
    ),
    ("bridge_clear_fn.jinja", include_str!("templates/bridge_clear_fn.jinja")),
    (
        "bridge_register_fn.jinja",
        include_str!("templates/bridge_register_fn.jinja"),
    ),
    (
        "bridge_unregister_fn.jinja",
        include_str!("templates/bridge_unregister_fn.jinja"),
    ),
    (
        "config_conversion_ternary.jinja",
        include_str!("templates/config_conversion_ternary.jinja"),
    ),
    (
        "config_default_on_none.jinja",
        include_str!("templates/config_default_on_none.jinja"),
    ),
    (
        "data_enum_dict_coerce_guard.jinja",
        include_str!("templates/data_enum_dict_coerce_guard.jinja"),
    ),
    (
        "data_enum_dict_coerce_no_guard.jinja",
        include_str!("templates/data_enum_dict_coerce_no_guard.jinja"),
    ),
    (
        "data_enum_dict_coerce_optional_default.jinja",
        include_str!("templates/data_enum_dict_coerce_optional_default.jinja"),
    ),
    (
        "service_api_py_header.py.jinja",
        include_str!("templates/service_api_py_header.py.jinja"),
    ),
    (
        "service_api_py_runtime_import.py.jinja",
        include_str!("templates/service_api_py_runtime_import.py.jinja"),
    ),
    (
        "service_api_py_type_checking_import.py.jinja",
        include_str!("templates/service_api_py_type_checking_import.py.jinja"),
    ),
    (
        "service_api_py_class_header.py.jinja",
        include_str!("templates/service_api_py_class_header.py.jinja"),
    ),
    (
        "service_api_py_init_header.py.jinja",
        include_str!("templates/service_api_py_init_header.py.jinja"),
    ),
    (
        "service_api_py_registration_state.py.jinja",
        include_str!("templates/service_api_py_registration_state.py.jinja"),
    ),
    (
        "service_api_py_init_assignment.py.jinja",
        include_str!("templates/service_api_py_init_assignment.py.jinja"),
    ),
    (
        "service_api_py_configurator_header.py.jinja",
        include_str!("templates/service_api_py_configurator_header.py.jinja"),
    ),
    (
        "service_api_py_configurator_assignment.py.jinja",
        include_str!("templates/service_api_py_configurator_assignment.py.jinja"),
    ),
    (
        "service_api_py_return_self.py.jinja",
        include_str!("templates/service_api_py_return_self.py.jinja"),
    ),
    (
        "service_api_py_entrypoint_header.py.jinja",
        include_str!("templates/service_api_py_entrypoint_header.py.jinja"),
    ),
    (
        "service_api_py_entrypoint_call.py.jinja",
        include_str!("templates/service_api_py_entrypoint_call.py.jinja"),
    ),
    (
        "service_api_py_direct_variant_header.py.jinja",
        include_str!("templates/service_api_py_direct_variant_header.py.jinja"),
    ),
    (
        "service_api_py_direct_variant_doc.py.jinja",
        include_str!("templates/service_api_py_direct_variant_doc.py.jinja"),
    ),
    (
        "service_api_py_statement.py.jinja",
        include_str!("templates/service_api_py_statement.py.jinja"),
    ),
    (
        "service_api_py_append_registration.py.jinja",
        include_str!("templates/service_api_py_append_registration.py.jinja"),
    ),
    (
        "service_api_py_decorator_factory_header.py.jinja",
        include_str!("templates/service_api_py_decorator_factory_header.py.jinja"),
    ),
    (
        "service_api_py_decorator_variant_doc.py.jinja",
        include_str!("templates/service_api_py_decorator_variant_doc.py.jinja"),
    ),
    (
        "service_api_py_decorator_body.py.jinja",
        include_str!("templates/service_api_py_decorator_body.py.jinja"),
    ),
    (
        "service_api_py_registration_method_header.py.jinja",
        include_str!("templates/service_api_py_registration_method_header.py.jinja"),
    ),
    (
        "service_api_py_direct_registration.py.jinja",
        include_str!("templates/service_api_py_direct_registration.py.jinja"),
    ),
    (
        "service_api_rs_header.rs.jinja",
        include_str!("templates/service_api_rs_header.rs.jinja"),
    ),
    (
        "service_api_handler_bridge_struct.rs.jinja",
        include_str!("templates/service_api_handler_bridge_struct.rs.jinja"),
    ),
    (
        "service_api_handler_bridge_impl.rs.jinja",
        include_str!("templates/service_api_handler_bridge_impl.rs.jinja"),
    ),
    (
        "service_api_pyfunction_header.rs.jinja",
        include_str!("templates/service_api_pyfunction_header.rs.jinja"),
    ),
    (
        "service_api_rs_owner_ctor.rs.jinja",
        include_str!("templates/service_api_rs_owner_ctor.rs.jinja"),
    ),
    (
        "service_api_registration_arm.rs.jinja",
        include_str!("templates/service_api_registration_arm.rs.jinja"),
    ),
    (
        "service_api_registration_meta_opaque.rs.jinja",
        include_str!("templates/service_api_registration_meta_opaque.rs.jinja"),
    ),
    (
        "service_api_registration_meta_value.rs.jinja",
        include_str!("templates/service_api_registration_meta_value.rs.jinja"),
    ),
    (
        "service_api_registration_owner_call.rs.jinja",
        include_str!("templates/service_api_registration_owner_call.rs.jinja"),
    ),
    (
        "service_api_unknown_registration_arm.rs.jinja",
        include_str!("templates/service_api_unknown_registration_arm.rs.jinja"),
    ),
    (
        "service_api_pyfunction_footer.rs.jinja",
        include_str!("templates/service_api_pyfunction_footer.rs.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}"))
}
