// See https://aka.ms/new-console-template for more information

using NJsonSchema.CodeGeneration.TypeScript;
using NSwag;
using NSwag.CodeGeneration.OperationNameGenerators;
using NSwag.CodeGeneration.TypeScript;

var document = OpenApiDocument.FromUrlAsync(("http://localhost:5000/swagger/v1/swagger.json")).Result;

// Get the solution directory by going up from the current executable location
var currentDirectory = Directory.GetCurrentDirectory();
var solutionDirectory = currentDirectory;

// Navigate up until we find the src directory or reach a reasonable limit
for (int i = 0; i < 10; i++)
{
    if (Directory.Exists(Path.Combine(solutionDirectory, "src")))
        break;
    
    var parent = Directory.GetParent(solutionDirectory);
    if (parent == null)
        break;
    solutionDirectory = parent.FullName;
}

var targetDirectory = Path.Combine(solutionDirectory, "src", "Managing.WebApp", "src", "generated");
Directory.CreateDirectory(targetDirectory); // Ensure the directory exists

var settings = new TypeScriptClientGeneratorSettings
{
    ClassName = "{controller}Client",
    ClientBaseClass = "AuthorizedApiBase",
    ConfigurationClass = "IConfig",
    GenerateDtoTypes = true,
    UseTransformOptionsMethod = true,
    TypeScriptGeneratorSettings =
    {
        EnumStyle = TypeScriptEnumStyle.Enum,
        DateTimeType = TypeScriptDateTimeType.Date,
        NullValue = TypeScriptNullValue.Null,
        TypeStyle = TypeScriptTypeStyle.Interface,
        GenerateDefaultValues = true,
        MarkOptionalProperties = true,
        TypeScriptVersion = 4.3m
    },
    OperationNameGenerator = new MultipleClientsFromFirstTagAndOperationIdGenerator(),
    Template = TypeScriptTemplate.Fetch,
};

var generatorApiClient = new TypeScriptClientGenerator(document, settings);
var codeApiClient = generatorApiClient.GenerateFile();

// Add the necessary imports after the auto-generated comment
var requiredImports = @"
import AuthorizedApiBase from ""./AuthorizedApiBase"";
import IConfig from ""./IConfig"";
";

// Find the end of the auto-generated comment and insert imports
var autoGeneratedEndIndex = codeApiClient.IndexOf("//----------------------");
if (autoGeneratedEndIndex != -1)
{
    // Find the second occurrence (end of the comment block)
    autoGeneratedEndIndex = codeApiClient.IndexOf("//----------------------", autoGeneratedEndIndex + 1);
    if (autoGeneratedEndIndex != -1)
    {
        autoGeneratedEndIndex = codeApiClient.IndexOf("\n", autoGeneratedEndIndex) + 1;
        codeApiClient = codeApiClient.Insert(autoGeneratedEndIndex, requiredImports);
    }
}

File.WriteAllText(Path.Combine(targetDirectory, "ManagingApi.ts"), codeApiClient);

var settingsTypes = new TypeScriptClientGeneratorSettings
{
    GenerateDtoTypes = true,
    TypeScriptGeneratorSettings =
    {
        EnumStyle = TypeScriptEnumStyle.Enum,
        DateTimeType = TypeScriptDateTimeType.Date,
        NullValue = TypeScriptNullValue.Null,
        TypeStyle = TypeScriptTypeStyle.Interface,
        GenerateDefaultValues = true,
        MarkOptionalProperties = true,
        TypeScriptVersion = 4.3m
    },
    GenerateClientClasses = false,
    GenerateClientInterfaces = false,
    GenerateOptionalParameters = false,
    GenerateResponseClasses = false,
};

var generatorTypes = new TypeScriptClientGenerator(document, settingsTypes);
var codeTypes = generatorTypes.GenerateFile();
File.WriteAllText(Path.Combine(targetDirectory, "ManagingApiTypes.ts"), codeTypes);