/*
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package com.runicgateway.app.core.auth

import android.os.Build
import javax.inject.Inject
import javax.inject.Singleton

/**
 * Supplies a friendly label for this device, sent as `device_name` at login so a
 * trusted-device / active-session row is recognizable in the account lists
 * (TRUSTED_DEVICES_MFA.md). Behind an interface so the auth repository stays free of
 * `android.os.Build` and unit-testable on the JVM.
 */
fun interface DeviceNameProvider {
    /** A human label like "Google Pixel 8", or null if nothing meaningful is available. */
    fun deviceName(): String?
}

/** Production impl: manufacturer + model from [Build] (e.g. "Samsung SM-S918B"). */
@Singleton
class BuildDeviceNameProvider @Inject constructor() : DeviceNameProvider {
    override fun deviceName(): String? {
        val manufacturer = Build.MANUFACTURER?.trim().orEmpty()
        val model = Build.MODEL?.trim().orEmpty()
        val label = when {
            model.isEmpty() -> manufacturer
            manufacturer.isEmpty() || model.startsWith(manufacturer, ignoreCase = true) -> model
            else -> "$manufacturer $model"
        }.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }
        return label.take(100).ifBlank { null }
    }
}
