import { z } from "zod";

const envSchema = z.object({
    // server stuff
    VITE_PORT: z.string().default("4200"),
    LOG_LEVEL: z.string().default("info"),

    // db stuff
    DATABASE_HOST: z.string(),
    DATABASE_PORT: z.string(),
    DATABASE_USER: z.string(),
    DATABASE_PASSWORD: z.string(),
    DATABASE_DB: z.string().default("lst"),

    // prod stuff
    PROD_SERVER: z.string(),
    PROD_PLANT_TOKEN: z.string(),
    PROD_USER: z.string(),
    PROD_PASSWORD: z.string(),

    // auth stuff
    BETTER_AUTH_URL: z.string(),
    BETTER_AUTH_SECRET: z.string(),

    // docker specifc
    RUNNING_IN_DOCKER: z.string().default("false"),
});

export type Env = z.infer<typeof envSchema>;

export function validateEnv(raw: NodeJS.ProcessEnv): Env {
    const parsed = envSchema.safeParse(raw);
    if (!parsed.success) {
        throw new Error(
            `Environment validation failed. Missing: ${parsed.error.issues
                .map((e) => e.path[0])
                .join(", ")}`
        );
    }
    return parsed.data;
}
