package com.gcsc.connection.apikey.dto;

import com.gcsc.connection.apikey.entity.SnpApiKey;

import java.time.LocalDateTime;

public record ApiKeyResponse(
    Long apiKeyId,
    String keyName,
    String apiKeyPrefix,
    String maskedKey,
    String status,
    String userName,
    LocalDateTime expiresAt,
    LocalDateTime lastUsedAt,
    LocalDateTime createdAt
) {

    public static ApiKeyResponse from(SnpApiKey key) {
        String masked = key.getApiKeyPrefix() + "****...****";
        String ownerName = key.getUser() != null ? key.getUser().getUserName() : null;
        return new ApiKeyResponse(
            key.getApiKeyId(),
            key.getKeyName(),
            key.getApiKeyPrefix(),
            masked,
            key.getStatus().name(),
            ownerName,
            key.getExpiresAt(),
            key.getLastUsedAt(),
            key.getCreatedAt()
        );
    }
}
