{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "ggen Kaizen Metrics Schema",
  "description": "Tracks 76% waste reduction promise across 8 critical metric categories",
  "type": "object",
  "required": ["week", "date", "metrics", "trend", "andon_signals"],
  "properties": {
    "week": {
      "type": "integer",
      "description": "Sprint week number (0 = baseline, 1-3 = improvement weeks)",
      "minimum": 0,
      "maximum": 52
    },
    "date": {
      "type": "string",
      "format": "date",
      "description": "ISO 8601 date of metric collection (YYYY-MM-DD)"
    },
    "metrics": {
      "type": "object",
      "description": "Current metric values for all 8 categories",
      "required": [
        "build_time",
        "test_pass_rate",
        "compiler_errors",
        "code_quality",
        "template_accessibility",
        "waste_score",
        "velocity",
        "cost_per_hour"
      ],
      "properties": {
        "build_time": {
          "type": "object",
          "description": "Build time metrics (target: 47% reduction)",
          "properties": {
            "first_build_seconds": {
              "type": "number",
              "description": "Time for first cargo build (target: <15s)",
              "minimum": 0
            },
            "incremental_build_seconds": {
              "type": "number",
              "description": "Time for incremental builds (target: <2s)",
              "minimum": 0
            },
            "test_suite_seconds": {
              "type": "number",
              "description": "Time to run full test suite (target: <10s)",
              "minimum": 0
            },
            "improvement_percentage": {
              "type": "number",
              "description": "Percentage improvement from baseline",
              "minimum": 0,
              "maximum": 100
            }
          }
        },
        "test_pass_rate": {
          "type": "object",
          "description": "Test pass rate (target: 85% → 100%)",
          "properties": {
            "unit_tests_percentage": {
              "type": "number",
              "minimum": 0,
              "maximum": 100
            },
            "integration_tests_percentage": {
              "type": "number",
              "minimum": 0,
              "maximum": 100
            },
            "overall_percentage": {
              "type": "number",
              "minimum": 0,
              "maximum": 100
            },
            "total_tests": {
              "type": "integer",
              "minimum": 0
            },
            "passing_tests": {
              "type": "integer",
              "minimum": 0
            },
            "failing_tests": {
              "type": "integer",
              "minimum": 0
            }
          }
        },
        "compiler_errors": {
          "type": "object",
          "description": "Compiler error count (target: 158 → 0)",
          "properties": {
            "total_errors": {
              "type": "integer",
              "minimum": 0
            },
            "total_warnings": {
              "type": "integer",
              "minimum": 0
            },
            "by_type": {
              "type": "object",
              "description": "Error counts grouped by error code (E0061, E0599, etc.)",
              "additionalProperties": {
                "type": "integer"
              }
            },
            "trend": {
              "type": "string",
              "enum": ["improving", "stable", "degrading"]
            }
          }
        },
        "code_quality": {
          "type": "object",
          "description": "Code quality metrics (target: 7.2 → 9.5)",
          "properties": {
            "quality_score": {
              "type": "number",
              "description": "Overall quality score (0-10 scale)",
              "minimum": 0,
              "maximum": 10
            },
            "clippy_warnings": {
              "type": "integer",
              "minimum": 0
            },
            "cyclomatic_complexity_avg": {
              "type": "number",
              "minimum": 0
            },
            "test_coverage_percentage": {
              "type": "number",
              "minimum": 0,
              "maximum": 100
            },
            "documentation_coverage_percentage": {
              "type": "number",
              "minimum": 0,
              "maximum": 100
            }
          }
        },
        "template_accessibility": {
          "type": "object",
          "description": "Template discovery (target: 5% → 100%)",
          "properties": {
            "total_templates": {
              "type": "integer",
              "minimum": 0,
              "description": "Total templates in templates/ directory (258 expected)"
            },
            "discovered_templates": {
              "type": "integer",
              "minimum": 0,
              "description": "Templates discovered by CLI"
            },
            "accessible_templates": {
              "type": "integer",
              "minimum": 0,
              "description": "Templates accessible via CLI commands"
            },
            "accessibility_percentage": {
              "type": "number",
              "minimum": 0,
              "maximum": 100
            }
          }
        },
        "waste_score": {
          "type": "object",
          "description": "Lean waste metrics (target: 8.4 → 2.0)",
          "properties": {
            "overall_waste_score": {
              "type": "number",
              "description": "Composite waste score (0-10 scale, lower is better)",
              "minimum": 0,
              "maximum": 10
            },
            "todo_count": {
              "type": "integer",
              "minimum": 0,
              "description": "TODO comments in code (waste indicator)"
            },
            "unwrap_count": {
              "type": "integer",
              "minimum": 0,
              "description": "unwrap()/expect() calls (technical debt)"
            },
            "dead_code_lines": {
              "type": "integer",
              "minimum": 0,
              "description": "Lines of dead/unused code"
            },
            "overproduction_percentage": {
              "type": "number",
              "minimum": 0,
              "maximum": 100,
              "description": "Percentage of unused templates (overproduction waste)"
            },
            "defect_count": {
              "type": "integer",
              "minimum": 0,
              "description": "Known bugs/defects"
            }
          }
        },
        "velocity": {
          "type": "object",
          "description": "Developer velocity (target: 2 → 5 features/sprint)",
          "properties": {
            "features_per_sprint": {
              "type": "number",
              "minimum": 0
            },
            "average_fix_time_hours": {
              "type": "number",
              "minimum": 0
            },
            "blockers_resolved": {
              "type": "integer",
              "minimum": 0
            },
            "commits_per_week": {
              "type": "integer",
              "minimum": 0
            },
            "pull_requests_merged": {
              "type": "integer",
              "minimum": 0
            }
          }
        },
        "cost_per_hour": {
          "type": "object",
          "description": "Cost of waste (target: $33k → $8k annually)",
          "properties": {
            "developer_time_lost_hours": {
              "type": "number",
              "minimum": 0,
              "description": "Hours lost to blockers per week"
            },
            "rework_hours": {
              "type": "number",
              "minimum": 0,
              "description": "Hours spent on rework per week"
            },
            "incident_response_hours": {
              "type": "number",
              "minimum": 0,
              "description": "Hours responding to incidents per week"
            },
            "hourly_cost_usd": {
              "type": "number",
              "minimum": 0,
              "description": "Cost per developer hour"
            },
            "weekly_waste_cost_usd": {
              "type": "number",
              "minimum": 0
            },
            "annual_waste_cost_usd": {
              "type": "number",
              "minimum": 0
            }
          }
        }
      }
    },
    "trend": {
      "type": "object",
      "description": "Historical trend data (week 0 → 1 → 2 → 3 comparison)",
      "properties": {
        "week_0": {
          "$ref": "#/definitions/week_snapshot"
        },
        "week_1": {
          "$ref": "#/definitions/week_snapshot"
        },
        "week_2": {
          "$ref": "#/definitions/week_snapshot"
        },
        "week_3": {
          "$ref": "#/definitions/week_snapshot"
        }
      }
    },
    "andon_signals": {
      "type": "object",
      "description": "Andon signals (stop-the-line alerts)",
      "properties": {
        "critical": {
          "type": "array",
          "description": "CRITICAL signals requiring immediate action",
          "items": {
            "$ref": "#/definitions/andon_signal"
          }
        },
        "high": {
          "type": "array",
          "description": "HIGH priority signals",
          "items": {
            "$ref": "#/definitions/andon_signal"
          }
        },
        "medium": {
          "type": "array",
          "description": "MEDIUM priority signals",
          "items": {
            "$ref": "#/definitions/andon_signal"
          }
        }
      }
    },
    "improvements": {
      "type": "array",
      "description": "Improvements achieved this week",
      "items": {
        "type": "object",
        "properties": {
          "category": {
            "type": "string",
            "enum": [
              "build_time",
              "test_pass_rate",
              "compiler_errors",
              "code_quality",
              "template_accessibility",
              "waste_score",
              "velocity",
              "cost_per_hour"
            ]
          },
          "description": {
            "type": "string"
          },
          "before": {
            "type": "number"
          },
          "after": {
            "type": "number"
          },
          "improvement_percentage": {
            "type": "number"
          }
        }
      }
    },
    "remaining_gaps": {
      "type": "array",
      "description": "Remaining gaps to achieve 76% waste reduction target",
      "items": {
        "type": "object",
        "properties": {
          "category": {
            "type": "string"
          },
          "current_value": {
            "type": "number"
          },
          "target_value": {
            "type": "number"
          },
          "gap_percentage": {
            "type": "number"
          },
          "action_items": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "week_snapshot": {
      "type": "object",
      "description": "Snapshot of key metrics for a given week",
      "properties": {
        "compiler_errors": {
          "type": "integer",
          "minimum": 0
        },
        "test_pass_rate": {
          "type": "number",
          "minimum": 0,
          "maximum": 100
        },
        "build_time_seconds": {
          "type": "number",
          "minimum": 0
        },
        "template_accessibility": {
          "type": "number",
          "minimum": 0,
          "maximum": 100
        },
        "waste_score": {
          "type": "number",
          "minimum": 0,
          "maximum": 10
        },
        "velocity": {
          "type": "number",
          "minimum": 0
        },
        "cost_usd": {
          "type": "number",
          "minimum": 0
        }
      }
    },
    "andon_signal": {
      "type": "object",
      "description": "Andon signal alert",
      "required": ["type", "message", "threshold", "current_value", "triggered_at"],
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "compiler_error",
            "test_failure",
            "performance_regression",
            "quality_degradation",
            "coverage_drop"
          ]
        },
        "message": {
          "type": "string"
        },
        "threshold": {
          "type": "number",
          "description": "Threshold value that triggered alert"
        },
        "current_value": {
          "type": "number",
          "description": "Current metric value"
        },
        "triggered_at": {
          "type": "string",
          "format": "date-time"
        },
        "action_required": {
          "type": "string",
          "description": "Required action to clear signal"
        }
      }
    }
  }
}
