// Predefined env var keys yang dikenali sistem
export const ENV_VAR_KEYS = {
  // AI Providers
  OPENAI_API_KEY:       "OPENAI_API_KEY",
  ANTHROPIC_API_KEY:    "ANTHROPIC_API_KEY",
  GOOGLE_AI_API_KEY:    "GOOGLE_AI_API_KEY",
  AGENTROUTER_API_KEY:  "AGENTROUTER_API_KEY",
  AIHUBMIX_API_KEY:     "AIHUBMIX_API_KEY",
  // Fix #3: AIHubMix fallback keys — Gemini key dan free-tier keys untuk rotasi saat rate-limit
  AIHUBMIX_GEMINI_KEY:  "AIHUBMIX_GEMINI_KEY",
  AIHUBMIX_FREE_KEY_1:  "AIHUBMIX_FREE_KEY_1",
  AIHUBMIX_FREE_KEY_2:  "AIHUBMIX_FREE_KEY_2",
  AIHUBMIX_FREE_KEY_3:  "AIHUBMIX_FREE_KEY_3",
  // AI Preferences
  PREFERRED_AI_PROVIDER: "PREFERRED_AI_PROVIDER",
  PREFERRED_MODEL:       "PREFERRED_MODEL",
  // Supabase (BYOD)
  SUPABASE_URL:       "SUPABASE_URL",
  SUPABASE_ANON_KEY:  "SUPABASE_ANON_KEY",
} as const;

export type EnvVarKey = typeof ENV_VAR_KEYS[keyof typeof ENV_VAR_KEYS];

export type EnvVarCategory = "ai_provider" | "supabase" | "other";

export interface EnvVar {
  id: string;
  user_id: string;
  key: string;
  encrypted_value: string;   // raw — hanya ada di server
  category: EnvVarCategory;
  description: string | null;
  is_active: boolean;
  last_used_at: string | null;
  created_at: string;
  updated_at: string;
}

// Versi aman untuk frontend (tanpa encrypted_value)
export interface EnvVarPublic {
  id: string;
  key: string;
  category: EnvVarCategory;
  description: string | null;
  is_active: boolean;
  masked_value: string;      // misal: "••••••••3AED"
  last_used_at: string | null;
  updated_at: string;
}

// Metadata per key untuk keperluan UI
export interface EnvVarMeta {
  key: string;
  label: string;
  category: EnvVarCategory;
  placeholder: string;
  description: string;
  isSecret: boolean;
  docUrl?: string;
}

