using B2Net;

namespace Api;

public class EpisodeUrl {
  public required string Url { get; set; }
  public required string Token { get; set; }
}

public class B2ApiClient {
  private static string? KeyId => Environment.GetEnvironmentVariable("KEY_ID");
  private static string? Key => Environment.GetEnvironmentVariable("SECRET_KEY");
  private static string? BucketId => Environment.GetEnvironmentVariable("BUCKET_ID");
  private static string? Bucket => Environment.GetEnvironmentVariable("BUCKET");

  private readonly B2Client _client;

  public B2ApiClient() {
    _client = new B2Client(KeyId, Key);
  }

  public async Task<EpisodeUrl> GetDownloadLink(string file) {
    var auth = await _client.Files.GetDownloadAuthorization(file, 14400, BucketId);
    return new EpisodeUrl {
      Url = _client.Files.GetFriendlyDownloadUrl(file, Bucket),
      Token = auth.AuthorizationToken,
    };
  }
}
