#!/bin/bash
set -e

# Always write inside project folder
BASE="db-dashboard/docs"

echo "Updating DB Dashboard documentation..."

# Create folder safely
mkdir -p "$BASE"

###############################################
# BUG REPORT
###############################################
cat << 'EOF' > "$BASE/BUG-DBDASH-001.md"
# BUG-DBDASH-001  
## Predefined Query Output Not Clearing on Error  
**Status:** OPEN  
**Severity:** Medium  
**Owner:** Rick  

### Description  
When running predefined queries, stale output persists after an error.  
The dashboard displays both the error message and previous successful rows.  
This is misleading and unsafe for diagnostics.

### Symptoms  
- Error message appears  
- Stale rows remain visible  
- Output container not cleared before rendering  

### Root Cause  
Rendering logic prints `<pre>` blocks unconditionally.  
No state reset occurs before rendering error output.

### Required Fix  
- Add output container reset  
- Add renderResult() helper  
- Refactor predefined/custom query blocks  
EOF

###############################################
# DEVELOPER DOCUMENTATION
###############################################
cat << 'EOF' > "$BASE/DEVELOPER-DOCS.md"
# DB Dashboard Developer Documentation

## Overview
The DB Dashboard is a diagnostics tool for safe, SELECT-only database inspection.  
It supports:
- .env defaults  
- Independent overrides  
- Remote/local switching  
- Diagnostics suite  
- Schema inspector  
- Table profiler  
- Failure simulation  

## Security Model
The dashboard enforces SELECT-only mode.  
Blocked keywords:
DELETE, UPDATE, INSERT, DROP, ALTER, TRUNCATE, CREATE

## Rendering Rules
- All output containers must be cleared before rendering new results.  
- Errors must display only:
  - Error message  
  - Empty JSON object `{}`  
- No stale rows may remain visible.

## Override Rules
- Only non-empty override fields apply.  
- Empty fields do not override `.env` defaults.  
- Reset button restores `.env` defaults.

## Bug Tracking
See BUG-DBDASH-001 for current open issue regarding stale predefined output.
EOF

###############################################
# TASK CARDS
###############################################
cat << 'EOF' > "$BASE/TASKS.md"
# Engineering Tasks for DB Dashboard

## TASK-DBDASH-001  
Refactor predefined query renderer to clear output before rendering.

## TASK-DBDASH-002  
Refactor custom query renderer with same logic.

## TASK-DBDASH-003  
Add renderResult() helper for unified rendering.

## TASK-DBDASH-004  
Add output containers (#custom-output, #predefined-output, etc.)

## TASK-DBDASH-005  
Add automated test cases for:
- Missing table  
- Missing column  
- Bad credentials  
- Invalid schema  
- Destructive SQL  
- Empty overrides  
- Override combinations  

## TASK-DBDASH-006  
Add Bash installer + scaffolding generator.

## TASK-DBDASH-007  
Add Bash code generator for dashboard + diagnostics.

## TASK-DBDASH-008  
Add Bash updater for future patches.
EOF

###############################################
# INSTALLATION DOCS
###############################################
cat << 'EOF' > "$BASE/INSTALLATION.md"
# Installation Instructions

## 1. Run installer

