#!/bin/bash
# weekly_report.sh - Generate weekly trend comparison report
# Usage: weekly_report.sh <week_number>

set -euo pipefail

WEEK="${1:-1}"
METRICS_DIR="/Users/sac/ggen/.metrics"
OUTPUT_DIR="/Users/sac/ggen/docs/metrics"
DATE=$(date +%Y-%m-%d)
OUTPUT_FILE="$OUTPUT_DIR/weekly-report-week-$WEEK.md"

mkdir -p "$OUTPUT_DIR"

echo "📊 Generating Week $WEEK Report..."

# Get baseline metrics
BASELINE="$METRICS_DIR/baseline-week-0.json"
CURRENT="$METRICS_DIR/latest.json"

if [[ ! -f "$BASELINE" ]]; then
    echo "Error: Baseline not found. Run: ./scripts/metrics/baseline_snapshot.sh" >&2
    exit 1
fi

if [[ ! -f "$CURRENT" ]]; then
    echo "Error: Current metrics not found. Run: cargo make metrics-collect" >&2
    exit 1
fi

# Parse metrics with jq fallback to grep
parse_metric() {
    local file="$1"
    local path="$2"
    if command -v jq &> /dev/null; then
        jq -r "$path" "$file" 2>/dev/null || echo "0"
    else
        # Fallback parsing (simplified)
        echo "0"
    fi
}

# Baseline values (Week 0)
BASELINE_ERRORS=158
BASELINE_PASS_RATE=15
BASELINE_BUILD_TIME=30
BASELINE_TEMPLATE_ACCESS=5.04
BASELINE_WASTE=8.4
BASELINE_QUALITY=7.2
BASELINE_VELOCITY=2
BASELINE_COST=634.62

# Current values
CURRENT_ERRORS=$(parse_metric "$CURRENT" '.metrics.compiler_errors.total_errors')
CURRENT_PASS_RATE=$(parse_metric "$CURRENT" '.metrics.test_pass_rate.overall_percentage')
CURRENT_BUILD_TIME=$(parse_metric "$CURRENT" '.metrics.build_time.first_build_seconds')
CURRENT_TEMPLATE_ACCESS=$(parse_metric "$CURRENT" '.metrics.template_accessibility.accessibility_percentage')
CURRENT_WASTE=$(parse_metric "$CURRENT" '.metrics.waste_score.overall_waste_score')
CURRENT_QUALITY=$(parse_metric "$CURRENT" '.metrics.code_quality.quality_score')

# Calculate improvements
calc_improvement() {
    local baseline="$1"
    local current="$2"
    local inverse="${3:-false}"  # For metrics where lower is better

    if [[ "$inverse" == "true" ]]; then
        awk "BEGIN {printf \"%.2f\", (($baseline - $current) / $baseline) * 100}"
    else
        awk "BEGIN {printf \"%.2f\", (($current - $baseline) / $baseline) * 100}"
    fi
}

ERRORS_IMPROVEMENT=$(calc_improvement "$BASELINE_ERRORS" "$CURRENT_ERRORS" "true")
PASS_RATE_IMPROVEMENT=$(calc_improvement "$BASELINE_PASS_RATE" "$CURRENT_PASS_RATE" "false")
BUILD_TIME_IMPROVEMENT=$(calc_improvement "$BASELINE_BUILD_TIME" "$CURRENT_BUILD_TIME" "true")
TEMPLATE_IMPROVEMENT=$(calc_improvement "$BASELINE_TEMPLATE_ACCESS" "$CURRENT_TEMPLATE_ACCESS" "false")
WASTE_IMPROVEMENT=$(calc_improvement "$BASELINE_WASTE" "$CURRENT_WASTE" "true")
QUALITY_IMPROVEMENT=$(calc_improvement "$BASELINE_QUALITY" "$CURRENT_QUALITY" "false")

# Generate report
cat > "$OUTPUT_FILE" <<EOF
# ggen Kaizen Metrics - Week $WEEK Report
**Generated**: $DATE
**Reporting Period**: Week 0 → Week $WEEK

## Executive Summary

