import packageInfo from "../../../package.json";

import type { FeedbackContext } from "@/data/schemas/feedback";
import { BrowserDetector } from "@/utils/browser-detect";
import { PlatformDetector } from "@/utils/platform-detect";

export function createFeedbackContext(): FeedbackContext {
  const platform = PlatformDetector.getPlatformInfo();
  const browser = BrowserDetector.getBrowserInfo();
  const browserName = browser.inAppBrowserName
    ? `${browser.inAppBrowserName} IAB / ${browser.name}`
    : browser.name;

  return {
    appVersion: packageInfo.version,
    platform: joinContextParts([
      platform.platform,
      platform.osName,
      platform.osVersion,
    ]),
    browser: joinContextParts([browserName, browser.version]),
    viewport:
      typeof window === "undefined"
        ? "unknown"
        : `${window.innerWidth}x${window.innerHeight}@${window.devicePixelRatio}`,
  };
}

function joinContextParts(parts: readonly string[]): string {
  return parts.filter((part) => part.trim().length > 0).join(" ") || "unknown";
}
