import type { VoteRequest } from "../types/events";
import type { DeviceContext, ViewportMode } from "../types/player";

export function createSessionId() {
  if (typeof crypto !== "undefined" && "randomUUID" in crypto) {
    return crypto.randomUUID();
  }

  return `sess-${Math.random().toString(36).slice(2, 10)}`;
}

export function createDeviceContext(viewportMode: ViewportMode): DeviceContext {
  if (typeof navigator === "undefined") {
    return {
      viewportMode,
      userAgent: "server",
      platform: "server"
    };
  }

  const navigatorWithUAData = navigator as Navigator & {
    userAgentData?: { platform?: string };
  };

  return {
    viewportMode,
    userAgent: navigator.userAgent,
    platform: navigatorWithUAData.userAgentData?.platform ?? navigator.platform
  };
}

export async function mockVoteApi(_presentationId: string, payload: VoteRequest) {
  await new Promise((resolve) => window.setTimeout(resolve, 160));
  console.log("[vote-api]", payload);
}
