{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "mantara.schema.v1",
  "title": "Mantara Schema Definition v1",
  "description": "JSON Schema defining the exact structure an LLM must produce when generating a Mantara-standard PostgreSQL schema. This intermediate representation is validated before SQL generation.",
  "type": "object",
  "required": ["$schema", "system_name", "schema_name", "description", "menus"],
  "additionalProperties": false,
  "properties": {
    "$schema": {
      "type": "string",
      "const": "mantara.schema.v1",
      "description": "Schema identifier. Must always be 'mantara.schema.v1'."
    },
    "system_name": {
      "type": "string",
      "minLength": 1,
      "description": "Human-readable name of the system (e.g., 'Irrigation Management System')."
    },
    "schema_name": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]*$",
      "maxLength": 30,
      "description": "PostgreSQL schema name. Must be lowercase snake_case abbreviation (e.g., 'ims', 'hms')."
    },
    "description": {
      "type": "string",
      "description": "One-line description of the system's purpose."
    },
    "menus": {
      "type": "array",
      "minItems": 1,
      "description": "Top-level navigation menus. Each menu groups related submenus.",
      "items": {
        "$ref": "#/$defs/menu"
      }
    },
    "enum_types": {
      "type": "array",
      "description": "PostgreSQL ENUM type definitions for controlled-value columns.",
      "items": {
        "$ref": "#/$defs/enum_type"
      }
    },
    "assumptions": {
      "type": "array",
      "description": "Domain assumptions made when the input was ambiguous.",
      "items": {
        "type": "string"
      }
    },
    "open_questions": {
      "type": "array",
      "description": "Questions that a real implementation would need answered.",
      "items": {
        "type": "string"
      }
    }
  },
  "$defs": {
    "menu": {
      "type": "object",
      "required": ["menu_id", "menu_name", "sequence_number", "description", "submenus"],
      "additionalProperties": false,
      "properties": {
        "menu_id": {
          "type": "integer",
          "minimum": 1,
          "description": "Unique menu identifier. Sequential starting at 1."
        },
        "menu_name": {
          "type": "string",
          "minLength": 1,
          "description": "Display name of the menu (e.g., 'Dashboard', 'Irrigation Planning')."
        },
        "sequence_number": {
          "type": "integer",
          "minimum": 1,
          "description": "Display order of this menu in the navigation."
        },
        "description": {
          "type": "string",
          "description": "What this menu module encompasses."
        },
        "submenus": {
          "type": "array",
          "minItems": 1,
          "description": "Functional screens/windows under this menu.",
          "items": {
            "$ref": "#/$defs/submenu"
          }
        }
      }
    },
    "submenu": {
      "type": "object",
      "required": ["submenu_id", "submenu_name", "sequence_number", "description"],
      "additionalProperties": false,
      "properties": {
        "submenu_id": {
          "type": "integer",
          "minimum": 101,
          "description": "Unique submenu identifier. Convention: menu_id * 100 + sequence_within_menu (e.g., Menu 1 → 101, 102; Menu 2 → 201, 202)."
        },
        "submenu_name": {
          "type": "string",
          "minLength": 1,
          "description": "Display name of the submenu (e.g., 'Farm Overview', 'Crop Irrigation Scheduling')."
        },
        "sequence_number": {
          "type": "integer",
          "minimum": 1,
          "description": "Position of this submenu within its parent menu (1, 2, 3, ...)."
        },
        "description": {
          "type": "string",
          "description": "What this screen/window does."
        },
        "tables": {
          "type": "array",
          "description": "Business tables that store data for this submenu. May be empty for UI-only screens.",
          "items": {
            "$ref": "#/$defs/table"
          }
        }
      }
    },
    "table": {
      "type": "object",
      "required": ["table_name", "comment", "columns"],
      "additionalProperties": false,
      "properties": {
        "table_name": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]*$",
          "description": "Snake_case table name (without schema prefix — the schema prefix is added automatically)."
        },
        "comment": {
          "type": "string",
          "minLength": 10,
          "description": "COMMENT ON TABLE text. Must be descriptive (min 10 chars)."
        },
        "columns": {
          "type": "array",
          "minItems": 2,
          "description": "Column definitions. First column MUST be 'id SERIAL PRIMARY KEY'. Second MUST be 'submenu_id' with appropriate DEFAULT and FK.",
          "items": {
            "$ref": "#/$defs/column"
          }
        },
        "foreign_keys": {
          "type": "array",
          "description": "Foreign key relationships to other tables (beyond the mandatory submenu_id FK).",
          "items": {
            "$ref": "#/$defs/foreign_key"
          }
        },
        "indexes": {
          "type": "array",
          "description": "Optional additional indexes for performance.",
          "items": {
            "$ref": "#/$defs/index"
          }
        }
      }
    },
    "column": {
      "type": "object",
      "required": ["name", "type"],
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]*$",
          "description": "Snake_case column name."
        },
        "type": {
          "type": "string",
          "description": "PostgreSQL data type (e.g., 'SERIAL', 'INT', 'VARCHAR(255)', 'TEXT', 'FLOAT', 'DATE', 'TIMESTAMP', 'BOOLEAN') or a schema-qualified ENUM type name."
        },
        "constraints": {
          "type": "string",
          "description": "Inline constraints (e.g., 'PRIMARY KEY', 'NOT NULL', 'UNIQUE', 'DEFAULT value', 'REFERENCES schema.table(col)')."
        },
        "comment": {
          "type": "string",
          "description": "Optional inline comment explaining this column."
        }
      }
    },
    "foreign_key": {
      "type": "object",
      "required": ["column", "references"],
      "additionalProperties": false,
      "properties": {
        "column": {
          "type": "string",
          "description": "The column in this table that holds the foreign key."
        },
        "references": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]*\\.[a-z][a-z0-9_]*\\([a-z][a-z0-9_]*\\)$",
          "description": "Target in format 'schema.table(column)' (e.g., 'ims.farm(id)')."
        },
        "on_delete": {
          "type": "string",
          "enum": ["CASCADE", "SET NULL", "SET DEFAULT", "RESTRICT", "NO ACTION"],
          "description": "ON DELETE behavior. Defaults to NO ACTION if omitted."
        },
        "on_update": {
          "type": "string",
          "enum": ["CASCADE", "SET NULL", "SET DEFAULT", "RESTRICT", "NO ACTION"],
          "description": "ON UPDATE behavior. Defaults to NO ACTION if omitted."
        }
      }
    },
    "enum_type": {
      "type": "object",
      "required": ["type_name", "values"],
      "additionalProperties": false,
      "properties": {
        "type_name": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]*\\.[a-z][a-z0-9_]*_enum$",
          "description": "Schema-qualified ENUM type name (e.g., 'ims.user_role_enum'). Must end with '_enum'."
        },
        "values": {
          "type": "array",
          "minItems": 2,
          "items": {
            "type": "string",
            "pattern": "^[a-z][a-z0-9_]*$"
          },
          "description": "Allowed values. Must be lowercase snake_case."
        },
        "description": {
          "type": "string",
          "description": "Optional description of what this ENUM represents."
        }
      }
    },
    "index": {
      "type": "object",
      "required": ["name", "columns"],
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string",
          "description": "Index name (e.g., 'idx_sensor_deployment_farm')."
        },
        "columns": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string"
          },
          "description": "Columns included in the index."
        },
        "unique": {
          "type": "boolean",
          "default": false,
          "description": "Whether this is a unique index."
        }
      }
    }
  }
}
