# Changelog

All notable changes to this project will be documented in this file.

## [Unreleased] — 2026-07-30

### Added
- **Budget Decision Lab** — Decision-support report page at `/budget-analysis`
  - 90/60/30-day window selector
  - Aggregate stats: total changes, manual changes, success rate, verdict distribution
  - Auto-generated recommendations based on data patterns
  - Scale % bucket analysis (decrease / 0-25% / 25-50% / 50-100% / 100%+)
  - Top-10 most-scaled campaigns with verdict distribution
  - Filterable history table (ALL / GOOD / BAD / NEUTRAL / RESET / PAUSE / NO_DATA)
  - Per-row before/after KPI 7d windows
  - Color-coded deltas (green/red/gray)
  - 25/26 UI tests passing (mobile + desktop)
  - Backend: `GET /public/budget-analysis?days=N&campaignId=...`
  - Verdict logic: GOOD if revenue Δ ≥ +30% (or ROAS Δ ≥ +1), BAD if revenue Δ ≤ -30% (or ROAS Δ ≤ -1), else NEUTRAL

- **Post-Midnight Reapply Cron** (`src/cron/postMidnightReapply.ts`)
  - Runs at 00:05-00:09 ICT daily (5 min after midnight-reset)
  - Re-applies production budgets to proven-winner campaigns after reset clears them
  - Reads list from `app_config.post_midnight_reapply_json` (JSON array)
  - Idempotent (skips campaigns already at target budget)
  - Logs each reapply to `scale_action_log` with actor=`post-midnight-reapply`
  - Initial config: `1 [buy]tsm` (id 120230799572200590) → ฿88/d
  - Bumped `max_budget_increase_pct_per_change` from 100% → 200% to allow 39→88 (+125%)

- **Audit log entry** for manual `1 [buy]tsm` scale (฿69 → ฿88) — `id=85` in `scale_action_log`

### Changed
- `src/index.ts`: added imports + register call for budget analysis route and post-midnight reapply cron
- `nginx` config (sites-enabled/adsfb.namnan.co.th): added `location = /budget-analysis` and `location = /budget-analysis.html`
- `app_config.max_budget_increase_pct_per_change`: 100 → 200 (to enable post-midnight reapply)

### Verified
- ✅ 25/26 UI tests passing (full_test.py)
- ✅ PM2 service running with new routes + cron
- ✅ `/public/budget-analysis` returns valid data
- ✅ Page renders without JS errors (fixed initial null reference)
- ✅ Mobile (390px) + Desktop (1440px) both render correctly
- ✅ nginx serves `/budget-analysis` (200 OK)
- ✅ Existing functionality unaffected (other API endpoints still work)

## Earlier (uncommitted)

### 2026-07-30 — `1 [buy]tsm` manual scale
- User request: scale `1 [buy]tsm` from ฿69 → ฿88/day
- Done via Meta Marketing API v19.0 direct call
- 30d ROAS = 14.67x justifies the scale
- Daily spend target: ~฿88 vs current ~฿39 = +125% (overrides old cap)

### 2026-07-30 — midnight-reset discovery
- Found that `midnight-reset` cron resets ALL active campaigns to ฿39 every 00:00 ICT
- 7-day history shows 6 reset events for `1 [buy]tsm` (฿69/88/99/159 → ฿39)
- This was killing the scale immediately — need post-midnight reapply to fix
