import "dotenv/config";
import { z } from "zod";

const boolFromEnv = z.preprocess((value) => {
  if (typeof value === "boolean") return value;
  const text = String(value ?? "").trim().toLowerCase();
  return text === "1" || text === "true" || text === "yes" || text === "on";
}, z.boolean());

const EnvSchema = z.object({
  DISCORD_TOKEN: z.string().min(1),
  DISCORD_CLIENT_ID: z.string().min(1),
  DISCORD_GUILD_ID: z.string().min(1).optional(),
  DATABASE_URL: z.string().min(1),
  REDIS_URL: z.string().min(1).optional(),
  LAVALINK_NODE_NAME: z.string().default("local"),
  LAVALINK_HOST: z.string().default("127.0.0.1"),
  LAVALINK_PORT: z.coerce.number().default(2333),
  LAVALINK_PASSWORD: z.string().default("youshallnotpass"),
  LAVALINK_SECURE: boolFromEnv.default(false),
  GEMINI_API_KEY: z.string().optional(),
  GEMINI_FLASH_MODEL: z.string().default("gemini-2.5-flash"),
  GEMINI_PRO_MODEL: z.string().default("gemini-2.5-pro"),
  DEEPL_API_KEY: z.string().optional(),
  DEEPL_CLI_BIN: z.string().default("deepl"),
  DEEPL_CLI_TIMEOUT_MS: z.coerce.number().default(45000),
  GDS_DVIEWER_BASE_URL: z.string().optional(),
  GDS_DVIEWER_API_KEY: z.string().optional(),
  GDS_DVIEWER_SOURCE_ID: z.string().default("0"),
  EXTERNAL_VIDEO_PLAYER_URL: z.string().optional(),
  IINA_LINK_ICON: z.string().default("🎬"),
  LOG_LEVEL: z.string().default("info"),
});

export const env = EnvSchema.parse(process.env);
