// utils/TimeRangeUtils.kt
package com.example.initiateaphonecallapp.utils

import com.example.initiateaphonecallapp.data.model.CallRecordRequest
import java.text.SimpleDateFormat
import java.util.*

object TimeRangeUtils {

    private val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault())

    /**
     * 创建今天的时间范围请求
     */
    fun createTodayRequest(): CallRecordRequest {
        val calendar = Calendar.getInstance()

        // 今天开始时间
        val startOfDay = calendar.apply {
            set(Calendar.HOUR_OF_DAY, 0)
            set(Calendar.MINUTE, 0)
            set(Calendar.SECOND, 0)
            set(Calendar.MILLISECOND, 0)
        }.time

        // 今天结束时间
        val endOfDay = calendar.apply {
            set(Calendar.HOUR_OF_DAY, 23)
            set(Calendar.MINUTE, 59)
            set(Calendar.SECOND, 59)
            set(Calendar.MILLISECOND, 999)
        }.time

        return CallRecordRequest(
            startTime = dateFormat.format(startOfDay),
            endTime = dateFormat.format(endOfDay)
        )
    }

    /**
     * 创建本周的时间范围请求
     */
    fun createWeekRequest(): CallRecordRequest {
        val calendar = Calendar.getInstance()

        // 本周开始时间（周一）
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.MILLISECOND, 0)
        val startOfWeek = calendar.time

        // 当前时间作为结束时间
        val endOfToday = Calendar.getInstance().apply {
            set(Calendar.HOUR_OF_DAY, 23)
            set(Calendar.MINUTE, 59)
            set(Calendar.SECOND, 59)
            set(Calendar.MILLISECOND, 999)
        }.time

        return CallRecordRequest(
            startTime = dateFormat.format(startOfWeek),
            endTime = dateFormat.format(endOfToday)
        )
    }

    /**
     * 创建本月的时间范围请求
     */
    fun createMonthRequest(): CallRecordRequest {
        val calendar = Calendar.getInstance()

        // 本月开始时间
        calendar.set(Calendar.DAY_OF_MONTH, 1)
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.MILLISECOND, 0)
        val startOfMonth = calendar.time

        // 当前时间作为结束时间
        val endOfToday = Calendar.getInstance().apply {
            set(Calendar.HOUR_OF_DAY, 23)
            set(Calendar.MINUTE, 59)
            set(Calendar.SECOND, 59)
            set(Calendar.MILLISECOND, 999)
        }.time

        return CallRecordRequest(
            startTime = dateFormat.format(startOfMonth),
            endTime = dateFormat.format(endOfToday)
        )
    }

    /**
     * 创建自定义时间范围请求
     */
    fun createCustomRequest(startDate: Date, endDate: Date): CallRecordRequest {
        return CallRecordRequest(
            startTime = dateFormat.format(startDate),
            endTime = dateFormat.format(endDate)
        )
    }
}