package com.hipograf.companion

import android.app.Application
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import com.hipograf.companion.sync.SyncWorker
import java.util.concurrent.TimeUnit

class HipografApp : Application() {

    override fun onCreate() {
        super.onCreate()

        // Schedule periodic sync every 15 minutes (if logged in)
        val prefs = getSharedPreferences("hipograf", MODE_PRIVATE)
        if (prefs.getString("token", null) != null) {
            scheduleSyncWorker()
        }
    }

    fun scheduleSyncWorker() {
        val syncRequest = PeriodicWorkRequestBuilder<SyncWorker>(
            15, TimeUnit.MINUTES
        ).build()

        WorkManager.getInstance(this).enqueueUniquePeriodicWork(
            "hipograf_samsung_sync",
            ExistingPeriodicWorkPolicy.KEEP,
            syncRequest
        )
    }

    fun cancelSyncWorker() {
        WorkManager.getInstance(this).cancelUniqueWork("hipograf_samsung_sync")
    }
}
