import { createEnv } from '@t3-oss/env-nextjs';
import { z } from 'zod';

export const env = createEnv({
    server: {
        DATABASE_URL: z.url(),
        NODE_ENV: z.enum(['development', 'test', 'production']).default('development'),
        AUTH_SECRET: process.env.NODE_ENV === 'production' ? z.string() : z.string().optional(),
        // queues + LLM
        REDIS_URL: z.url().default('redis://127.0.0.1:6379'),
        OPENAI_API_KEY: z.string(),
        OPENAI_BASE_URL: z.url().optional(),
        OPENAI_API_MODEL: z.string().default('gpt-4o-mini'),
        LLM_API_KEY: z.string().optional(),
        LLM_BASE_URL: z.url().optional(),
        LLM_MODEL: z.string().optional(),
        EMBEDDING_API_KEY: z.string().optional(),
        EMBEDDING_BASE_URL: z.url().optional(),
        EMBEDDING_API_PATH: z.string().optional(),
        EMBEDDING_MODEL: z.string().optional(),
        EMBEDDING_DIMENSIONS: z.string().optional(),
        MINERU_API_MODE: z.enum(['agent', 'standard', 'precise']).optional(),
        MINERU_API_TOKEN: z.string().optional(),
        MINERU_API_BASE_URL: z.url().optional(),
        MINERU_AGENT_BASE_URL: z.url().optional(),
        MINERU_MODEL_VERSION: z.string().optional(),
        MINERU_TIMEOUT_MS: z.string().optional(),
        MINERU_POLL_INTERVAL_MS: z.string().optional(),
        MINERU_LANGUAGE: z.string().optional(),
        MINERU_IS_OCR: z.string().optional(),
        MINERU_ENABLE_TABLE: z.string().optional(),
        MINERU_ENABLE_FORMULA: z.string().optional(),
        MINERU_PAGE_RANGE: z.string().optional(),
        MINERU_DISABLE: z.string().optional(),

        // internal HTTP base for tools (e.g., dictionary lookup)
        DICTIONARY_API_URL: z.url().default('http://localhost:3000'),

        // Object storage（可选，STORAGE_TYPE=minio|cos）
        STORAGE_TYPE: z.enum(['minio', 'cos']).optional(),
        STORAGE_ENDPOINT: z.string().optional(),
        STORAGE_PORT: z.string().optional(),
        STORAGE_USE_SSL: z.string().optional(),
        STORAGE_ACCESS_KEY: z.string().optional(),
        STORAGE_SECRET_KEY: z.string().optional(),
        STORAGE_BUCKET: z.string().optional(),
        STORAGE_REGION: z.string().optional(),
        STORAGE_APP_ID: z.string().optional(),
        STORAGE_DOMAIN: z.string().optional(),
        STORAGE_UPLOAD_URL_EXPIRES_SECONDS: z.string().optional(),
        STORAGE_DOWNLOAD_URL_EXPIRES_SECONDS: z.string().optional(),
        MINIO_ACCESS_KEY: z.string().optional(),
        MINIO_SECRET_KEY: z.string().optional(),
        MINIO_BUCKET: z.string().optional(),
        MINIO_BROWSER_REDIRECT_URL: z.url().optional(),
        COS_SECRET_ID: z.string().optional(),
        COS_SECRET_KEY: z.string().optional(),
        COS_BUCKET: z.string().optional(),
        COS_REGION: z.string().optional(),
        COS_APP_ID: z.string().optional(),
        COS_DOMAIN: z.string().optional(),
        COS_USE_SSL: z.string().optional(),
    },

    client: {
        // NEXT_PUBLIC_* 放这里
    },

    runtimeEnv: {
        DATABASE_URL: process.env.DATABASE_URL ?? process.env.POSTGRES_URL,
        NODE_ENV: process.env.NODE_ENV,
        AUTH_SECRET: process.env.AUTH_SECRET ?? process.env.NEXTAUTH_SECRET,
        REDIS_URL: process.env.REDIS_URL,
        OPENAI_API_KEY: process.env.OPENAI_API_KEY,
        OPENAI_BASE_URL: process.env.OPENAI_BASE_URL,
        OPENAI_API_MODEL: process.env.OPENAI_API_MODEL,
        LLM_API_KEY: process.env.LLM_API_KEY,
        LLM_BASE_URL: process.env.LLM_BASE_URL,
        LLM_MODEL: process.env.LLM_MODEL,
        EMBEDDING_API_KEY: process.env.EMBEDDING_API_KEY,
        EMBEDDING_BASE_URL: process.env.EMBEDDING_BASE_URL,
        EMBEDDING_API_PATH: process.env.EMBEDDING_API_PATH,
        EMBEDDING_MODEL: process.env.EMBEDDING_MODEL,
        EMBEDDING_DIMENSIONS: process.env.EMBEDDING_DIMENSIONS,
        MINERU_API_MODE: process.env.MINERU_API_MODE,
        MINERU_API_TOKEN: process.env.MINERU_API_TOKEN,
        MINERU_API_BASE_URL: process.env.MINERU_API_BASE_URL,
        MINERU_AGENT_BASE_URL: process.env.MINERU_AGENT_BASE_URL,
        MINERU_MODEL_VERSION: process.env.MINERU_MODEL_VERSION,
        MINERU_TIMEOUT_MS: process.env.MINERU_TIMEOUT_MS,
        MINERU_POLL_INTERVAL_MS: process.env.MINERU_POLL_INTERVAL_MS,
        MINERU_LANGUAGE: process.env.MINERU_LANGUAGE,
        MINERU_IS_OCR: process.env.MINERU_IS_OCR,
        MINERU_ENABLE_TABLE: process.env.MINERU_ENABLE_TABLE,
        MINERU_ENABLE_FORMULA: process.env.MINERU_ENABLE_FORMULA,
        MINERU_PAGE_RANGE: process.env.MINERU_PAGE_RANGE,
        MINERU_DISABLE: process.env.MINERU_DISABLE,

        STORAGE_TYPE: process.env.STORAGE_TYPE,
        STORAGE_ENDPOINT: process.env.STORAGE_ENDPOINT,
        STORAGE_PORT: process.env.STORAGE_PORT,
        STORAGE_USE_SSL: process.env.STORAGE_USE_SSL,
        STORAGE_ACCESS_KEY: process.env.STORAGE_ACCESS_KEY,
        STORAGE_SECRET_KEY: process.env.STORAGE_SECRET_KEY,
        STORAGE_BUCKET: process.env.STORAGE_BUCKET,
        STORAGE_REGION: process.env.STORAGE_REGION,
        STORAGE_APP_ID: process.env.STORAGE_APP_ID,
        STORAGE_DOMAIN: process.env.STORAGE_DOMAIN,
        STORAGE_UPLOAD_URL_EXPIRES_SECONDS: process.env.STORAGE_UPLOAD_URL_EXPIRES_SECONDS,
        STORAGE_DOWNLOAD_URL_EXPIRES_SECONDS: process.env.STORAGE_DOWNLOAD_URL_EXPIRES_SECONDS,
        MINIO_ACCESS_KEY: process.env.MINIO_ACCESS_KEY,
        MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY,
        MINIO_BUCKET: process.env.MINIO_BUCKET,
        MINIO_BROWSER_REDIRECT_URL: process.env.MINIO_BROWSER_REDIRECT_URL,
        COS_SECRET_ID: process.env.COS_SECRET_ID,
        COS_SECRET_KEY: process.env.COS_SECRET_KEY,
        COS_BUCKET: process.env.COS_BUCKET,
        COS_REGION: process.env.COS_REGION,
        COS_APP_ID: process.env.COS_APP_ID,
        COS_DOMAIN: process.env.COS_DOMAIN,
        COS_USE_SSL: process.env.COS_USE_SSL,
        DICTIONARY_API_URL: process.env.DICTIONARY_API_URL,
    },

    skipValidation: !!process.env.SKIP_ENV_VALIDATION,
    emptyStringAsUndefined: true,
});
