import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { ClerkProvider } from "@clerk/react";
import App from "./App.jsx";
import "./index.css";

const PUBLISHABLE_KEY = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY;

if (!PUBLISHABLE_KEY) {
  throw new Error("Missing VITE_CLERK_PUBLISHABLE_KEY — add it to signal-ui/.env");
}

// Where Clerk sends the user after sign-in, and where it points its own redirects.
// Defaults to the host the app is actually being served from, so this can never
// again drift out of sync with the deployed domain. Override with VITE_APP_URL
// only if a deploy must redirect somewhere other than its own origin.
// Previously hardcoded to https://signal-ui-xi.vercel.app, which the production
// Clerk instance rejects (origin_invalid), so every sign-in redirected into an
// origin that could not authenticate.
const APP_URL = import.meta.env.VITE_APP_URL ?? window.location.origin;

createRoot(document.getElementById("root")).render(
  <StrictMode>
    <ClerkProvider
      publishableKey={PUBLISHABLE_KEY}
      signInUrl={APP_URL}
      signUpUrl={APP_URL}
      signInFallbackRedirectUrl={APP_URL}
      signUpFallbackRedirectUrl={APP_URL}
    >
      <App />
    </ClerkProvider>
  </StrictMode>
);