# AI Service Validator

let ai_service_schema = import "../schemas/ai-service.ncl" in
let constraints = import "../constraints/constraints.toml" in

{
  validate_ai_service_config | ai_service_schema.AiServiceConfig -> Array String = fun config =>
    let errors = [] in

    # Server port validation
    let errors = if config.server.port < 1024 || config.server.port > 65535
                 then errors @ ["Server port must be between 1024 and 65535"]
                 else errors in

    # Server workers validation
    let errors = if config.server.workers < constraints.ai_service.workers.min
                 then errors @ ["Workers below minimum (#{constraints.ai_service.workers.min})"]
                 else if config.server.workers > constraints.ai_service.workers.max
                      then errors @ ["Workers above maximum (#{constraints.ai_service.workers.max})"]
                      else errors in

    # RAG integration validation
    let errors = if config.rag.enabled == true
                 then
                   let e = [] in
                   let e = if std.array.length config.rag.rag_service_url == 0
                            then e @ ["RAG service URL cannot be empty when enabled"]
                            else e in
                   let e = if config.rag.timeout < 1000
                            then e @ ["RAG timeout must be at least 1000ms"]
                            else e in
                   errors @ e
                 else errors in

    # MCP integration validation
    let errors = if config.mcp.enabled == true
                 then
                   let e = [] in
                   let e = if std.array.length config.mcp.mcp_service_url == 0
                            then e @ ["MCP service URL cannot be empty when enabled"]
                            else e in
                   let e = if config.mcp.timeout < 1000
                            then e @ ["MCP timeout must be at least 1000ms"]
                            else e in
                   errors @ e
                 else errors in

    # DAG configuration validation
    let errors = if config.dag.max_concurrent_tasks < constraints.ai_service.max_concurrent_tasks.min
                 then errors @ ["Max concurrent tasks below minimum (#{constraints.ai_service.max_concurrent_tasks.min})"]
                 else if config.dag.max_concurrent_tasks > constraints.ai_service.max_concurrent_tasks.max
                      then errors @ ["Max concurrent tasks above maximum (#{constraints.ai_service.max_concurrent_tasks.max})"]
                      else errors in

    let errors = if config.dag.task_timeout < 1000
                 then errors @ ["Task timeout must be at least 1000ms"]
                 else errors in

    let errors = if config.dag.retry_attempts < 0 || config.dag.retry_attempts > 20
                 then errors @ ["Retry attempts must be between 0 and 20"]
                 else errors in

    # At least one integration should be enabled in production
    let errors =
      let rag_enabled = config.rag.enabled in
      let mcp_enabled = config.mcp.enabled in
      if !rag_enabled && !mcp_enabled
      then errors @ ["At least one integration (RAG or MCP) should be enabled"]
      else errors in

    errors,
}
