using Microsoft.AspNetCore.Mvc;

namespace GameIdeas.WebAPI.Controllers;

[ApiController]
[Route("api/[controller]")]

public class TranslationsController (ILogger<TranslationsController> Logger) : ControllerBase
{
    [HttpGet]
	public async Task<IActionResult> GetTranslations()
	{
		var dictionary = new Dictionary<string, string>();

		try
		{
			var filesDirectory = Path.Combine(
				Directory.GetCurrentDirectory(),
				"Files");
			var translationFiles = Directory.GetFiles(filesDirectory, "*.json");
			foreach (var file in translationFiles)
			{
				var name = file.Split('.');
				var culture = name[^2];
				var content = await System.IO.File.ReadAllTextAsync(file);
				dictionary.Add(culture, content);
			}
		}
		catch(Exception ex)
		{
			Logger.LogError(ex, "Internal translations error");
		}

		return Ok(dictionary);
	}
}