import { NextResponse } from 'next/server';
import { cookies } from 'next/headers';
const DEVICE_COOKIE_NAME = 'bf_device_id';
const COOKIE_MAX_AGE = 60 * 60 * 24 * 365 // 1 year
;
/**
 * Generate a unique device ID
 * Uses crypto.randomUUID for secure random generation
 */ function generateDeviceId() {
    return crypto.randomUUID();
}
/**
 * GET /api/device
 * Returns or creates a unique device ID for the client
 * The device ID is stored in an httpOnly cookie for security
 */ export async function GET() {
    const cookieStore = await cookies();
    let deviceId = cookieStore.get(DEVICE_COOKIE_NAME)?.value;
    // Generate new device ID if not exists
    if (!deviceId) {
        deviceId = generateDeviceId();
    }
    // Create response with device ID
    const response = NextResponse.json({
        deviceId
    });
    // Set/refresh the httpOnly cookie
    response.cookies.set(DEVICE_COOKIE_NAME, deviceId, {
        httpOnly: true,
        secure: process.env.NODE_ENV === 'production',
        sameSite: 'strict',
        maxAge: COOKIE_MAX_AGE,
        path: '/'
    });
    return response;
}
