using Newtonsoft.Json;
using System.Text;

namespace IWS5.Autotest.IWS5.Api.Common
{
    public static class JwtTokenHelper
    {
        public static string GenerateFakeJwtToken(string fakeUserId = "12345", string fakeEmail = "fake@example.com")
        {
            var header = new { alg = "HS256", typ = "JWT" };
            var payload = new
            {
                sub = fakeUserId,
                email = fakeEmail,
                iat = DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
                exp = DateTimeOffset.UtcNow.AddMinutes(5).ToUnixTimeSeconds()
            };

            string encodedHeader = Base64UrlEncode(JsonConvert.SerializeObject(header));
            string encodedPayload = Base64UrlEncode(JsonConvert.SerializeObject(payload));
            string fakeSignature = Base64UrlEncode("fakesignature");

            return $"Bearer {encodedHeader}.{encodedPayload}.{fakeSignature}";
        }

        private static string Base64UrlEncode(string input)
        {
            var bytes = Encoding.UTF8.GetBytes(input);
            return Convert.ToBase64String(bytes)
                .TrimEnd('=')
                .Replace('+', '-')
                .Replace('/', '_');
        }
    }
}