// Copyright 2012-2026 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Config templates mirror Go templates from server/jetstream_helpers_test.go.
// Note: C# string.Format uses {{ }} to escape literal braces.

namespace ZB.MOM.NatsNet.Server.IntegrationTests.Helpers;

/// <summary>
/// Config templates and temp config file management for integration tests.
/// Templates mirror the Go originals from server/jetstream_helpers_test.go.
/// </summary>
internal static class ConfigHelper
{
    // =========================================================================
    // Config templates
    // =========================================================================

    /// <summary>
    /// Standard JetStream cluster template.
    /// Placeholders: {0}=server_name, {1}=store_dir, {2}=cluster_name,
    ///               {3}=cluster_port, {4}=routes.
    /// Mirrors Go <c>jsClusterTempl</c>.
    /// </summary>
    public const string JsClusterTemplate = @"
listen: 127.0.0.1:-1
server_name: {0}
jetstream: {{max_mem_store: 2GB, max_file_store: 2GB, store_dir: '{1}'}}

leaf {{
    listen: 127.0.0.1:-1
}}

cluster {{
    name: {2}
    listen: 127.0.0.1:{3}
    routes = [{4}]
}}

# For access to system account.
accounts {{ $SYS {{ users = [ {{ user: ""admin"", pass: ""s3cr3t!"" }} ] }} }}
";

    /// <summary>
    /// JetStream cluster template with multiple named accounts.
    /// Placeholders: {0}=server_name, {1}=store_dir, {2}=cluster_name,
    ///               {3}=cluster_port, {4}=routes.
    /// Mirrors Go <c>jsClusterAccountsTempl</c>.
    /// </summary>
    public const string JsClusterAccountsTemplate = @"
listen: 127.0.0.1:-1
server_name: {0}
jetstream: {{max_mem_store: 2GB, max_file_store: 2GB, store_dir: '{1}'}}

leaf {{
    listen: 127.0.0.1:-1
}}

cluster {{
    name: {2}
    listen: 127.0.0.1:{3}
    routes = [{4}]
}}

no_auth_user: one

accounts {{
    ONE {{ users = [ {{ user: ""one"", pass: ""p"" }} ]; jetstream: enabled }}
    TWO {{ users = [ {{ user: ""two"", pass: ""p"" }} ]; jetstream: enabled }}
    NOJS {{ users = [ {{ user: ""nojs"", pass: ""p"" }} ] }}
    $SYS {{ users = [ {{ user: ""admin"", pass: ""s3cr3t!"" }} ] }}
}}
";

    /// <summary>
    /// Super-cluster gateway wrapper template.
    /// Placeholders: {0}=inner_cluster_config, {1}=gateway_name,
    ///               {2}=gateway_port, {3}=gateway_list.
    /// Mirrors Go <c>jsSuperClusterTempl</c>.
    /// </summary>
    public const string JsSuperClusterTemplate = @"
{0}
gateway {{
    name: {1}
    listen: 127.0.0.1:{2}
    gateways = [{3}
    ]
}}

system_account: ""$SYS""
";

    /// <summary>
    /// Gateway entry template used inside <see cref="JsSuperClusterTemplate"/>.
    /// Placeholders: {0}=prefix_whitespace, {1}=gateway_name, {2}=urls.
    /// Mirrors Go <c>jsGWTempl</c>.
    /// </summary>
    public const string JsGatewayEntryTemplate = @"{0}{{name: {1}, urls: [{2}]}}";

    // =========================================================================
    // File helpers
    // =========================================================================

    /// <summary>
    /// Writes <paramref name="content"/> to a temporary file and returns the path.
    /// The caller is responsible for deleting the file when done.
    /// </summary>
    public static string CreateConfigFile(string content)
    {
        var path = Path.Combine(Path.GetTempPath(), "nats-test-" + Guid.NewGuid().ToString("N")[..8] + ".conf");
        File.WriteAllText(path, content);
        return path;
    }
}