Week $WEEK marks significant progress in our 76% waste reduction journey. This report compares current metrics against the Week 0 baseline to demonstrate measurable improvements across all 8 critical categories.

---

## Critical Metrics Comparison

### 1. Compiler Errors (Target: 0)
| Metric | Week 0 | Week $WEEK | Improvement |
|--------|--------|-----------|-------------|
| **Total Errors** | 158 | $CURRENT_ERRORS | **${ERRORS_IMPROVEMENT}%** |
| **Status** | 🔴 CRITICAL | $([ "$CURRENT_ERRORS" -eq 0 ] && echo "✅ RESOLVED" || echo "⚠️ IN PROGRESS") | |

**Impact**: $([ "$CURRENT_ERRORS" -eq 0 ] && echo "All compiler errors eliminated - development unblocked" || echo "$CURRENT_ERRORS errors remaining - continued focus required")

---

### 2. Test Pass Rate (Target: 100%)
| Metric | Week 0 | Week $WEEK | Improvement |
|--------|--------|-----------|-------------|
| **Pass Rate** | 15% | ${CURRENT_PASS_RATE}% | **+${PASS_RATE_IMPROVEMENT}%** |
| **Failing Tests** | 17 | $(awk "BEGIN {printf \"%.0f\", ((100 - $CURRENT_PASS_RATE) / 100) * 20}") | |

**Impact**: Test reliability improving - $(awk "BEGIN {printf \"%.0f\", $CURRENT_PASS_RATE}")% of tests now passing consistently

---

### 3. Build Time (Target: <15s for 47% reduction)
| Metric | Week 0 | Week $WEEK | Improvement |
|--------|--------|-----------|-------------|
| **First Build** | 30s | ${CURRENT_BUILD_TIME}s | **${BUILD_TIME_IMPROVEMENT}%** |
| **Target Met** | ❌ No | $(awk "BEGIN {print ($CURRENT_BUILD_TIME <= 15) ? \"✅ Yes\" : \"⚠️ No\"}") | |

**Impact**: Build time $(awk "BEGIN {printf \"%.1f\", (30 - $CURRENT_BUILD_TIME)}")s faster - developer velocity increased

---

### 4. Template Accessibility (Target: 100%)
| Metric | Week 0 | Week $WEEK | Improvement |
|--------|--------|-----------|-------------|
| **Accessibility** | 5.04% (13/258) | ${CURRENT_TEMPLATE_ACCESS}% | **+${TEMPLATE_IMPROVEMENT}%** |
| **Templates Accessible** | 13 | $(awk "BEGIN {printf \"%.0f\", ($CURRENT_TEMPLATE_ACCESS / 100) * 258}") | |

**Impact**: $(awk "BEGIN {printf \"%.0f\", (($CURRENT_TEMPLATE_ACCESS - 5.04) / 100) * 258}") additional templates now accessible via CLI

---

### 5. Waste Score (Target: 2.0)
| Metric | Week 0 | Week $WEEK | Improvement |
|--------|--------|-----------|-------------|
| **Overall Waste** | 8.4 | $CURRENT_WASTE | **${WASTE_IMPROVEMENT}%** |
| **Target Met** | ❌ No | $(awk "BEGIN {print ($CURRENT_WASTE <= 2.5) ? \"✅ Yes\" : \"⚠️ In Progress\"}") | |

**Impact**: Waste reduction of $(awk "BEGIN {printf \"%.1f\", 8.4 - $CURRENT_WASTE}") points - efficiency improving

---

### 6. Code Quality (Target: 9.5)
| Metric | Week 0 | Week $WEEK | Improvement |
|--------|--------|-----------|-------------|
| **Quality Score** | 7.2 | $CURRENT_QUALITY | **+${QUALITY_IMPROVEMENT}%** |
| **Target Gap** | 2.3 points | $(awk "BEGIN {printf \"%.1f\", 9.5 - $CURRENT_QUALITY}") points | |

**Impact**: Code quality improving - $(awk "BEGIN {printf \"%.1f\", (($CURRENT_QUALITY - 7.2) / 2.3) * 100}")% of quality gap closed

---

## Overall Progress Toward 76% Waste Reduction

### Achievements This Week

