You are an expert code reviewer. Your task is to analyze the provided code files and provide a comprehensive code review. For each file, consider:

1. Code Quality: Assess readability, maintainability, and adherence to best practices
2. Potential Issues: Identify bugs, security vulnerabilities, or performance concerns
3. Suggestions: Provide specific recommendations for improvements

Format your review as follows:
1. Start with a brief overview of all files
2. For each file, provide:
   - A summary of the file's purpose
   - Key findings (both positive and negative)
   - Specific recommendations
3. End with any overall suggestions for the codebase

Your review should be detailed but concise, focusing on the most important aspects of the code.

Files to review:

File: ./src/lib/env.ts
Content:
import { z } from "https://deno.land/x/zod@v3.23.8/mod.ts";

/**
 * Zod schema for validating environment variables.
 */
export const envSchema = z.object({
  NODE_ENV: z
    .enum(["development", "production", "test"])
    .default("development"),
  TEST: z.array(z.string()).default(["test_value"]),
});

type EnvValue = string | string[] | number;
type EnvVars = Record<string, EnvValue>;

/**
 * Parses a string value into an array of strings.
 *
 * @param value - The string value to parse.
 * @returns An array of non-empty trimmed strings.
 */
function parseStringArray(value: string): string[] {
  // Yeet that string into an array, no cap
  return value
    .split(" ")
    .map((item) => item.trim())
    .filter(Boolean);
}

/**
 * Resolves a reference to another environment variable.
 *
 * @param value - The reference string (starting with $).
 * @param envVars - The current environment variables object.
 * @returns The resolved value or the original reference if not found.
 */
function resolveEnvReference(value: string, envVars: EnvVars): string {
  // Time to play "Find that var" - it's giving hide and seek vibes
  return value.replace(/\$([A-Z0-9_]+)/g, (_, p1) => {
    const replacement = envVars[p1];
    if (typeof replacement === "string") {
      return resolveEnvReference(replacement, envVars);
    }
    return replacement !== undefined ? String(replacement) : `$${p1}`;
  });
}

/**
 * Attempts to parse a string as a number.
 *
 * @param value - The string to parse.
 * @returns The parsed number or the original string if parsing fails.
 */
function parseNumberOrString(value: string): number | string {
  const num = Number(value);
  // Is it giving number? Or is it just a string catfishing us?
  return isNaN(num) ? value : num;
}

/**
 * Parses an environment variable value.
 *
 * @param value - The string value to parse.
 * @param envVars - The current environment variables object.
 * @returns The parsed value as a string, string array, or number.
 */
function parseEnv(value: string, envVars: EnvVars): EnvValue {
  const resolvedValue = resolveEnvReference(value, envVars);

  // This value be looking kinda sus, might need to split
  if (resolvedValue.includes(" ")) {
    return parseStringArray(resolvedValue);
  }

  return parseNumberOrString(resolvedValue);
}

/**
 * Loads environment variables from a file.
 *
 * @param filename - The name of the file to load.
 * @returns An object containing the loaded environment variables.
 */
export async function loadEnv(filename: string): Promise<EnvVars> {
  try {
    const envText = await Deno.readTextFile(filename);
    const envVars: EnvVars = {};

    // Time to split these lines like my parents split up
    const lines = envText.split("\n");
    for (const line of lines) {
      const equalIndex = line.indexOf("=");
      if (equalIndex > 0) {
        const key = line.slice(0, equalIndex).trim();
        const value = line.slice(equalIndex + 1).trim();
        envVars[key] = value; // Yolo, store that raw value
      }
    }

    // Resolve variable references after all variables are loaded
    for (const [key, value] of Object.entries(envVars)) {
      if (typeof value === "string") {
        envVars[key] = parseEnv(value, envVars);
      }
    }

    return envVars;
  } catch {
    return {}; // Oof, empty object. This file ghosted us harder than my ex
  }
}

/**
 * Type definition for the environment variables.
 */
export type Env = z.infer<typeof envSchema>;

/**
 * Loads and parses environment variables.
 *
 * @param customEnvPath - Optional custom path for the .env file.
 * @returns A promise that resolves to the parsed environment variables.
 * @throws {z.ZodError} If the loaded environment variables fail validation.
 */
export async function loadAndParseEnv(customEnvPath?: string): Promise<Env> {
  const nodeEnv = Deno.env.get("NODE_ENV")?.toLowerCase() ?? "development";
  let loadedEnv: EnvVars = {};

  // Time to go on a scavenger hunt for env files, it's giving Amazing Race vibes
  const envPaths = [
    customEnvPath,
    `.env.${nodeEnv}`,
    ".env",
    ...getAdditionalEnvPaths(),
  ].filter(Boolean);

  for (const path of envPaths) {
    try {
      loadedEnv = await loadEnv(path as string);
      if (Object.keys(loadedEnv).length > 0) {
        break; // We found a non-empty env file, it's a whole vibe
      }
    } catch {
      // noop (No Operation, like my will to adult today)
    }
  }

  const combinedEnv = { ...loadedEnv, ...Deno.env.toObject() };

  // Time to face Zod, the final boss. It's gonna be lit... or we'll rage quit
  return envSchema.parse(combinedEnv);
}

/**
 * Gets additional environment file paths from configuration.
 *
 * @returns An array of additional environment file paths.
 */
function getAdditionalEnvPaths(): string[] {
  const additionalPaths = Deno.env.get("ADDITIONAL_ENV_PATHS");
  // More env files? This config is extra af
  return additionalPaths ? parseStringArray(additionalPaths) : [];
}

/**
 * The parsed and validated environment variables.
 *
 * @example
 * ```ts
 * console.log(env.NODE_ENV);
 * ```
 */
export const env = await loadAndParseEnv();



