#!/bin/bash
# review-report-scaffold.sh - Create review report directory structure
# Usage: ./review-report-scaffold.sh <report-name> [project-name]

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPORT_NAME="${1:-codebase-review}"
PROJECT_NAME="${2:-$(basename $(pwd))}"
REPORT_DATE=$(date +%Y-%m-%d)
REPORT_DIR="docs/reports/${REPORT_NAME}-${REPORT_DATE}"

if [[ -d "$REPORT_DIR" ]]; then
    echo "Warning: $REPORT_DIR already exists"
    read -p "Overwrite? [y/N] " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

mkdir -p "${REPORT_DIR}"

# Create executive summary
cat > "${REPORT_DIR}/00-executive-summary.md" << EOF
# ${PROJECT_NAME} - ${REPORT_NAME}: Executive Summary

**Date:** ${REPORT_DATE}
**Scope:** Full codebase review
**Method:** Parallel review agents covering security, code quality, and QA/test coverage

---

## At a Glance

| Dimension | Findings | Critical | High | Medium | Low |
|-----------|----------|----------|------|--------|-----|
| Security - API | | | | | |
| Security - Web | | | | | |
| Security - Orchestrator | | | | | |
| Code Quality - API | | | | | |
| Code Quality - Web | | | | | |
| Code Quality - Orchestrator | | | | | |
| **Totals** | | | | | |

---

## Top 10 Most Urgent Findings

<!-- Populated by review agents -->

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

---

## Summary by Workspace

### apps/api
- **Security:**
- **Code Quality:**
- **Test Grade:**

### apps/web
- **Security:**
- **Code Quality:**
- **Test Grade:**

### apps/orchestrator
- **Security:**
- **Code Quality:**
- **Test Grade:**

---

## Next Steps

1. Create phase issues for critical/high findings
2. Bootstrap tasks.md from findings
3. Track remediation progress

EOF

# Create security review
cat > "${REPORT_DIR}/01-security-review.md" << EOF
# ${PROJECT_NAME} - Security Review

**Date:** ${REPORT_DATE}
**Scope:** Security vulnerabilities, authentication, authorization, input validation

---

## Methodology

- Static code analysis
- Dependency vulnerability scan
- Authentication/authorization review
- Input validation audit
- Secret detection

---

## Findings

### Critical Severity

<!--
Format:
#### SEC-{AREA}-{N}: {Title}

| Aspect | Detail |
|--------|--------|
| **Location** | \`path/to/file.ts:123\` |
| **Risk** | Description of security risk |
| **Impact** | What could happen if exploited |
| **Remediation** | Steps to fix |
| **Effort** | Estimate (e.g., 8K tokens) |
-->

### High Severity

### Medium Severity

### Low Severity

---

## Summary

| Severity | Count |
|----------|-------|
| Critical | |
| High | |
| Medium | |
| Low | |

EOF

# Create code quality review
cat > "${REPORT_DIR}/02-code-quality-review.md" << EOF
# ${PROJECT_NAME} - Code Quality Review

**Date:** ${REPORT_DATE}
**Scope:** Code patterns, error handling, performance, maintainability

---

## Methodology

- Pattern consistency analysis
- Error handling audit
- Performance anti-pattern detection
- Type safety review
- Memory leak detection

---

## Findings

### Critical Severity

<!--
Format:
#### CQ-{AREA}-{N}: {Title}

| Aspect | Detail |
|--------|--------|
| **Location** | \`path/to/file.ts:123\` |
| **Issue** | Description of the problem |
| **Impact** | Effect on system behavior |
| **Remediation** | Steps to fix |
| **Effort** | Estimate (e.g., 10K tokens) |
-->

### High Severity

### Medium Severity

### Low Severity

---

## Summary

| Severity | Count |
|----------|-------|
| Critical | |
| High | |
| Medium | |
| Low | |

EOF

# Create QA/test coverage review
cat > "${REPORT_DIR}/03-qa-test-coverage.md" << EOF
# ${PROJECT_NAME} - QA & Test Coverage Review

**Date:** ${REPORT_DATE}
**Scope:** Test coverage gaps, testing patterns, quality assurance

---

## Coverage Summary

| Workspace | Statements | Branches | Functions | Lines | Grade |
|-----------|------------|----------|-----------|-------|-------|
| apps/api | | | | | |
| apps/web | | | | | |
| apps/orchestrator | | | | | |

---

## Critical Coverage Gaps

<!--
Format:
#### TEST-{AREA}-{N}: {Title}

| Aspect | Detail |
|--------|--------|
| **Location** | \`path/to/file.ts\` |
| **Gap** | What is not tested |
| **Risk** | Why this matters |
| **Recommended Tests** | Specific tests to add |
| **Effort** | Estimate (e.g., 15K tokens) |
-->

---

## Testing Pattern Issues

### Missing Test Types

### Flaky Tests

### Test Organization

---

## Recommendations

1.
2.
3.

EOF

echo "Created: ${REPORT_DIR}/"
echo "  - 00-executive-summary.md"
echo "  - 01-security-review.md"
echo "  - 02-code-quality-review.md"
echo "  - 03-qa-test-coverage.md"
echo ""
echo "Next: Run review agents to populate findings"
