namespace QuantEngine.Web.Client.Services;

public class AppStateService
{
    private UserContext _currentUser;
    private List<string> _userRoles = new();
    private bool _isInitialized = false;

    public event Action OnStateChanged;

    public UserContext CurrentUser
    {
        get => _currentUser;
        set
        {
            _currentUser = value;
            NotifyStateChanged();
        }
    }

    public List<string> UserRoles
    {
        get => _userRoles;
        set
        {
            _userRoles = value;
            NotifyStateChanged();
        }
    }

    public bool IsInitialized
    {
        get => _isInitialized;
        set
        {
            _isInitialized = value;
            NotifyStateChanged();
        }
    }

    public AppStateService()
    {
        _currentUser = new UserContext();
        _userRoles = new List<string>();
    }

    /// <summary>
    /// Initialize app state from current user context
    /// </summary>
    public async Task InitializeAsync(HttpClient httpClient)
    {
        try
        {
            var response = await httpClient.GetAsync("api/auth/user");
            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                // Parse user info (implement as needed)
                CurrentUser = new UserContext { Name = "Admin", Email = "admin@quantengine.local" };
                UserRoles = new List<string> { "Admin" };
            }
        }
        catch
        {
            // Handle error
        }
        finally
        {
            IsInitialized = true;
        }
    }

    /// <summary>
    /// Check if user has specific role (RBAC)
    /// </summary>
    public bool HasRole(string role)
    {
        return UserRoles.Contains(role);
    }

    /// <summary>
    /// Check if user has any of the specified roles
    /// </summary>
    public bool HasAnyRole(params string[] roles)
    {
        return roles.Any(r => UserRoles.Contains(r));
    }

    /// <summary>
    /// Check if user has all specified roles
    /// </summary>
    public bool HasAllRoles(params string[] roles)
    {
        return roles.All(r => UserRoles.Contains(r));
    }

    /// <summary>
    /// Clear user state
    /// </summary>
    public void Clear()
    {
        CurrentUser = new UserContext();
        UserRoles = new List<string>();
        IsInitialized = false;
    }

    private void NotifyStateChanged() => OnStateChanged?.Invoke();
}

/// <summary>
/// User context model
/// </summary>
public class UserContext
{
    public string Id { get; set; } = "";
    public string Name { get; set; } = "";
    public string Email { get; set; } = "";
    public DateTime CreatedAt { get; set; } = DateTime.Now;
    public bool IsActive { get; set; } = true;
}

/// <summary>
/// API Response wrapper
/// </summary>
public class ApiResponse<T>
{
    public bool Success { get; set; }
    public string Message { get; set; }
    public T Data { get; set; }
}

/// <summary>
/// Pagination model
/// </summary>
public class PaginatedResponse<T>
{
    public List<T> Items { get; set; }
    public int PageNumber { get; set; }
    public int PageSize { get; set; }
    public int TotalCount { get; set; }
    public int TotalPages => (TotalCount + PageSize - 1) / PageSize;
}
