package com.social.media.domain.socialaccount.valueobject;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * Value Object representing social account authentication tokens
 */
public record AuthenticationTokens(
    String accessToken,
    String refreshToken,
    LocalDateTime expiresAt,
    List<String> scopes,
    Map<String, Object> additionalData
) {
    
    public AuthenticationTokens {
        if (accessToken == null || accessToken.trim().isEmpty()) {
            throw new IllegalArgumentException("Access token cannot be null or empty");
        }
        scopes = scopes != null ? List.copyOf(scopes) : List.of();
        additionalData = additionalData != null ? Map.copyOf(additionalData) : Map.of();
    }
    
    public boolean isExpired() {
        return expiresAt != null && expiresAt.isBefore(LocalDateTime.now());
    }
    
    public boolean hasScope(String scope) {
        return scopes.contains(scope);
    }
    
    public boolean hasRefreshToken() {
        return refreshToken != null && !refreshToken.trim().isEmpty();
    }
    
    public boolean needsRefresh() {
        return isExpired() && hasRefreshToken();
    }
    
    public AuthenticationTokens withNewTokens(String newAccessToken, String newRefreshToken, 
                                            LocalDateTime newExpiresAt) {
        return new AuthenticationTokens(
            newAccessToken,
            newRefreshToken != null ? newRefreshToken : this.refreshToken,
            newExpiresAt,
            this.scopes,
            this.additionalData
        );
    }
    
    public long getMinutesToExpiry() {
        if (expiresAt == null) {
            return Long.MAX_VALUE;
        }
        return java.time.Duration.between(LocalDateTime.now(), expiresAt).toMinutes();
    }
}
