{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://atomizer.io/schemas/optimization_config.json",
  "title": "Atomizer Optimization Configuration",
  "description": "Schema for Atomizer optimization study configuration files",
  "type": "object",
  "required": ["study_name", "description", "optimization_settings", "design_variables", "objectives", "simulation"],

  "properties": {
    "study_name": {
      "type": "string",
      "description": "Unique identifier for this optimization study",
      "pattern": "^[a-z0-9_]+$",
      "minLength": 3,
      "maxLength": 100
    },

    "description": {
      "type": "string",
      "description": "Brief description of the optimization problem",
      "minLength": 10,
      "maxLength": 500
    },

    "engineering_context": {
      "type": "string",
      "description": "Real-world engineering scenario and requirements (optional but recommended)",
      "minLength": 20
    },

    "optimization_settings": {
      "type": "object",
      "required": ["protocol", "n_trials"],
      "properties": {
        "protocol": {
          "type": "string",
          "description": "Optimization protocol to use",
          "enum": [
            "protocol_10_single_objective",
            "protocol_11_multi_objective",
            "legacy"
          ]
        },
        "n_trials": {
          "type": "integer",
          "description": "Number of optimization trials to run",
          "minimum": 1,
          "maximum": 10000
        },
        "sampler": {
          "type": "string",
          "description": "Optuna sampler algorithm",
          "enum": ["TPESampler", "NSGAIISampler", "CmaEsSampler", "RandomSampler"]
        },
        "pruner": {
          "type": ["string", "null"],
          "description": "Optuna pruner (null for no pruning)",
          "enum": ["MedianPruner", "HyperbandPruner", "SuccessiveHalvingPruner", null]
        },
        "timeout_per_trial": {
          "type": "integer",
          "description": "Maximum time per trial in seconds",
          "minimum": 60,
          "maximum": 7200
        }
      }
    },

    "design_variables": {
      "type": "array",
      "description": "List of design variables (NX expressions) to optimize",
      "minItems": 1,
      "maxItems": 50,
      "items": {
        "type": "object",
        "required": ["parameter", "bounds", "description"],
        "properties": {
          "parameter": {
            "type": "string",
            "description": "NX expression name",
            "pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$"
          },
          "bounds": {
            "type": "array",
            "description": "[min, max] bounds for this parameter",
            "minItems": 2,
            "maxItems": 2,
            "items": {
              "type": "number"
            }
          },
          "description": {
            "type": "string",
            "description": "What this parameter controls"
          }
        }
      }
    },

    "objectives": {
      "type": "array",
      "description": "Optimization objectives (1 for single-objective, 2-3 for multi-objective)",
      "minItems": 1,
      "maxItems": 3,
      "items": {
        "type": "object",
        "required": ["name", "goal", "weight", "description", "extraction"],
        "properties": {
          "name": {
            "type": "string",
            "description": "Objective name (e.g., 'mass', 'stiffness', 'frequency')"
          },
          "goal": {
            "type": "string",
            "description": "Optimization direction",
            "enum": ["minimize", "maximize"]
          },
          "weight": {
            "type": "number",
            "description": "Relative importance weight",
            "minimum": 0,
            "maximum": 100
          },
          "description": {
            "type": "string",
            "description": "What this objective measures"
          },
          "target": {
            "type": ["number", "null"],
            "description": "Target value (optional)"
          },
          "extraction": {
            "$ref": "#/definitions/extraction_spec"
          }
        }
      }
    },

    "constraints": {
      "type": "array",
      "description": "Constraints that must be satisfied",
      "items": {
        "type": "object",
        "required": ["name", "type", "threshold", "description", "extraction"],
        "properties": {
          "name": {
            "type": "string",
            "description": "Constraint name"
          },
          "type": {
            "type": "string",
            "description": "Constraint type",
            "enum": ["less_than", "greater_than", "equal_to"]
          },
          "threshold": {
            "type": "number",
            "description": "Threshold value for constraint"
          },
          "description": {
            "type": "string",
            "description": "Engineering justification for this constraint"
          },
          "extraction": {
            "$ref": "#/definitions/extraction_spec"
          }
        }
      }
    },

    "simulation": {
      "type": "object",
      "required": ["model_file", "sim_file", "fem_file", "solver"],
      "properties": {
        "model_file": {
          "type": "string",
          "description": "NX Part file name (e.g., 'Beam.prt')",
          "pattern": "\\.(prt|PRT)$"
        },
        "sim_file": {
          "type": "string",
          "description": "NX Simulation file name (e.g., 'Beam_sim1.sim')",
          "pattern": "\\.(sim|SIM)$"
        },
        "fem_file": {
          "type": "string",
          "description": "FEM mesh file name (e.g., 'Beam_fem1.fem')",
          "pattern": "\\.(fem|FEM)$"
        },
        "solver": {
          "type": "string",
          "description": "FEA solver type",
          "enum": ["nastran", "NX_Nastran"]
        },
        "analysis_types": {
          "type": "array",
          "description": "Types of analysis required",
          "items": {
            "type": "string",
            "enum": ["static", "modal", "thermal", "buckling"]
          }
        }
      }
    },

    "reporting": {
      "type": "object",
      "description": "Result reporting settings (optional)",
      "properties": {
        "generate_plots": {
          "type": "boolean",
          "default": true
        },
        "save_incremental": {
          "type": "boolean",
          "default": true
        },
        "llm_summary": {
          "type": "boolean",
          "default": false,
          "description": "Generate LLM summaries (experimental, requires API setup)"
        }
      }
    }
  },

  "definitions": {
    "extraction_spec": {
      "type": "object",
      "required": ["action", "domain", "params"],
      "properties": {
        "action": {
          "type": "string",
          "description": "Extractor function to call",
          "enum": [
            "extract_mass",
            "extract_stress",
            "extract_displacement",
            "extract_frequency",
            "extract_stiffness"
          ]
        },
        "domain": {
          "type": "string",
          "description": "Extraction domain (always 'result_extraction' for MVP)",
          "enum": ["result_extraction"]
        },
        "params": {
          "type": "object",
          "description": "Parameters for the extractor function",
          "required": ["result_type"],
          "properties": {
            "result_type": {
              "type": "string",
              "description": "Type of result to extract",
              "enum": ["mass", "stress", "displacement", "frequency", "stiffness"]
            },
            "metric": {
              "type": "string",
              "description": "Specific metric to extract (e.g., 'max', 'total', 'max_von_mises')"
            },
            "mode_number": {
              "type": "integer",
              "description": "Mode number for frequency extraction",
              "minimum": 1
            },
            "subcase": {
              "type": "integer",
              "description": "Subcase number for result extraction"
            },
            "element_type": {
              "type": "string",
              "description": "Element type for stress extraction (e.g., 'CTETRA', 'CHEXA')"
            }
          }
        }
      }
    }
  }
}
