#!/bin/bash
# generate-docs.sh
# Automated documentation generator for the Diagnostics Framework

DOCS_DIR="./docs"

echo "📘 Generating project documentation..."
mkdir -p "$DOCS_DIR"

###############################################
# README.md
###############################################
cat << 'EOF' > "$DOCS_DIR/README.md"
# 📘 Diagnostics & Performance Framework — README

A modular PHP-based diagnostics framework providing:

- Unified debugging
- SQL profiling
- DB connection abstraction
- Responsive test applications
- Bootstrap 5 + DataTables UI
- Structured error handling
- Environment-based configuration

## 🧩 Architecture Overview

### 1. debug.php — Unified Debugging Layer
Handles all developer-facing diagnostic output.

### 2. perf.php — Performance & Profiling Engine
Provides high-precision timing and SQL profiling.

### 3. db-conn.php — Pure DB Connection Module
Handles environment loading and PDO connection.

### 4. test-sample-app.php — Responsive Diagnostic Test App
A Bootstrap 5 + DataTables test harness demonstrating SQL profiling and debug toggling.

EOF

###############################################
# DEVELOPER_GUIDE.md
###############################################
cat << 'EOF' > "$DOCS_DIR/DEVELOPER_GUIDE.md"
# 🧑‍💻 Developer Guide

## Coding Standards
- Strict types (`declare(strict_types=1)`)
- Modular architecture
- Error handling in every layer
- No mixed responsibilities
- Consistent naming conventions

## Module Responsibilities

### debug.php
Formats and logs debug messages.

### perf.php
Measures performance and profiles SQL.

### db-conn.php
Creates PDO connections and loads environment.

### test-sample-app.php
Executes SQL, handles errors, renders UI.

EOF

###############################################
# ARCHITECTURE_GUIDE.md
###############################################
cat << 'EOF' > "$DOCS_DIR/ARCHITECTURE_GUIDE.md"
# 🏛 Architecture Guide

## System Layers

### 1. Diagnostics Layer
- debug.php
- perf.php

### 2. Data Layer
- db-conn.php

### 3. Application Layer
- test-sample-app.php
- future test harnesses

## Error Flow
- Each module catches its own errors.
- App layer handles SQL + DB exceptions.
- No fatal errors reach the user.

## Perf/Debug Integration
- `?debug=1` enables both debug and perf.

EOF

###############################################
# CODING_STANDARDS.md
###############################################
cat << 'EOF' > "$DOCS_DIR/CODING_STANDARDS.md"
# 🧾 Coding Standards

## PHP Standards
- Strict types
- PSR-12 formatting
- No inline SQL without parameters
- Always catch exceptions
- Use debug groups

## UI Standards
- Bootstrap 5
- Responsive layout
- DataTables for tables

EOF

###############################################
# MYSQL_GUIDE.md
###############################################
cat << 'EOF' > "$DOCS_DIR/MYSQL_GUIDE.md"
# 🗄️ MySQL Schema Guide

## Expected Schema
Minimum columns:
- id
- title
- description (optional)
- tip (optional)
- created_at (optional)

## Indexing Strategy
- PRIMARY KEY (id)
- INDEX(title)
- INDEX(created_at)

## Query Patterns
- SELECT with LIMIT
- ORDER BY id DESC

EOF

###############################################
# ROADMAP.md
###############################################
cat << 'EOF' > "$DOCS_DIR/ROADMAP.md"
# 🧭 Project Roadmap

## Active Tasks
- Auto-detect SELECT builder
- Schema introspection tool
- Slow query simulation

## Pending Tasks
- test-conn.php
- sql-perf.php
- debug-test.php
- perf-test.php

## Future Enhancements
- Full diagnostic dashboard
- Query optimizer assistant
- Multi-environment test suite

EOF

###############################################
# TEST_PLAN.md
###############################################
cat << 'EOF' > "$DOCS_DIR/TEST_PLAN.md"
# 🧪 Test Plan

## Connection Tests
- Host resolution
- DNS timing
- SSL handshake

## SQL Tests
- SELECT profiling
- Missing column detection
- Slow query simulation

## Debug Tests
- Level filtering
- Group filtering

## Perf Tests
- Timer accuracy
- SQL profiling

## UI Tests
- DataTables sorting/searching
- Bootstrap responsiveness

EOF

###############################################
# LEARN_DEBUG.md
###############################################
cat << 'EOF' > "$DOCS_DIR/LEARN_DEBUG.md"
# 🧠 Learn Debug System

## Topics
- Debug levels
- Debug groups
- JSON/raw/text formatting
- File/line reporting
- Logging

EOF

###############################################
# LEARN_PERF.md
###############################################
cat << 'EOF' > "$DOCS_DIR/LEARN_PERF.md"
# ⚡ Learn Performance System

## Topics
- Timers
- SQL profiling
- Function profiling
- Loop profiling
- Perf summary interpretation

EOF

###############################################
# LEARN_SQL.md
###############################################
cat << 'EOF' > "$DOCS_DIR/LEARN_SQL.md"
# 🧩 Learn SQL Diagnostics

## Topics
- SELECT profiling
- SQL error handling
- Schema validation
- Index effectiveness

EOF

###############################################
# LEARN_UI.md
###############################################
cat << 'EOF' > "$DOCS_DIR/LEARN_UI.md"
# 🎨 Learn UI System

## Topics
- Bootstrap 5 layout
- Responsive design
- DataTables sorting/searching
- Error message display

EOF

###############################################
# LEARN_ERRORS.md
###############################################
cat << 'EOF' > "$DOCS_DIR/LEARN_ERRORS.md"
# 🚨 Learn Error Handling

## Topics
- Module-level error catching
- App-level SQL error handling
- DB connection errors
- Debug logging of errors

EOF

###############################################
# AI_GUIDE.md
###############################################
cat << 'EOF' > "$DOCS_DIR/AI_GUIDE.md"
# 🤖 AI Integration Guide

## Topics
- AI-assisted documentation generation
- AI-assisted code review
- AI-assisted SQL optimization
- AI-assisted performance tuning

EOF

###############################################
# TROUBLESHOOTING.md
###############################################
cat << 'EOF' > "$DOCS_DIR/TROUBLESHOOTING.md"
# 🛠 Troubleshooting Guide

## Common Issues
- DB connection failures
- SQL column errors
- Perf profiling errors
- Debug output missing

EOF

###############################################
# SECURITY_GUIDE.md
###############################################
cat << 'EOF' > "$DOCS_DIR/SECURITY_GUIDE.md"
# 🔐 Security Guide

## Topics
- SQL injection prevention
- Safe SELECT builder
- Error sanitization
- Environment variable safety

EOF

###############################################
# RELEASE_NOTES.md
###############################################
cat << 'EOF' > "$DOCS_DIR/RELEASE_NOTES.md"
# 📝 Release Notes

## v1.0
- Core modules complete
- Test app created
- Documentation generator added

EOF

echo "✅ Documentation generated in $DOCS_DIR/"

