import { z } from "zod";

const smokeEnvSchema = z.object({
  accessToken: z.string().min(1),
  agentModelBaseUrl: z.url().optional(),
  agentModelName: z.string().min(1).optional(),
  agentModelProvider: z.string().min(1).optional(),
  convexUrl: z.url(),
  cpaApiKey: z.string().min(1),
  cpaBaseUrl: z.url(),
  daemonId: z.string().min(1),
  featureRequest: z.string().min(10),
  flueUrl: z.url(),
  projectId: z.string().min(1).optional(),
  webUrl: z.url(),
});

export type SmokeEnv = z.infer<typeof smokeEnvSchema>;

export const parseSmokeEnv = (
  runtimeEnv: Record<string, string | undefined>
): SmokeEnv =>
  smokeEnvSchema.parse({
    accessToken: runtimeEnv.ZOPU_SMOKE_ACCESS_TOKEN,
    agentModelBaseUrl: runtimeEnv.AGENT_MODEL_BASE_URL,
    agentModelName: runtimeEnv.AGENT_MODEL_NAME,
    agentModelProvider: runtimeEnv.AGENT_MODEL_PROVIDER,
    convexUrl: runtimeEnv.ZOPU_SMOKE_CONVEX_URL ?? runtimeEnv.CONVEX_URL,
    cpaApiKey:
      runtimeEnv.ZOPU_SMOKE_CPA_API_KEY ?? runtimeEnv.AGENT_MODEL_API_KEY,
    cpaBaseUrl:
      runtimeEnv.ZOPU_SMOKE_CPA_BASE_URL ?? runtimeEnv.AGENT_MODEL_BASE_URL,
    daemonId: runtimeEnv.ZOPU_SMOKE_DAEMON_ID ?? runtimeEnv.DAEMON_ID,
    featureRequest:
      runtimeEnv.ZOPU_SMOKE_FEATURE_REQUEST ??
      "Add a tiny accessible status control to the existing web project's primary page, keep existing behavior unchanged, and add a focused test for it.",
    flueUrl:
      runtimeEnv.ZOPU_SMOKE_FLUE_URL ??
      runtimeEnv.VITE_FLUE_URL ??
      "http://localhost:3583",
    projectId: runtimeEnv.ZOPU_SMOKE_PROJECT_ID,
    webUrl:
      runtimeEnv.ZOPU_SMOKE_WEB_URL ??
      runtimeEnv.SITE_URL ??
      "http://localhost:5173",
  });
