using Godot;
    using System;
    using System.Collections.Generic;
    using System.Text.Json;
    using System.Threading.Tasks;
    using SessionZeroClient.API;
    
    namespace SessionZeroClient;
    
    public class AccountManager
    {
        public string Username { get; private set; } = "";
        public string Email { get; private set; } = "";
        public string Jwt { get; private set; } = "";
        
        private IApiHandler _apiHandler;
    
        public AccountManager(IApiHandler apiHandler)
        {
            _apiHandler = apiHandler;
        }
        
        public async Task<(OperationStatus, string)> Register(string url, string username, string email, string password)
        {
            var (status, response) = await _apiHandler.Register(url, username, email, password);
            return (status, response);
        }
        
        public async Task<(OperationStatus, string)> Login(string url, string username, string password)
        {
            var (status, response) = await _apiHandler.Login(url, username, password);
            
            if (status == OperationStatus.Success)
            {
                var responseData = JsonSerializer.Deserialize<Dictionary<string, string>>(response);
                if (responseData != null && responseData.ContainsKey("token"))
                {
                    Jwt = responseData["token"];
                    Username = username;
                }
            }
            
            return (status, response);
        }
        
        public void Logout()
        {
            Jwt = "";
            Username = "";
            Email = "";
        }
    }