import { createEnv } from '@t3-oss/env-nextjs';
import { z } from 'zod/v4';

export const env = createEnv({
  server: {
    NODE_ENV: z
      .enum(['development', 'production', 'test'])
      .default('development'),
    SKIP_ENV_VALIDATION: z.boolean().default(false),
    SENTRY_AUTH_TOKEN: z.string(),
    CI: z.boolean().default(false),
    AUTH_GITHUB_ID: z.string().optional(),
    AUTH_GITHUB_SECRET: z.string().optional(),
    GITHUB_APP_ID: z.string().optional(),
    GITHUB_APP_CLIENT_ID: z.string().optional(),
    GITHUB_APP_CLIENT_SECRET: z.string().optional(),
    GITHUB_APP_PRIVATE_KEY: z.string().optional(),
    GITHUB_APP_WEBHOOK_SECRET: z.string().optional(),
    GITHUB_APP_SLUG: z.string().optional(),
    GITHUB_APP_INSTALLATION_ID: z.string().optional(),
    GITHUB_APP_OWNER: z.string().optional(),
    SPOON_AGENT_WORKER_URL: z.url().default('http://localhost:3921'),
    SPOON_AGENT_WORKER_INTERNAL_TOKEN: z.string().optional(),
    SPOON_WORKER_TOKEN: z.string().optional(),
    // Secret shared with the worker for signing short-lived terminal tokens.
    // Falls back (in code) to the worker internal token.
    SPOON_AGENT_TERMINAL_SECRET: z.string().optional(),
  },

  /**
   * Specify your client-side environment variables schema here.
   * For them to be exposed to the client, prefix them with `NEXT_PUBLIC_`.
   */
  client: {
    NEXT_PUBLIC_SITE_URL: z.url(),
    NEXT_PUBLIC_CONVEX_URL: z.url(),
    NEXT_PUBLIC_PLAUSIBLE_URL: z.url(),
    NEXT_PUBLIC_SENTRY_DSN: z.string(),
    NEXT_PUBLIC_SENTRY_URL: z.string(),
    NEXT_PUBLIC_SENTRY_ORG: z.string(),
    NEXT_PUBLIC_SENTRY_PROJECT_NAME: z.string(),
    // Browser-facing WebSocket base URL of the agent worker, e.g.
    // `wss://worker.spoon.gbrown.org` (prod) or `ws://localhost:3921` (dev).
    // When unset, the workspace Terminal tab is disabled.
    NEXT_PUBLIC_SPOON_AGENT_WORKER_WS_URL: z.string().optional(),
  },
  /**
   * Destructure all variables from `process.env` to make sure they aren't tree-shaken away.
   */
  runtimeEnv: {
    NODE_ENV: process.env.NODE_ENV,
    SKIP_ENV_VALIDATION: process.env.SKIP_ENV_VALIDATION,
    SENTRY_AUTH_TOKEN: process.env.SENTRY_AUTH_TOKEN,
    CI: process.env.CI,
    AUTH_GITHUB_ID: process.env.AUTH_GITHUB_ID,
    AUTH_GITHUB_SECRET: process.env.AUTH_GITHUB_SECRET,
    GITHUB_APP_ID: process.env.GITHUB_APP_ID,
    GITHUB_APP_CLIENT_ID: process.env.GITHUB_APP_CLIENT_ID,
    GITHUB_APP_CLIENT_SECRET: process.env.GITHUB_APP_CLIENT_SECRET,
    GITHUB_APP_PRIVATE_KEY: process.env.GITHUB_APP_PRIVATE_KEY,
    GITHUB_APP_WEBHOOK_SECRET: process.env.GITHUB_APP_WEBHOOK_SECRET,
    GITHUB_APP_SLUG: process.env.GITHUB_APP_SLUG,
    GITHUB_APP_INSTALLATION_ID: process.env.GITHUB_APP_INSTALLATION_ID,
    GITHUB_APP_OWNER: process.env.GITHUB_APP_OWNER,
    SPOON_AGENT_WORKER_URL: process.env.SPOON_AGENT_WORKER_URL,
    SPOON_AGENT_WORKER_INTERNAL_TOKEN:
      process.env.SPOON_AGENT_WORKER_INTERNAL_TOKEN,
    SPOON_WORKER_TOKEN: process.env.SPOON_WORKER_TOKEN,
    SPOON_AGENT_TERMINAL_SECRET: process.env.SPOON_AGENT_TERMINAL_SECRET,
    NEXT_PUBLIC_SPOON_AGENT_WORKER_WS_URL:
      process.env.NEXT_PUBLIC_SPOON_AGENT_WORKER_WS_URL,
    NEXT_PUBLIC_SITE_URL: process.env.NEXT_PUBLIC_SITE_URL,
    NEXT_PUBLIC_CONVEX_URL: process.env.NEXT_PUBLIC_CONVEX_URL,
    NEXT_PUBLIC_PLAUSIBLE_URL: process.env.NEXT_PUBLIC_PLAUSIBLE_URL,
    NEXT_PUBLIC_SENTRY_DSN: process.env.NEXT_PUBLIC_SENTRY_DSN,
    NEXT_PUBLIC_SENTRY_URL: process.env.NEXT_PUBLIC_SENTRY_URL,
    NEXT_PUBLIC_SENTRY_ORG: process.env.NEXT_PUBLIC_SENTRY_ORG,
    NEXT_PUBLIC_SENTRY_PROJECT_NAME:
      process.env.NEXT_PUBLIC_SENTRY_PROJECT_NAME,
  },
  skipValidation: !!process.env.SKIP_ENV_VALIDATION,
  emptyStringAsUndefined: true,
});
