package com.atridad.ascently.utils

import java.security.MessageDigest

/**
 * Utility for creating consistent image filenames across iOS and Android platforms. Uses
 * deterministic naming based on problem ID and timestamp to ensure sync compatibility.
 */
object ImageNamingUtils {

    private const val IMAGE_EXTENSION = ".jpg"
    private const val HASH_LENGTH = 12 // First 12 chars of SHA-256

    /** Generates a deterministic filename for a problem image */
    fun generateImageFilename(problemId: String, imageIndex: Int): String {
        val input = "${problemId}_$imageIndex"
        val hash = createHash(input)

        return "problem_${hash}_${imageIndex}${IMAGE_EXTENSION}"
    }

    /** Legacy method for backward compatibility */
    @Suppress("UNUSED_PARAMETER")
    fun generateImageFilename(problemId: String, timestamp: String, imageIndex: Int): String {
        return generateImageFilename(problemId, imageIndex)
    }

    /** Creates a deterministic hash from input string */
    private fun createHash(input: String): String {
        val digest = MessageDigest.getInstance("SHA-256")
        val hashBytes = digest.digest(input.toByteArray(Charsets.UTF_8))
        val hashHex = hashBytes.joinToString("") { "%02x".format(it) }
        return hashHex.take(HASH_LENGTH)
    }

    /** Creates a mapping of existing server filenames to canonical filenames */
}
