package com.example.shiftalarm

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import java.util.concurrent.TimeUnit

class BootReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent?) {
        if (intent?.action == Intent.ACTION_BOOT_COMPLETED) {
            android.util.Log.d("ShiftAlarm", "[부팅] 기기 부팅 감지, 알람 복구 시작")
            
            // 1) 즉시 1회 실행 → 당일 알람을 바로 복구
            val immediateWork = OneTimeWorkRequestBuilder<AlarmWorker>().build()
            WorkManager.getInstance(context).enqueueUniqueWork(
                "BootAlarmRestore",
                androidx.work.ExistingWorkPolicy.REPLACE,
                immediateWork
            )

            // 2) 24시간 주기 반복 워커 등록
            val periodicWork = PeriodicWorkRequestBuilder<AlarmWorker>(24, TimeUnit.HOURS)
                .build()
            WorkManager.getInstance(context).enqueueUniquePeriodicWork(
                "DailyShiftCheck",
                androidx.work.ExistingPeriodicWorkPolicy.KEEP,
                periodicWork
            )
            
            android.util.Log.d("ShiftAlarm", "[부팅] 알람 복구 워커 등록 완료")
        }
    }
}
