"use client";

import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { Environment, ParaProvider as ParaSDKProvider } from "@getpara/react-sdk";
// @if:evm
import { sepolia } from "wagmi/chains";
// @endif
// @if:cosmos
import { cosmoshub, osmosis, noble } from "graz/chains";
// @endif
// @if:solana
import { WalletAdapterNetwork } from "@solana/wallet-adapter-base";
import { clusterApiUrl } from "@solana/web3.js";
// @endif

const API_KEY = process.env.NEXT_PUBLIC_PARA_API_KEY ?? "";
const ENVIRONMENT = (process.env.NEXT_PUBLIC_PARA_ENVIRONMENT as Environment) || Environment.BETA;

const queryClient = new QueryClient();

// @if:cosmos
const cosmosChains = [cosmoshub, osmosis, noble];
// @endif
// @if:solana
const solanaNetwork = WalletAdapterNetwork.Devnet;
const endpoint = clusterApiUrl(solanaNetwork);
// @endif

export function ParaProvider({ children }: { children: React.ReactNode }) {
  if (!API_KEY) {
    return (
      <div style={{ padding: "20px", textAlign: "center" }}>
        <h2>Missing API Key</h2>
        <p>Please set NEXT_PUBLIC_PARA_API_KEY in your .env file.</p>
        <p>
          Get your API key at{" "}
          <a href="https://developer.getpara.com" target="_blank" rel="noopener noreferrer">
            developer.getpara.com
          </a>
        </p>
      </div>
    );
  }

  return (
    <QueryClientProvider client={queryClient}>
      <ParaSDKProvider
        paraClientConfig={{
          apiKey: API_KEY,
          env: ENVIRONMENT,
        }}
        // @if:hasExternalWallets
        externalWalletConfig={{
          wallets: [
            // @inject:wallets
          ],
          // @if:evm
          evmConnector: {
            config: {
              chains: [sepolia],
            },
          },
          walletConnect: {
            projectId: process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID || "",
          },
          // @endif
          // @if:cosmos
          cosmosConnector: {
            config: {
              chains: cosmosChains,
              selectedChainId: cosmoshub.chainId,
              multiChain: false,
              onSwitchChain: (chainId) => {
                console.log("Switched chain to:", chainId);
              },
            },
          },
          // @endif
          // @if:solana
          solanaConnector: {
            config: {
              endpoint,
              chain: solanaNetwork,
              appIdentity: {
                uri: typeof window !== "undefined" ? `${window.location.protocol}//${window.location.host}` : "",
              },
            },
          },
          // @endif
        }}
        // @endif
        config={{ appName: "{{appName}}" }}
        paraModalConfig={{
          disableEmailLogin: {{disableEmail}},
          disablePhoneLogin: {{disablePhone}},
          authLayout: ["AUTH:FULL", "EXTERNAL:FULL"],
          oAuthMethods: [{{oauthMethods}}],
          theme: {
            foregroundColor: "#222222",
            backgroundColor: "#FFFFFF",
            accentColor: "#888888",
            darkForegroundColor: "#EEEEEE",
            darkBackgroundColor: "#111111",
            darkAccentColor: "#AAAAAA",
            mode: "light",
            borderRadius: "none",
            font: "Inter",
          },
          recoverySecretStepEnabled: true,
          twoFactorAuthEnabled: false,
        }}>
        {children}
      </ParaSDKProvider>
    </QueryClientProvider>
  );
}
