using ArgosV2.AppInsight.Logging;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Abstractions;

namespace ArgosV2.Server.WebAPI.Controllers;

[Route("api/[controller]")]
[ApiController]
public class TranslationsController (ITelemetryService telemetryClient) : ControllerBase
{
	[Authorize]
	[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)
		{
			telemetryClient.TrackException<TranslationsController>(ex);
		}

		return Ok(dictionary);
	}
}