EOF

# Add achievements
if [[ "$CURRENT_ERRORS" -eq 0 ]]; then
    echo "- ✅ **Compiler errors eliminated**: 158 → 0 (100% improvement)" >> "$OUTPUT_FILE"
fi

if awk "BEGIN {exit !($CURRENT_BUILD_TIME <= 15)}"; then
    echo "- ✅ **Build time target met**: ${CURRENT_BUILD_TIME}s (target: <15s)" >> "$OUTPUT_FILE"
fi

if awk "BEGIN {exit !($CURRENT_PASS_RATE >= 85)}"; then
    echo "- ✅ **Test pass rate target approaching**: ${CURRENT_PASS_RATE}% (target: 100%)" >> "$OUTPUT_FILE"
fi

cat >> "$OUTPUT_FILE" <<EOF

### Remaining Gaps

EOF

# Add remaining gaps
if [[ "$CURRENT_ERRORS" -gt 0 ]]; then
    echo "- ⚠️ **Compiler errors**: $CURRENT_ERRORS remaining" >> "$OUTPUT_FILE"
fi

if awk "BEGIN {exit !($CURRENT_PASS_RATE < 100)}"; then
    REMAINING_TESTS=$(awk "BEGIN {printf \"%.0f\", ((100 - $CURRENT_PASS_RATE) / 100) * 20}")
    echo "- ⚠️ **Test failures**: $REMAINING_TESTS tests still failing" >> "$OUTPUT_FILE"
fi

if awk "BEGIN {exit !($CURRENT_TEMPLATE_ACCESS < 100)}"; then
    REMAINING_TEMPLATES=$(awk "BEGIN {printf \"%.0f\", ((100 - $CURRENT_TEMPLATE_ACCESS) / 100) * 258}")
    echo "- ⚠️ **Template accessibility**: $REMAINING_TEMPLATES templates still inaccessible" >> "$OUTPUT_FILE"
fi

if awk "BEGIN {exit !($CURRENT_WASTE > 2.5)}"; then
    echo "- ⚠️ **Waste score**: Current $CURRENT_WASTE (target: 2.0)" >> "$OUTPUT_FILE"
fi

cat >> "$OUTPUT_FILE" <<EOF

---

## Andon Signals (Current Status)

EOF

# Check for current Andon signals
if [[ "$CURRENT_ERRORS" -gt 0 ]]; then
    cat >> "$OUTPUT_FILE" <<EOF
### 🔴 CRITICAL
- **$CURRENT_ERRORS compiler errors** - STOP THE LINE
  - Action Required: Fix all compiler errors immediately

EOF
fi

if awk "BEGIN {exit !($CURRENT_PASS_RATE < 100)}"; then
    cat >> "$OUTPUT_FILE" <<EOF
### 🟡 HIGH
- **Test failures detected** - ${CURRENT_PASS_RATE}% pass rate
  - Action Required: Fix failing tests before new feature work

EOF
fi

if [[ "$CURRENT_ERRORS" -eq 0 ]] && awk "BEGIN {exit !($CURRENT_PASS_RATE == 100)}"; then
    echo "### ✅ All Clear - No Active Andon Signals" >> "$OUTPUT_FILE"
    echo "" >> "$OUTPUT_FILE"
fi

cat >> "$OUTPUT_FILE" <<EOF

---

## Week $WEEK Action Items

### High Priority (This Week)
1. $([ "$CURRENT_ERRORS" -gt 0 ] && echo "Fix remaining $CURRENT_ERRORS compiler errors" || echo "✅ Compiler errors resolved - maintain clean compilation")
2. $(awk "BEGIN {exit !($CURRENT_PASS_RATE < 100)}" && echo "Fix failing tests to reach 100% pass rate" || echo "✅ All tests passing - add new test coverage")
3. Improve template accessibility from ${CURRENT_TEMPLATE_ACCESS}% → 100%
4. Reduce waste score from $CURRENT_WASTE → 2.0

### Medium Priority (Next Week)
1. Improve code quality from $CURRENT_QUALITY → 9.5
2. Increase test coverage to 80%+
3. Document all improvements in knowledge base
4. Train team on new patterns and tools

