import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";

const convexUrlSchema = (exampleHost: string) =>
  z.url().refine((url) => new URL(url).hostname !== exampleHost, {
    message: `Replace the ${exampleHost} placeholder before running the app`,
  });

export const env = createEnv({
  server: {
    CONVEX_URL: convexUrlSchema("example.convex.cloud"),
    DAEMON_ID: z.string().min(1),
    DAEMON_NAME: z.string().min(1).optional(),
    DAEMON_VERSION: z.string().default("0.0.0"),
    DAEMON_HEARTBEAT_MS: z.coerce.number().int().positive().default(15_000),
    DAEMON_COMMAND_LEASE_MS: z.coerce.number().int().positive().default(60_000),
    FLUE_DB_TOKEN: z.string().min(1),
    RIVET_ENDPOINT: z.url().optional(),
  },
  runtimeEnv: process.env,
  skipValidation: process.env.SKIP_ENV_VALIDATION === "true",
  emptyStringAsUndefined: true,
});
