package com.example.initiateaphonecallapp.data.model

import android.net.Uri
import android.provider.MediaStore

data class AudioFile(
    val id: Long,
    val displayName: String,
    val title: String,
    val artist: String,
    val album: String,
    val duration: Long,
    val size: Long,
    val dateAdded: Long,
    val dateModified: Long,
    val contentUri: Uri,
    val filePath: String? = null
) {
    val formattedDuration: String
        get() = formatDuration(duration)

    val formattedSize: String
        get() = formatFileSize(size)
}

private fun formatDuration(milliseconds: Long): String {
    val seconds = milliseconds / 1000
    val minutes = seconds / 60
    val hours = minutes / 60

    return if (hours > 0) {
        String.format("%02d:%02d:%02d", hours, minutes % 60, seconds % 60)
    } else {
        String.format("%02d:%02d", minutes, seconds % 60)
    }
}

private fun formatFileSize(size: Long): String {
    return when {
        size < 1024 -> "$size B"
        size < 1024 * 1024 -> "${String.format("%.1f", size / 1024.0)} KB"
        size < 1024 * 1024 * 1024 -> "${String.format("%.1f", size / (1024.0 * 1024.0))} MB"
        else -> "${String.format("%.1f", size / (1024.0 * 1024.0 * 1024.0))} GB"
    }
}