namespace MembershipService.Infrastructure.ExternalServices;

/// <summary>
/// EN: Configuration for IAM Service communication.
/// VI: Cấu hình giao tiếp với IAM Service.
/// </summary>
public class IamServiceSettings
{
    public const string SectionName = "IamService";
    
    /// <summary>EN: IAM Service base URL / VI: URL cơ sở của IAM Service</summary>
    public string BaseUrl { get; set; } = "http://iam-service-net:8080";
    
    /// <summary>EN: Internal service name header / VI: Header tên service nội bộ</summary>
    public string ServiceName { get; set; } = "membership-service";
    
    /// <summary>EN: Request timeout in seconds / VI: Timeout request (giây)</summary>
    public int TimeoutSeconds { get; set; } = 30;
    
    /// <summary>EN: Cache user info duration in seconds / VI: Thời gian cache user info (giây)</summary>
    public int CacheDurationSeconds { get; set; } = 300; // 5 minutes
    
    /// <summary>EN: Health check cache duration in seconds / VI: Thời gian cache health check (giây)</summary>
    public int HealthCheckCacheDurationSeconds { get; set; } = 60; // 1 minute
}

/// <summary>
/// EN: User information from IAM Service.
/// VI: Thông tin user từ IAM Service.
/// </summary>
public record IamUserInfo(
    string UserId,
    string Email,
    string? DisplayName,
    bool IsActive,
    IReadOnlyList<string> Roles,
    IReadOnlyList<string>? Permissions = null);

/// <summary>
/// EN: Role information from IAM Service.
/// VI: Thông tin role từ IAM Service.
/// </summary>
public record IamRoleInfo(
    string Id,
    string Name,
    string? Description,
    IReadOnlyList<string> Permissions);

/// <summary>
/// EN: IAM Service health status.
/// VI: Trạng thái health của IAM Service.
/// </summary>
public record IamHealthStatus(
    bool IsHealthy,
    string Status,
    DateTime CheckedAt);

/// <summary>
/// EN: Interface for communicating with IAM Service.
/// VI: Interface giao tiếp với IAM Service.
/// </summary>
public interface IIamServiceClient
{
    // ============================================
    // EN: User Operations / VI: Thao tác User
    // ============================================
    
    /// <summary>
    /// EN: Validate user token and get user info (with caching).
    /// VI: Xác thực token và lấy thông tin user (có cache).
    /// </summary>
    Task<IamUserInfo?> ValidateUserAsync(string accessToken, CancellationToken cancellationToken = default);
    
    /// <summary>
    /// EN: Get user by ID.
    /// VI: Lấy user theo ID.
    /// </summary>
    Task<IamUserInfo?> GetUserByIdAsync(string userId, string accessToken, CancellationToken cancellationToken = default);
    
    /// <summary>
    /// EN: Check if user exists.
    /// VI: Kiểm tra user có tồn tại.
    /// </summary>
    Task<bool> UserExistsAsync(string userId, string accessToken, CancellationToken cancellationToken = default);
    
    /// <summary>
    /// EN: Get user's roles.
    /// VI: Lấy danh sách roles của user.
    /// </summary>
    Task<IReadOnlyList<string>> GetUserRolesAsync(string userId, string accessToken, CancellationToken cancellationToken = default);
    
    /// <summary>
    /// EN: Get user's permissions.
    /// VI: Lấy danh sách permissions của user.
    /// </summary>
    Task<IReadOnlyList<string>> GetUserPermissionsAsync(string userId, string accessToken, CancellationToken cancellationToken = default);
    
    /// <summary>
    /// EN: Check if user has specific permission.
    /// VI: Kiểm tra user có permission cụ thể.
    /// </summary>
    Task<bool> HasPermissionAsync(string userId, string permission, string accessToken, CancellationToken cancellationToken = default);
    
    /// <summary>
    /// EN: Check if user has specific role.
    /// VI: Kiểm tra user có role cụ thể.
    /// </summary>
    Task<bool> HasRoleAsync(string userId, string role, string accessToken, CancellationToken cancellationToken = default);
    
    // ============================================
    // EN: Health Check / VI: Kiểm tra Health
    // ============================================
    
    /// <summary>
    /// EN: Check IAM Service health (with caching).
    /// VI: Kiểm tra health của IAM Service (có cache).
    /// </summary>
    Task<IamHealthStatus> CheckHealthAsync(CancellationToken cancellationToken = default);
    
    /// <summary>
    /// EN: Check if IAM Service is available.
    /// VI: Kiểm tra IAM Service có sẵn sàng.
    /// </summary>
    Task<bool> IsAvailableAsync(CancellationToken cancellationToken = default);
    
    // ============================================
    // EN: Cache Management / VI: Quản lý Cache
    // ============================================
    
    /// <summary>
    /// EN: Invalidate user cache.
    /// VI: Xóa cache của user.
    /// </summary>
    void InvalidateUserCache(string userId);
    
    /// <summary>
    /// EN: Clear all caches.
    /// VI: Xóa toàn bộ cache.
    /// </summary>
    void ClearCache();
}