### Low Priority (Future Sprints)
1. Optimize build pipeline further
2. Enhance CI/CD automation
3. Create developer productivity dashboards
4. Establish continuous improvement rituals

---

## Cost of Waste Analysis

| Metric | Week 0 | Week $WEEK | Improvement |
|--------|--------|-----------|-------------|
| **Weekly Waste Cost** | \$634.62 | \$$(awk "BEGIN {printf \"%.2f\", $BASELINE_COST * (1 - ($WASTE_IMPROVEMENT / 100))}") | **${WASTE_IMPROVEMENT}%** |
| **Annual Projection** | \$33,000 | \$$(awk "BEGIN {printf \"%.0f\", (33000 * (1 - ($WASTE_IMPROVEMENT / 100)))}") | **-\$$(awk "BEGIN {printf \"%.0f\", 33000 * ($WASTE_IMPROVEMENT / 100)}")** |

**Financial Impact**: Week $WEEK improvements are on track to save approximately **\$$(awk "BEGIN {printf \"%.0f\", 33000 * ($WASTE_IMPROVEMENT / 100)}")** annually through waste reduction.

---

## Trend Visualization

\`\`\`
Compiler Errors:   [158] ─────────────────────→ [$CURRENT_ERRORS]   $([ "$CURRENT_ERRORS" -eq 0 ] && echo "✅" || echo "⚠️")
Test Pass Rate:    [ 15%] ─────────────────────→ [${CURRENT_PASS_RATE}%] $(awk "BEGIN {exit !($CURRENT_PASS_RATE >= 85)}" && echo "✅" || echo "⚠️")
Build Time:        [ 30s] ─────────────────────→ [${CURRENT_BUILD_TIME}s]  $(awk "BEGIN {exit !($CURRENT_BUILD_TIME <= 15)}" && echo "✅" || echo "⚠️")
Template Access:   [  5%] ─────────────────────→ [${CURRENT_TEMPLATE_ACCESS}%] ⚠️
Waste Score:       [ 8.4] ─────────────────────→ [$CURRENT_WASTE]   ⚠️
Code Quality:      [ 7.2] ─────────────────────→ [$CURRENT_QUALITY]   ⚠️
\`\`\`

---

## Next Steps

1. **Immediate**: Address any CRITICAL/HIGH Andon signals
2. **This Week**: Execute High Priority action items
3. **Continuous**: Run \`cargo make metrics-collect\` daily
4. **Weekly**: Review progress and adjust strategy

---

## Kaizen Principle

> "Today is better than yesterday. Tomorrow will be better than today."

Week $WEEK demonstrates measurable progress across all metrics. We continue the journey toward 76% waste reduction with systematic, data-driven improvements.

**Dashboard**: [View Live Metrics](/docs/metrics/latest.html)
**Raw Data**: [\`.metrics/latest.json\`](/.metrics/latest.json)

---
*Generated by Kaizen Metrics Specialist - Continuous Improvement Tracking*
EOF

echo "✅ Weekly report generated: $OUTPUT_FILE"
echo ""
echo "📊 Key Highlights:"
echo "  - Compiler Errors: $BASELINE_ERRORS → $CURRENT_ERRORS (${ERRORS_IMPROVEMENT}% improvement)"
echo "  - Test Pass Rate: $BASELINE_PASS_RATE% → ${CURRENT_PASS_RATE}% (+${PASS_RATE_IMPROVEMENT}%)"
echo "  - Build Time: ${BASELINE_BUILD_TIME}s → ${CURRENT_BUILD_TIME}s (${BUILD_TIME_IMPROVEMENT}% improvement)"
echo "  - Template Access: ${BASELINE_TEMPLATE_ACCESS}% → ${CURRENT_TEMPLATE_ACCESS}% (+${TEMPLATE_IMPROVEMENT}%)"
echo "  - Waste Score: $BASELINE_WASTE → $CURRENT_WASTE (${WASTE_IMPROVEMENT}% improvement)"
echo "  - Code Quality: $BASELINE_QUALITY → $CURRENT_QUALITY (+${QUALITY_IMPROVEMENT}%)"
