using System;
using System.IO;
using FitConnect;
using FluentAssertions;
using Newtonsoft.Json;
using NUnit.Framework;

namespace E2ETests;

public class SenderTest {
    private string _clientId = "";
    private string _clientSecret = "";
    private Sender _sender;

    [OneTimeSetUp]
    public void OneTimeSetup() {
        // relative to the project execution directory
        const string secretFile = "../../../http-client.private.env.json";

        if (!File.Exists(secretFile)) {
            // If the secret file is not found, create it with the default values
            // The file will be pretty in C#11, when """ is introduced
            File.WriteAllText(secretFile, @"
{
    ""sender"": {
        ""id"": ""00000000-0000-0000-0000-000000000000"",
        ""secret"": ""0000000000000000000000000000000000000000000"",
        ""scope"": ""send:region:DE""
    }
}");
            throw new Exception("Please fill the secret.json file with your sender credentials");
        }

        var jsonContent = File.ReadAllText(secretFile);
        var secret = JsonConvert.DeserializeObject<dynamic>(jsonContent);
        _clientId = secret.sender.id;
        _clientSecret = secret.sender.secret;
    }

    [SetUp]
    public void Setup() {
        _sender = new Sender(null,
            FitConnectEndpoints.Create(FitConnectEndpoints.EndpointType.Development));
    }

    [Test]
    public void CheckIfSecretsAreValid() {
        _clientId.Should().NotBe("00000000-0000-0000-0000-000000000000");
        _clientSecret.Should().NotBe("0000000000000000000000000000000000000000000");
    }

    [Test]
    public void GetAccessToken() {
        var token = _sender.GetTokenAsync(_clientId, _clientSecret).Result;
        token.Should().NotBeNull();
        token.ExpiresIn.Should().Be(1800);
    }
}