package com.nate.posedetection

import androidx.compose.runtime.Composable
import com.performancecoachlab.posedetection.custom.ModelPath
import platform.Foundation.NSBundle
import platform.Foundation.NSURL

// Scans the main bundle for compiled Core ML models (.mlmodelc) at runtime.
@Composable
actual fun discoverModels(): List<NamedModel> {
    val bundle = NSBundle.mainBundle
    // pathsForResourcesOfType returns full paths to every .mlmodelc bundle in the app
    @Suppress("UNCHECKED_CAST")
    val paths = bundle.pathsForResourcesOfType("mlmodelc", inDirectory = null) as List<String>
    return paths
        .map { path ->
            // e.g. ".../balls_full.mlmodelc" -> "balls_full"
            val resourceName = NSURL.fileURLWithPath(path)
                .lastPathComponent
                ?.removeSuffix(".mlmodelc")
                ?: path
            val displayName = resourceName.replace("_", " ")
            NamedModel(
                name = displayName,
                path = ModelPath(iosModelPath = resourceName)
            )
        }
        .sortedBy { it.name }
}

