# NGBVD Backend Error + API Data Quality Fix Notes

## Scope
This patch is intentionally backend/stability focused. It does not change the accepted UI/UX baseline except for the roles/analysis support assets required to resolve current errors.

## Errors resolved

### 1. Missing DataEntryController::caseReport()
Restored the case report method required by `/dataentry/report?id=...`.

### 2. Missing SettingsController::resetTheme()
Restored the theme reset method required by `/settings/theme/reset`.

### 3. Roles permission route error
Resolved the missing parameter issue on `/roles/list` by removing fragile route-name generation and using URL generation with the role ID directly.

## Analysis reporting standard
The analysis workbench now supports the agreed standard:

- Flexmonster Pivot
- PivotJS
- Grid Data
- Visual Summary

Applied to:

- `/analysis`
- `/analysis/mine`
- `/analysis/region`

## API data quality corrections
The incident statistics API now addresses the key findings in the API Data Quality Assessment Report:

- `total_incidents` is a real record count, not a dictionary-key count.
- gender grouping includes Male, Female and Unknown.
- national_status is grouped genuinely instead of repeating one total per category.
- religion is grouped genuinely instead of repeating one total per category.
- all dimensions are calculated from the same base `incident_data` record population.
- blank incident-type keys are excluded from main output and counted as quarantined records.
- null dimension values are returned under Unknown.
- response includes metadata: `api_version`, `data_as_of`, `filters_applied`, `record_count`.

## API routes
Existing v1 route remains:

- `/api/v1/incidentstats`

A quality-corrected v2 alias was added:

- `/api/v2/incidentstats`

Both currently use the corrected aggregation service.

## Apply steps

```powershell
composer dump-autoload
php artisan optimize:clear
php artisan route:clear
php artisan config:clear
php artisan view:clear
php artisan cache:clear
```

## Test URLs

- `/dataentry/report?id=MjE0NzQ1MTE1Nw==`
- `/settings/theme/reset`
- `/roles/list`
- `/roles/permissions/{role_id}`
- `/analysis`
- `/analysis/mine`
- `/analysis/region`
- `/api/v1/incidentstats`
- `/api/v2/incidentstats`
