use minijinja::Environment;

static TEMPLATES: &[(&str, &str)] = &[
    (
        "visitor_nodecontext_helper.jinja",
        include_str!("templates/visitor_nodecontext_helper.jinja"),
    ),
    (
        "visitor_zval_to_visitresult.jinja",
        include_str!("templates/visitor_zval_to_visitresult.jinja"),
    ),
    (
        "visitor_bridge_struct.jinja",
        include_str!("templates/visitor_bridge_struct.jinja"),
    ),
    (
        "bridge_constructor.jinja",
        include_str!("templates/bridge_constructor.jinja"),
    ),
    (
        "bridge_unregister_fn.jinja",
        include_str!("templates/bridge_unregister_fn.jinja"),
    ),
    ("bridge_clear_fn.jinja", include_str!("templates/bridge_clear_fn.jinja")),
    (
        "bridge_registration_fn.jinja",
        include_str!("templates/bridge_registration_fn.jinja"),
    ),
    (
        "sync_method_body.jinja",
        include_str!("templates/sync_method_body.jinja"),
    ),
    (
        "async_method_body.jinja",
        include_str!("templates/async_method_body.jinja"),
    ),
    (
        "bridge_sync_impl.jinja",
        include_str!("templates/bridge_sync_impl.jinja"),
    ),
    (
        "bridge_registration_validation.jinja",
        include_str!("templates/bridge_registration_validation.jinja"),
    ),
    (
        "bridge_registration_body.jinja",
        include_str!("templates/bridge_registration_body.jinja"),
    ),
    (
        "php_named_let_binding.jinja",
        include_str!("templates/php_named_let_binding.jinja"),
    ),
    (
        "php_vec_named_let_binding.jinja",
        include_str!("templates/php_vec_named_let_binding.jinja"),
    ),
    (
        "php_sanitized_vec_let_binding.jinja",
        include_str!("templates/php_sanitized_vec_let_binding.jinja"),
    ),
    (
        "php_vec_string_refs_let_binding.jinja",
        include_str!("templates/php_vec_string_refs_let_binding.jinja"),
    ),
    (
        "php_vec_named_struct_let_binding.jinja",
        include_str!("templates/php_vec_named_struct_let_binding.jinja"),
    ),
    (
        "php_struct_field_assignment.jinja",
        include_str!("templates/php_struct_field_assignment.jinja"),
    ),
    (
        "php_impl_from_begin.jinja",
        include_str!("templates/php_impl_from_begin.jinja"),
    ),
    (
        "php_impl_from_end.jinja",
        include_str!("templates/php_impl_from_end.jinja"),
    ),
    (
        "php_lossy_binding_struct_end.jinja",
        include_str!("templates/php_lossy_binding_struct_end.jinja"),
    ),
    (
        "php_lossy_binding_struct_begin.jinja",
        include_str!("templates/php_lossy_binding_struct_begin.jinja"),
    ),
    (
        "php_default_update.jinja",
        include_str!("templates/php_default_update.jinja"),
    ),
    (
        "php_duration_default_expr.jinja",
        include_str!("templates/php_duration_default_expr.jinja"),
    ),
    (
        "php_phpdoc_block_start.jinja",
        include_str!("templates/php_phpdoc_block_start.jinja"),
    ),
    (
        "php_phpdoc_block_end.jinja",
        include_str!("templates/php_phpdoc_block_end.jinja"),
    ),
    (
        "php_phpdoc_empty_line.jinja",
        include_str!("templates/php_phpdoc_empty_line.jinja"),
    ),
    (
        "php_phpdoc_text_line.jinja",
        include_str!("templates/php_phpdoc_text_line.jinja"),
    ),
    (
        "php_phpdoc_lines.jinja",
        include_str!("templates/php_phpdoc_lines.jinja"),
    ),
    (
        "php_phpdoc_param_line.jinja",
        include_str!("templates/php_phpdoc_param_line.jinja"),
    ),
    (
        "php_phpdoc_return_line.jinja",
        include_str!("templates/php_phpdoc_return_line.jinja"),
    ),
    (
        "php_phpdoc_throws_line.jinja",
        include_str!("templates/php_phpdoc_throws_line.jinja"),
    ),
    (
        "php_class_declaration.jinja",
        include_str!("templates/php_class_declaration.jinja"),
    ),
    ("php_class_end.jinja", include_str!("templates/php_class_end.jinja")),
    (
        "php_method_signature_start.jinja",
        include_str!("templates/php_method_signature_start.jinja"),
    ),
    (
        "php_method_signature_end.jinja",
        include_str!("templates/php_method_signature_end.jinja"),
    ),
    ("php_method_end.jinja", include_str!("templates/php_method_end.jinja")),
    (
        "php_constructor_method.jinja",
        include_str!("templates/php_constructor_method.jinja"),
    ),
    ("php_file_header.jinja", include_str!("templates/php_file_header.jinja")),
    (
        "php_declare_strict_types.jinja",
        include_str!("templates/php_declare_strict_types.jinja"),
    ),
    ("php_namespace.jinja", include_str!("templates/php_namespace.jinja")),
    (
        "php_namespace_block_begin.jinja",
        include_str!("templates/php_namespace_block_begin.jinja"),
    ),
    (
        "php_namespace_block_end.jinja",
        include_str!("templates/php_namespace_block_end.jinja"),
    ),
    (
        "php_class_registration.jinja",
        include_str!("templates/php_class_registration.jinja"),
    ),
    (
        "php_class_declaration_final.jinja",
        include_str!("templates/php_class_declaration_final.jinja"),
    ),
    (
        "php_method_call_void.jinja",
        include_str!("templates/php_method_call_void.jinja"),
    ),
    (
        "php_method_call_return.jinja",
        include_str!("templates/php_method_call_return.jinja"),
    ),
    (
        "php_exception_class.jinja",
        include_str!("templates/php_exception_class.jinja"),
    ),
    (
        "php_opaque_class_stub.jinja",
        include_str!("templates/php_opaque_class_stub.jinja"),
    ),
    (
        "php_record_class_begin.jinja",
        include_str!("templates/php_record_class_begin.jinja"),
    ),
    (
        "php_property_type_hint.jinja",
        include_str!("templates/php_property_type_hint.jinja"),
    ),
    (
        "php_property_declaration.jinja",
        include_str!("templates/php_property_declaration.jinja"),
    ),
    (
        "php_constructor_doc_param.jinja",
        include_str!("templates/php_constructor_doc_param.jinja"),
    ),
    (
        "php_constructor_doc_return.jinja",
        include_str!("templates/php_constructor_doc_return.jinja"),
    ),
    (
        "php_enum_class_begin.jinja",
        include_str!("templates/php_enum_class_begin.jinja"),
    ),
    (
        "php_tagged_enum_begin.jinja",
        include_str!("templates/php_tagged_enum_begin.jinja"),
    ),
    (
        "php_enum_variant.jinja",
        include_str!("templates/php_enum_variant.jinja"),
    ),
    (
        "php_facade_class_begin.jinja",
        include_str!("templates/php_facade_class_begin.jinja"),
    ),
    (
        "php_facade_function_doc_param.jinja",
        include_str!("templates/php_facade_function_doc_param.jinja"),
    ),
    (
        "php_facade_function_doc_return.jinja",
        include_str!("templates/php_facade_function_doc_return.jinja"),
    ),
    (
        "php_flat_enum_struct_start.jinja",
        include_str!("templates/php_flat_enum_struct_start.jinja"),
    ),
    (
        "php_flat_enum_tag_field.jinja",
        include_str!("templates/php_flat_enum_tag_field.jinja"),
    ),
    (
        "php_flat_enum_option_field.jinja",
        include_str!("templates/php_flat_enum_option_field.jinja"),
    ),
    (
        "php_flat_enum_struct_end.jinja",
        include_str!("templates/php_flat_enum_struct_end.jinja"),
    ),
    (
        "php_flat_enum_impl_from_start.jinja",
        include_str!("templates/php_flat_enum_impl_from_start.jinja"),
    ),
    (
        "php_flat_enum_variant_match_empty.jinja",
        include_str!("templates/php_flat_enum_variant_match_empty.jinja"),
    ),
    (
        "php_flat_enum_impl_match_end.jinja",
        include_str!("templates/php_flat_enum_impl_match_end.jinja"),
    ),
    (
        "php_flat_enum_impl_into_start.jinja",
        include_str!("templates/php_flat_enum_impl_into_start.jinja"),
    ),
    (
        "php_flat_enum_variant_match_into_empty.jinja",
        include_str!("templates/php_flat_enum_variant_match_into_empty.jinja"),
    ),
    (
        "php_flat_enum_variant_field.jinja",
        include_str!("templates/php_flat_enum_variant_field.jinja"),
    ),
    (
        "php_flat_enum_tag_assignment.jinja",
        include_str!("templates/php_flat_enum_tag_assignment.jinja"),
    ),
    (
        "php_flat_enum_fallback_variant_empty.jinja",
        include_str!("templates/php_flat_enum_fallback_variant_empty.jinja"),
    ),
    (
        "php_flat_enum_fallback_variant_tuple_start.jinja",
        include_str!("templates/php_flat_enum_fallback_variant_tuple_start.jinja"),
    ),
    (
        "php_flat_enum_fallback_variant_struct_start.jinja",
        include_str!("templates/php_flat_enum_fallback_variant_struct_start.jinja"),
    ),
    (
        "php_flat_enum_fallback_variant_field.jinja",
        include_str!("templates/php_flat_enum_fallback_variant_field.jinja"),
    ),
    (
        "php_flat_enum_tuple_exprs.jinja",
        include_str!("templates/php_flat_enum_tuple_exprs.jinja"),
    ),
    (
        "php_class_stub_start.jinja",
        include_str!("templates/php_class_stub_start.jinja"),
    ),
    ("php_doc_line.jinja", include_str!("templates/php_doc_line.jinja")),
    (
        "php_enum_stub_start.jinja",
        include_str!("templates/php_enum_stub_start.jinja"),
    ),
    (
        "php_enum_variant_stub.jinja",
        include_str!("templates/php_enum_variant_stub.jinja"),
    ),
    (
        "php_property_annotation.jinja",
        include_str!("templates/php_property_annotation.jinja"),
    ),
    (
        "php_facade_class_declaration.jinja",
        include_str!("templates/php_facade_class_declaration.jinja"),
    ),
    (
        "php_exception_class_declaration.jinja",
        include_str!("templates/php_exception_class_declaration.jinja"),
    ),
    (
        "php_opaque_class_stub_declaration.jinja",
        include_str!("templates/php_opaque_class_stub_declaration.jinja"),
    ),
    (
        "php_record_class_stub_declaration.jinja",
        include_str!("templates/php_record_class_stub_declaration.jinja"),
    ),
    (
        "php_phpdoc_doc_line.jinja",
        include_str!("templates/php_phpdoc_doc_line.jinja"),
    ),
    (
        "php_property_type_annotation.jinja",
        include_str!("templates/php_property_type_annotation.jinja"),
    ),
    (
        "php_property_stub.jinja",
        include_str!("templates/php_property_stub.jinja"),
    ),
    (
        "php_phpdoc_array_param.jinja",
        include_str!("templates/php_phpdoc_array_param.jinja"),
    ),
    (
        "php_phpdoc_array_return.jinja",
        include_str!("templates/php_phpdoc_array_return.jinja"),
    ),
    ("php_getter_stub.jinja", include_str!("templates/php_getter_stub.jinja")),
    (
        "php_tagged_enum_declaration.jinja",
        include_str!("templates/php_tagged_enum_declaration.jinja"),
    ),
    (
        "php_method_call_statement.jinja",
        include_str!("templates/php_method_call_statement.jinja"),
    ),
    (
        "php_api_class_declaration.jinja",
        include_str!("templates/php_api_class_declaration.jinja"),
    ),
    (
        "php_phpdoc_static_param.jinja",
        include_str!("templates/php_phpdoc_static_param.jinja"),
    ),
    (
        "php_phpdoc_static_return.jinja",
        include_str!("templates/php_phpdoc_static_return.jinja"),
    ),
    (
        "php_static_method_stub.jinja",
        include_str!("templates/php_static_method_stub.jinja"),
    ),
    (
        "php_let_binding_named.jinja",
        include_str!("templates/php_let_binding_named.jinja"),
    ),
    (
        "php_let_binding_named_optional.jinja",
        include_str!("templates/php_let_binding_named_optional.jinja"),
    ),
    (
        "php_let_binding_vec_named.jinja",
        include_str!("templates/php_let_binding_vec_named.jinja"),
    ),
    (
        "php_let_binding_vec_named_optional.jinja",
        include_str!("templates/php_let_binding_vec_named_optional.jinja"),
    ),
    (
        "php_let_binding_sanitized_vec_string.jinja",
        include_str!("templates/php_let_binding_sanitized_vec_string.jinja"),
    ),
    (
        "php_let_binding_sanitized_vec_string_optional.jinja",
        include_str!("templates/php_let_binding_sanitized_vec_string_optional.jinja"),
    ),
    (
        "php_let_binding_string_refs.jinja",
        include_str!("templates/php_let_binding_string_refs.jinja"),
    ),
    (
        "php_serde_ref_named_optional_let_binding.jinja",
        include_str!("templates/php_serde_ref_named_optional_let_binding.jinja"),
    ),
    (
        "php_serde_ref_named_let_binding.jinja",
        include_str!("templates/php_serde_ref_named_let_binding.jinja"),
    ),
    (
        "php_result_unit_body.jinja",
        include_str!("templates/php_result_unit_body.jinja"),
    ),
    (
        "php_result_unit_body_with_let_bindings.jinja",
        include_str!("templates/php_result_unit_body_with_let_bindings.jinja"),
    ),
    (
        "php_result_wrapped_body.jinja",
        include_str!("templates/php_result_wrapped_body.jinja"),
    ),
    (
        "php_result_wrapped_body_with_let_bindings.jinja",
        include_str!("templates/php_result_wrapped_body_with_let_bindings.jinja"),
    ),
    (
        "php_result_debug_body_with_let_bindings.jinja",
        include_str!("templates/php_result_debug_body_with_let_bindings.jinja"),
    ),
    (
        "php_async_result_body_with_let_bindings.jinja",
        include_str!("templates/php_async_result_body_with_let_bindings.jinja"),
    ),
    (
        "php_async_body_with_let_bindings.jinja",
        include_str!("templates/php_async_body_with_let_bindings.jinja"),
    ),
    (
        "php_debug_body_with_let_bindings.jinja",
        include_str!("templates/php_debug_body_with_let_bindings.jinja"),
    ),
    (
        "php_wrapped_body_with_let_bindings.jinja",
        include_str!("templates/php_wrapped_body_with_let_bindings.jinja"),
    ),
    (
        "php_stub_error_body.jinja",
        include_str!("templates/php_stub_error_body.jinja"),
    ),
    (
        "php_enum_string_match_arm.jinja",
        include_str!("templates/php_enum_string_match_arm.jinja"),
    ),
    (
        "php_enum_string_match_fallback_arm.jinja",
        include_str!("templates/php_enum_string_match_fallback_arm.jinja"),
    ),
    (
        "php_enum_string_optional_match_expr.jinja",
        include_str!("templates/php_enum_string_optional_match_expr.jinja"),
    ),
    (
        "php_enum_string_match_expr.jinja",
        include_str!("templates/php_enum_string_match_expr.jinja"),
    ),
    (
        "php_enum_variant_unit_expr.jinja",
        include_str!("templates/php_enum_variant_unit_expr.jinja"),
    ),
    (
        "php_enum_variant_tuple_expr.jinja",
        include_str!("templates/php_enum_variant_tuple_expr.jinja"),
    ),
    (
        "php_enum_variant_struct_expr.jinja",
        include_str!("templates/php_enum_variant_struct_expr.jinja"),
    ),
    (
        "php_enum_variant_default_field_expr.jinja",
        include_str!("templates/php_enum_variant_default_field_expr.jinja"),
    ),
    (
        "php_method_definition_no_params.jinja",
        include_str!("templates/php_method_definition_no_params.jinja"),
    ),
    (
        "php_method_definition_with_params.jinja",
        include_str!("templates/php_method_definition_with_params.jinja"),
    ),
    (
        "php_static_method_definition_no_params.jinja",
        include_str!("templates/php_static_method_definition_no_params.jinja"),
    ),
    (
        "php_static_method_definition_with_params.jinja",
        include_str!("templates/php_static_method_definition_with_params.jinja"),
    ),
    (
        "php_async_static_method_definition_no_params.jinja",
        include_str!("templates/php_async_static_method_definition_no_params.jinja"),
    ),
    (
        "php_async_static_method_definition_with_params.jinja",
        include_str!("templates/php_async_static_method_definition_with_params.jinja"),
    ),
    (
        "php_async_instance_method_definition_no_params.jinja",
        include_str!("templates/php_async_instance_method_definition_no_params.jinja"),
    ),
    (
        "php_async_instance_method_definition_with_params.jinja",
        include_str!("templates/php_async_instance_method_definition_with_params.jinja"),
    ),
    (
        "php_visit_result_with_template.jinja",
        include_str!("templates/php_visit_result_with_template.jinja"),
    ),
    (
        "php_trait_impl_start.jinja",
        include_str!("templates/php_trait_impl_start.jinja"),
    ),
    (
        "php_visitor_method_signature.jinja",
        include_str!("templates/php_visitor_method_signature.jinja"),
    ),
    (
        "php_visitor_method_php_call.jinja",
        include_str!("templates/php_visitor_method_php_call.jinja"),
    ),
    (
        "php_visitor_method_result_match.jinja",
        include_str!("templates/php_visitor_method_result_match.jinja"),
    ),
    (
        "php_bridge_function_definition.jinja",
        include_str!("templates/php_bridge_function_definition.jinja"),
    ),
    (
        "php_visitor_arg_nodecontext.jinja",
        include_str!("templates/php_visitor_arg_nodecontext.jinja"),
    ),
    (
        "php_visitor_arg_optional_string_ref.jinja",
        include_str!("templates/php_visitor_arg_optional_string_ref.jinja"),
    ),
    (
        "php_visitor_arg_string_ref.jinja",
        include_str!("templates/php_visitor_arg_string_ref.jinja"),
    ),
    (
        "php_visitor_arg_string_owned.jinja",
        include_str!("templates/php_visitor_arg_string_owned.jinja"),
    ),
    (
        "php_visitor_arg_bool.jinja",
        include_str!("templates/php_visitor_arg_bool.jinja"),
    ),
    (
        "php_visitor_arg_default.jinja",
        include_str!("templates/php_visitor_arg_default.jinja"),
    ),
    (
        "php_visitor_template_var_let_binding.jinja",
        include_str!("templates/php_visitor_template_var_let_binding.jinja"),
    ),
    (
        "php_visitor_interface_start.jinja",
        include_str!("templates/php_visitor_interface_start.jinja"),
    ),
    (
        "php_visitor_interface_method.jinja",
        include_str!("templates/php_visitor_interface_method.jinja"),
    ),
    (
        "php_interface_start.jinja",
        include_str!("templates/php_interface_start.jinja"),
    ),
    (
        "php_interface_method.jinja",
        include_str!("templates/php_interface_method.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}"))
}