export const ENV_VAR_META: EnvVarMeta[] = [
  {
    key: ENV_VAR_KEYS.OPENAI_API_KEY,
    label: "OpenAI API Key",
    category: "ai_provider",
    placeholder: "sk-proj-xxxxxxxxxxxxxxxx",
    description: "Untuk generate prompt via GPT-4o / GPT-4o-mini",
    isSecret: true,
    docUrl: "https://platform.openai.com/api-keys",
  },
  {
    key: ENV_VAR_KEYS.ANTHROPIC_API_KEY,
    label: "Anthropic API Key",
    category: "ai_provider",
    placeholder: "sk-ant-xxxxxxxxxxxxxxxx",
    description: "Untuk generate prompt via Claude 3.5 / Haiku",
    isSecret: true,
    docUrl: "https://console.anthropic.com/settings/keys",
  },
  {
    key: ENV_VAR_KEYS.GOOGLE_AI_API_KEY,
    label: "Google AI API Key",
    category: "ai_provider",
    placeholder: "AIzaSyxxxxxxxxxxxxxxxx",
    description: "Untuk generate prompt via Gemini 1.5 Pro / Flash",
    isSecret: true,
    docUrl: "https://aistudio.google.com/app/apikey",
  },
  {
    key: ENV_VAR_KEYS.AGENTROUTER_API_KEY,
    label: "AgentRouter API Key",
    category: "ai_provider",
    placeholder: "sk-xxxxxxxxxxxxxxxxxxxxxxxx",
    description: "Gateway AI — akses 30+ provider (OpenAI, Claude, Gemini, dll) via satu key. Daftar di agentrouter.org/console",
    isSecret: true,
    docUrl: "https://agentrouter.org/console/personal",
  },
  {
    key: ENV_VAR_KEYS.AIHUBMIX_API_KEY,
    label: "AIHubMix API Key (Primary)",
    category: "ai_provider",
    placeholder: "sk-xxxxxxxxxxxxxxxxxxxxxxxx",
    description: "Gateway AI — akses GPT, Gemini, Claude, Llama, dan model gratis lainnya via satu key. Daftar di aihubmix.com",
    isSecret: true,
    docUrl: "https://console.aihubmix.com",
  },
  // Fix #3: AIHubMix fallback keys
  {
    key: ENV_VAR_KEYS.AIHUBMIX_GEMINI_KEY,
    label: "AIHubMix Gemini Key (Fallback)",
    category: "ai_provider",
    placeholder: "sk-xxxxxxxxxxxxxxxxxxxxxxxx",
    description: "Key AIHubMix khusus Gemini model. Digunakan sebagai fallback saat primary key rate-limited.",
    isSecret: true,
    docUrl: "https://console.aihubmix.com",
  },
  {
    key: ENV_VAR_KEYS.AIHUBMIX_FREE_KEY_1,
    label: "AIHubMix Free Key 1",
    category: "ai_provider",
    placeholder: "sk-xxxxxxxxxxxxxxxxxxxxxxxx",
    description: "Free-tier AIHubMix key #1 — fallback saat primary dan Gemini key rate-limited.",
    isSecret: true,
  },
  {
    key: ENV_VAR_KEYS.AIHUBMIX_FREE_KEY_2,
    label: "AIHubMix Free Key 2",
    category: "ai_provider",
    placeholder: "sk-xxxxxxxxxxxxxxxxxxxxxxxx",
    description: "Free-tier AIHubMix key #2 — fallback kedua.",
    isSecret: true,
  },
  {
    key: ENV_VAR_KEYS.AIHUBMIX_FREE_KEY_3,
    label: "AIHubMix Free Key 3",
    category: "ai_provider",
    placeholder: "sk-xxxxxxxxxxxxxxxxxxxxxxxx",
    description: "Free-tier AIHubMix key #3 — fallback terakhir sebelum provider lain.",
    isSecret: true,
  },
  {
    key: ENV_VAR_KEYS.PREFERRED_AI_PROVIDER,
    label: "Provider Utama",
    category: "ai_provider",
    placeholder: "auto | openai | anthropic | google | agentrouter",
    description: "Provider yang diprioritaskan. 'auto' = pilih otomatis",
    isSecret: false,
  },
  {
    key: ENV_VAR_KEYS.PREFERRED_MODEL,
    label: "Model Spesifik",
    category: "ai_provider",
    placeholder: "gpt-4o-mini, claude-3-haiku-20240307, gemini-1.5-flash...",
    description: "Kosongkan untuk gunakan model default dari provider",
    isSecret: false,
  },
  {
    key: ENV_VAR_KEYS.SUPABASE_URL,
    label: "Supabase Project URL",
    category: "supabase",
    placeholder: "https://xxxxxxxx.supabase.co",
    description: "URL project Supabase kamu (opsional — BYOD mode)",
    isSecret: false,
  },
  {
    key: ENV_VAR_KEYS.SUPABASE_ANON_KEY,
    label: "Supabase Anon Key",
    category: "supabase",
    placeholder: "eyJhbGciOiJIUzI1NiIsInR5cCI6...",
    description: "Anon/public key dari Supabase project kamu",
    isSecret: true,
    docUrl: "https://supabase.com/dashboard/project/_/settings/api",
  },
];
