// @ts-nocheck
/* eslint-disable */

import { z } from "zod";

/**
 * Specify your server-side environment variables schema here. This way you can ensure the app isn't
 * built with invalid env vars.
 */
const server = z.object({
  NODE_ENV: z.enum(["development", "test", "production"]),
  INTERNAL_BASE_URL: z.string().url(),
  PGHOST: z.string(),
  POSTGRES_USER: z.string(),
  POSTGRES_DB: z.string(),
  POSTGRES_PASSWORD: z.string(),
  PGPORT: z.string(),
  BACKENDSERVER_URL: z.string(),
  STRIPE_SECRET_KEY: z.string(),
  STRIPE_LIMITED_KEY: z.string(),
  STRIPE_WEBHOOK_SECRET: z.string(),
  FIC_CLIENT_ID: z.string(),
  FIC_ACCESS_TOKEN: z.string(),
  FIC_COMPANY_ID: z.string(),
  ARUBA_USER: z.string(),
  ARUBA_PASS: z.string(),
  EXP_API_USER: z.string(),
  EXP_API_PASS: z.string(),
  TILES_URL: z.string(),
  KEYDB_URL: z.string(),
  JWT_SECRET: z.string(),
  SKIP_REDIS: z.string(),
});

/**
 * Specify your client-side environment variables schema here. This way you can ensure the app isn't
 * built with invalid env vars. To expose them to the client, prefix them with `NEXT_PUBLIC_`.
 */
const client = z.object({
  NEXT_PUBLIC_BASE_URL: z.string(),
  NEXT_PUBLIC_STRIPE_PUBLIC_KEY: z.string(),
});

/**
 * You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g.
 * middlewares) or client-side so we need to destruct manually.
 *
 * @type {Record<keyof z.infer<typeof server> | keyof z.infer<typeof client>, string | undefined>}
 */
const processEnv = {
  NODE_ENV: process.env.NODE_ENV,
  INTERNAL_BASE_URL: process.env.INTERNAL_BASE_URL,
  PGHOST: process.env.PGHOST,
  POSTGRES_USER: process.env.POSTGRES_USER,
  POSTGRES_DB: process.env.POSTGRES_DB,
  POSTGRES_PASSWORD: process.env.POSTGRES_PASSWORD,
  PGPORT: process.env.PGPORT,
  BACKENDSERVER_URL: process.env.BACKENDSERVER_URL,
  NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL,
  STRIPE_SECRET_KEY: process.env.STRIPE_SECRET_KEY,
  NEXT_PUBLIC_STRIPE_PUBLIC_KEY: process.env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY,
  STRIPE_LIMITED_KEY: process.env.STRIPE_LIMITED_KEY,
  STRIPE_WEBHOOK_SECRET: process.env.STRIPE_WEBHOOK_SECRET,
  FIC_CLIENT_ID: process.env.FIC_CLIENT_ID,
  FIC_ACCESS_TOKEN: process.env.FIC_ACCESS_TOKEN,
  FIC_COMPANY_ID: process.env.FIC_COMPANY_ID,
  ARUBA_USER: process.env.ARUBA_USER,
  ARUBA_PASS: process.env.ARUBA_PASS,
  EXP_API_USER: process.env.EXP_API_USER,
  EXP_API_PASS: process.env.EXP_API_PASS,
  TILES_URL: process.env.TILES_URL,
  KEYDB_URL: process.env.KEYDB_URL,
  JWT_SECRET: process.env.JWT_SECRET,
  SKIP_REDIS: process.env.SKIP_REDIS,
};

// Don't touch the part below
// --------------------------

const merged = server.merge(client);

/** @typedef {z.input<typeof merged>} MergedInput */
/** @typedef {z.infer<typeof merged>} MergedOutput */
/** @typedef {z.SafeParseReturnType<MergedInput, MergedOutput>} MergedSafeParseReturn */

let env = /** @type {MergedOutput} */ (process.env);

if (!!process.env.SKIP_ENV_VALIDATION === false) {
  const isServer = typeof window === "undefined";
  const parsed = /** @type {MergedSafeParseReturn} */ (
    isServer
      ? merged.safeParse(processEnv) // on server we can validate all env vars
      : client.safeParse(processEnv) // on client we can only validate the ones that are exposed
  );

  if (parsed.success === false) {
    console.error(
      "❌ Invalid environment variables:",
      parsed.error.flatten().fieldErrors,
    );
    throw new Error("Invalid environment variables");
  }

  env = new Proxy(parsed.data, {
    get(target, prop) {
      if (typeof prop !== "string") return undefined;
      // Throw a descriptive error if a server-side env var is accessed on the client
      // Otherwise it would just be returning `undefined` and be annoying to debug
      if (!isServer && !prop.startsWith("NEXT_PUBLIC_"))
        throw new Error(
          process.env.NODE_ENV === "production"
            ? "❌ Attempted to access a server-side environment variable on the client"
            : `❌ Attempted to access server-side environment variable '${prop}' on the client`,
        );
      return target[/** @type {keyof typeof target} */ (prop)];
    },
  });
}

export { env };
