"use client"

import * as React from "react"
import { Provider } from "jotai";
import { ConvexProvider, ConvexReactClient } from "convex/react";

// Pin the live EU deployment over any stale `happy-otter-123` value a Vercel
// project might still inject (same issue the web app hit). Use the env var only
// when it's a real, non-stale URL; otherwise fall back to the live EU backend.
const LIVE_CONVEX_URL = "https://cautious-rhinoceros-421.eu-west-1.convex.cloud";
const envConvexUrl = process.env.NEXT_PUBLIC_CONVEX_URL;
const convexUrl =
  envConvexUrl && !envConvexUrl.includes("happy-otter") ? envConvexUrl : LIVE_CONVEX_URL;
const convex = new ConvexReactClient(convexUrl);

export function Providers({ children }: { children: React.ReactNode }) {
  return (
    <ConvexProvider client={convex}>
      <Provider>
        {children}
      </Provider>
    </ConvexProvider>
  );
};
