/*  Copyright (C) 2025 Gideon Zenz

    This file is part of Gadgetbridge.

    Gadgetbridge is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Gadgetbridge is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.util.healthconnect.syncers

import androidx.health.connect.client.records.StepsRecord
import androidx.health.connect.client.records.metadata.Metadata
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.time.Instant
import java.time.ZoneOffset
import java.time.temporal.ChronoUnit
import kotlin.reflect.KClass

internal object StepsSyncer : AbstractActivitySampleSyncer<StepsRecord>() {
    override val logger: Logger = LoggerFactory.getLogger(StepsSyncer::class.java)
    override val recordClass: KClass<StepsRecord> = StepsRecord::class

    override fun convertSample(
        sample: ActivitySample,
        offset: ZoneOffset,
        metadata: Metadata,
        deviceName: String,
        version: Long
    ): StepsRecord? {
        val stepsInMinute = sample.steps.toLong()
        // <= 0 means "no steps in that minute" - common, drop silently.
        if (stepsInMinute <= 0L) {
            return null
        }
        if (stepsInMinute > 1_000_000L) {
            logger.skipOutOfRange(deviceName, "Steps", stepsInMinute, "1..1000000 per record")
            return null
        }

        val endTs = Instant.ofEpochSecond(sample.timestamp.toLong())
        val startTs = endTs.minus(1, ChronoUnit.MINUTES)

        return StepsRecord(
            startTs,
            offset,
            endTs,
            offset,
            stepsInMinute,
            clientRecordMetadata(metadata, "steps", endTs.epochSecond, version)
        )
    }
}
