import { cookies } from 'next/headers'

const DEVICE_ID_COOKIE_NAME = 'sd'

export const serverDeviceManager = {
  // 仅获取设备ID（不创建）
  async getDeviceId(): Promise<string | null> {
    const cookieStore = await cookies()
    return cookieStore.get(DEVICE_ID_COOKIE_NAME)?.value || null
  },

  // 从请求头获取设备ID（中间件设置的）
  async getDeviceIdFromHeaders(): Promise<string | null> {
    try {
      const { headers } = await import('next/headers')
      const headersList = await headers()
      return headersList.get('x-device-id') || null
    } catch {
      return null
    }
  },
}