PROMPT: You are an expert code reviewer. Your task is to analyze the provided code files and provide a comprehensive code review. For each file, consider:

1. Code Quality: Assess readability, maintainability, and adherence to best practices
2. Potential Issues: Identify bugs, security vulnerabilities, or performance concerns
3. Suggestions: Provide specific recommendations for improvements

Format your review as follows:
1. Start with a brief overview of all files
2. For each file, provide:
   - A summary of the file's purpose
   - Key findings (both positive and negative)
   - Specific recommendations
3. End with any overall suggestions for the codebase

Your review should be detailed but concise, focusing on the most important aspects of the code.

Files to review:

File: ./src/lib/env.ts
Content:
import { z } from "https://deno.land/x/zod@v3.23.8/mod.ts";

/**
 * Zod schema for validating environment variables.
 */
export const envSchema = z.object({
  NODE_ENV: z
    .enum(["development", "production", "test"])
    .default("development"),
  TEST: z.array(z.string()).default(["test_value"]),
});

type EnvValue = string | string[] | number;
type EnvVars = Record<string, EnvValue>;

/**
 * Parses a string value into an array of strings.
 *
 * @param value - The string value to parse.
 * @returns An array of non-empty trimmed strings.
 */
function parseStringArray(value: string): string[] {
  // Yeet that string into an array, no cap
  return value
    .split(" ")
    .map((item) => item.trim())
    .filter(Boolean);
}

/**
 * Resolves a reference to another environment variable.
 *
 * @param value - The reference string (starting with $).
 * @param envVars - The current environment variables object.
 * @returns The resolved value or the original reference if not found.
 */
function resolveEnvReference(value: string, envVars: EnvVars): string {
  // Time to play "Find that var" - it's giving hide and seek vibes
  return value.replace(/\$([A-Z0-9_]+)/g, (_, p1) => {
    const replacement = envVars[p1];
    if (typeof replacement === "string") {
      return resolveEnvReference(replacement, envVars);
    }
    return replacement !== undefined ? String(replacement) : `$${p1}`;
  });
}

/**
 * Attempts to parse a string as a number.
 *
 * @param value - The string to parse.
 * @returns The parsed number or the original string if parsing fails.
 */
function parseNumberOrString(value: string): number | string {
  const num = Number(value);
  // Is it giving number? Or is it just a string catfishing us?
  return isNaN(num) ? value : num;
}

/**
 * Parses an environment variable value.
 *
 * @param value - The string value to parse.
 * @param envVars - The current environment variables object.
 * @returns The parsed value as a string, string array, or number.
 */
function parseEnv(value: string, envVars: EnvVars): EnvValue {
  const resolvedValue = resolveEnvReference(value, envVars);

  // This value be looking kinda sus, might need to split
  if (resolvedValue.includes(" ")) {
    return parseStringArray(resolvedValue);
  }

  return parseNumberOrString(resolvedValue);
}

/**
 * Loads environment variables from a file.
 *
 * @param filename - The name of the file to load.
 * @returns An object containing the loaded environment variables.
 */
export async function loadEnv(filename: string): Promise<EnvVars> {
  try {
    const envText = await Deno.readTextFile(filename);
    const envVars: EnvVars = {};

    // Time to split these lines like my parents split up
    const lines = envText.split("\n");
    for (const line of lines) {
      const equalIndex = line.indexOf("=");
      if (equalIndex > 0) {
        const key = line.slice(0, equalIndex).trim();
        const value = line.slice(equalIndex + 1).trim();
        envVars[key] = value; // Yolo, store that raw value
      }
    }

    // Resolve variable references after all variables are loaded
    for (const [key, value] of Object.entries(envVars)) {
      if (typeof value === "string") {
        envVars[key] = parseEnv(value, envVars);
      }
    }

    return envVars;
  } catch {
    return {}; // Oof, empty object. This file ghosted us harder than my ex
  }
}

/**
 * Type definition for the environment variables.
 */
export type Env = z.infer<typeof envSchema>;

/**
 * Loads and parses environment variables.
 *
 * @param customEnvPath - Optional custom path for the .env file.
 * @returns A promise that resolves to the parsed environment variables.
 * @throws {z.ZodError} If the loaded environment variables fail validation.
 */
export async function loadAndParseEnv(customEnvPath?: string): Promise<Env> {
  const nodeEnv = Deno.env.get("NODE_ENV")?.toLowerCase() ?? "development";
  let loadedEnv: EnvVars = {};

  // Time to go on a scavenger hunt for env files, it's giving Amazing Race vibes
  const envPaths = [
    customEnvPath,
    `.env.${nodeEnv}`,
    ".env",
    ...getAdditionalEnvPaths(),
  ].filter(Boolean);

  for (const path of envPaths) {
    try {
      loadedEnv = await loadEnv(path as string);
      if (Object.keys(loadedEnv).length > 0) {
        break; // We found a non-empty env file, it's a whole vibe
      }
    } catch {
      // noop (No Operation, like my will to adult today)
    }
  }

  const combinedEnv = { ...loadedEnv, ...Deno.env.toObject() };

  // Time to face Zod, the final boss. It's gonna be lit... or we'll rage quit
  return envSchema.parse(combinedEnv);
}

/**
 * Gets additional environment file paths from configuration.
 *
 * @returns An array of additional environment file paths.
 */
function getAdditionalEnvPaths(): string[] {
  const additionalPaths = Deno.env.get("ADDITIONAL_ENV_PATHS");
  // More env files? This config is extra af
  return additionalPaths ? parseStringArray(additionalPaths) : [];
}

/**
 * The parsed and validated environment variables.
 *
 * @example
 * ```ts
 * console.log(env.NODE_ENV);
 * ```
 */
export const env = await loadAndParseEnv();