/**
 * Environment Variable Validation for Vreko VS Code Extension
 *
 * Uses composable presets from @vreko/env with T3-env core for
 * VS Code extension context (no database, payments, or email services needed).
 *
 * @example
 * ```typescript
 * import { env } from "@/lib/env";
 * // output:(env.VREKO_API_URL);
 * ```
 *
 * Presets included:
 * - platform: NODE_ENV, LOG_LEVEL, CORS, Vercel/Fly.io platform vars
 * - analytics: POSTHOG_*, SENTRY_* for telemetry
 *
 * Extension-specific variables:
 * - VREKO_API_URL: API endpoint
 * - VREKO_MCP_URL: MCP server endpoint
 * - VREKO_TELEMETRY_PROXY: Telemetry proxy endpoint
 * - VREKO_RULES_PUBLIC_KEY: Rules bundle verification
 * - VSCODE_SNAPSHOT_TEST_MODE: Test mode flag
 */

import { createEnv } from "@t3-oss/env-core";
import { analytics, platform } from "@vreko/env/presets";
import { z } from "zod";

export const env = createEnv({
	/**
	 * Extend from @vreko/env presets
	 * Extension only needs platform and analytics - no database, auth, payments, etc.
	 */
	extends: [platform(), analytics()],

	/**
	 * Extension-specific environment variables
	 */
	server: {
		// API Endpoints
		VREKO_API_URL: z
			.string()
			.url()
			.default("https://api.vreko.dev")
			.describe("Vreko API endpoint for extension-server communication"),

		VREKO_MCP_URL: z
			.string()
			.url()
			.default("https://mcp.vreko.dev")
			.describe("Vreko MCP server endpoint for AI assistant features"),

		VREKO_TELEMETRY_PROXY: z
			.string()
			.url()
			.default("https://api.vreko.dev/telemetry")
			.describe("Telemetry proxy endpoint for analytics events"),

		// Security
		VREKO_RULES_PUBLIC_KEY: z
			.string()
			.optional()
			.describe("Public key for verifying signed rules bundles (JWS verification)"),

		// Testing
		VSCODE_SNAPSHOT_TEST_MODE: z
			.enum(["true", "false"])
			.default("false")
			.transform((v) => v === "true")
			.describe("Enable test mode for snapshot testing"),

		// PostHog (extension-specific)
		POSTHOG_PROJECT_KEY: z
			.string()
			.optional()
			.describe("PostHog project key for extension telemetry (legacy, prefer POSTHOG_API_KEY)"),

		// Proxy configuration (for corporate environments)
		HTTP_PROXY: z.string().optional().describe("HTTP proxy URL for corporate environments"),
		HTTPS_PROXY: z.string().optional().describe("HTTPS proxy URL for corporate environments"),
		NO_PROXY: z.string().optional().describe("Comma-separated list of hosts to bypass proxy"),

		// System paths (for sandboxed execution)
		PATH: z.string().optional().describe("System PATH environment variable"),
		HOME: z.string().optional().describe("User home directory path"),
	},

	/**
	 * Runtime environment variables mapping
	 */
	runtimeEnv: {
		// Extension-specific
		VREKO_API_URL: process.env.VREKO_API_URL,
		VREKO_MCP_URL: process.env.VREKO_MCP_URL,
		VREKO_TELEMETRY_PROXY: process.env.VREKO_TELEMETRY_PROXY,
		VREKO_RULES_PUBLIC_KEY: process.env.VREKO_RULES_PUBLIC_KEY,
		VSCODE_SNAPSHOT_TEST_MODE: process.env.VSCODE_SNAPSHOT_TEST_MODE,
		POSTHOG_PROJECT_KEY: process.env.POSTHOG_PROJECT_KEY,
		HTTP_PROXY: process.env.HTTP_PROXY,
		HTTPS_PROXY: process.env.HTTPS_PROXY,
		NO_PROXY: process.env.NO_PROXY,
		PATH: process.env.PATH,
		HOME: process.env.HOME,
	},

	/**
	 * Skip validation during build/CI
	 * Most extension env vars have defaults, so this is safe
	 */
	skipValidation: !!process.env.SKIP_ENV_VALIDATION,
});

/**
 * Type-safe environment access
 */
export type Env = typeof env;

/**
 * Helper to check if running in test mode
 */
export function isTestMode(): boolean {
	return env.VSCODE_SNAPSHOT_TEST_MODE === true;
}

/**
 * Helper to get API base URL
 */
export function getApiUrl(): string {
	return env.VREKO_API_URL;
}

/**
 * Helper to get MCP server URL
 */
export function getMcpUrl(): string {
	return env.VREKO_MCP_URL;
}
