using System.Text.Json;

namespace Ocr.Report.Models;

internal struct TagFileInfo
{
  public string Path { get; private init; }

  public string ImageName { get; set; }

  public ICollection<string> GetWords()
  {
    using var file = File.OpenRead(Path);
    return JsonDocument
      .Parse(file)
      .RootElement
      .GetProperty("words")
      .EnumerateArray()
      .Select(w => w.GetString() ?? throw new Exception("Cannot parse null words"))
      .ToArray();
  }

  public static TagFileInfo FromPath(string path) => new()
  {
    Path = path,
    ImageName = System.IO.Path.GetFileNameWithoutExtension(path),
  };

  /// <inheritdoc />
  public override string ToString() => ImageName;
}
