import { DEFAULT_TUNING, REQUIRED_TUNING_KEYS } from "@/lib/features/chatbot/constants/constants";
import type { AgentFormValues } from "./agent.types";

export const getPrecision = (n: number) => (String(n).includes(".") ? String(n).split(".")[1].length : 0);

export const agentInitialValues = (overrides?: Partial<AgentFormValues>): AgentFormValues => ({
  agent_name: "",
  prompt_id: "",
  embed_model: "",
  language_model: "",
  is_active: true,
  csv_urls: [],
  embed_dimension: "",
  category_key: "",
  tuning: { ...DEFAULT_TUNING },
  ...overrides,
});

export const agentValidate = {
  agent_name: (value: string) => (value.trim() ? null : "Agent name is required."),
  prompt_id: (value: string) => (value ? null : "Associated prompt is required."),
  embed_model: (value: string) => (value.trim() ? null : "Embedding model is required."),
  language_model: (value: string) => (value.trim() ? null : "Language model is required."),
  embed_dimension: (value: any) => (Number(value) > 0 ? null : "Embedding dimension must be > 0."),
  category_key: (value: string) => (value.trim() ? null : "Category key is required."),
  is_active: (_value: boolean) => null,
  csv_urls: (_value: string[]) => null,
  tuning: {
    ...Object.fromEntries(
      REQUIRED_TUNING_KEYS.map((key) => [
        key,
        (value: any) => {
          const numericValue = Number(value);
          if (value === undefined || value === null || value === "" || isNaN(numericValue)) return "Required";
          return null;
        },
      ]),
    ),
  },
};
