---
phase: 11.1-agent-and-client-initials
plan: "02"
subsystem: api, pdf
tags: [pdf-lib, nextjs, agent-initials, prepare-pipeline]

# Dependency graph
requires:
  - phase: 11.1-agent-and-client-initials
    provides: agentInitialsData column on users table, /api/agent/initials route, 'agent-initials' SignatureFieldType, isClientVisibleField() security boundary
  - phase: 11-agent-saved-signature-and-signing-workflow
    provides: embed-once-draw-many pattern in preparePdf(), agent-signature 422 guard pattern in prepare route
provides:
  - preparePdf() with 6th optional param agentInitialsData; embeds PNG at each agent-initials field coordinate using embed-once-draw-many pattern
  - 422 guard in prepare route returning { error: 'agent-initials-missing' } when agent-initials fields present but no initials saved
  - prepare route fetches agentInitialsData alongside agentSignatureData in single DB query
affects:
  - 11.1-03 (E2E human verification of full agent-initials round-trip)

# Tech tracking
tech-stack:
  added: []
  patterns:
    - "embed-once-draw-many pattern extended to agentInitialsImage — embedPng called once before field loop, drawImage reused per field"
    - "422 guard pattern for missing agent-owned data mirrors the existing agent-signature guard exactly"
    - "Single DB query fetches all agent-owned data (agentSignatureData + agentInitialsData) via columns selector"

key-files:
  created: []
  modified:
    - teressa-copeland-homes/src/lib/pdf/prepare-document.ts
    - teressa-copeland-homes/src/app/api/documents/[id]/prepare/route.ts

key-decisions:
  - "agentInitialsData added as 6th optional param with default null — existing 5-arg call sites continue to compile without modification"
  - "agent-initials branch added after agent-signature branch in field loop — 'initials' (client-initials) branch completely untouched"
  - "422 guard for missing initials placed after existing agent-signature guard — parallel pattern for agent-owned field types"

patterns-established:
  - "Agent-owned field embedding pattern: embed once before loop, draw per field, guard 422 at route before calling preparePdf — applies to any future agent-owned image type"

requirements-completed: [INIT-03, INIT-04]

# Metrics
duration: 4min
completed: 2026-03-21
---

# Phase 11.1 Plan 02: Agent Initials Prepare Pipeline Summary

**preparePdf() extended with embed-once-draw-many agentInitialsImage; prepare route fetches initials in same DB query and guards 422 on missing initials — agent initials now baked into prepared PDFs**

## Performance

- **Duration:** 4 min
- **Started:** 2026-03-21T21:02:18Z
- **Completed:** 2026-03-21T21:06:00Z
- **Tasks:** 2
- **Files modified:** 2

## Accomplishments
- preparePdf() gains 6th optional param agentInitialsData; embeds PNG once before field loop and draws at each agent-initials field coordinate
- prepare route fetches both agentSignatureData and agentInitialsData in a single DB query (one round-trip, not two)
- 422 guard returns { error: 'agent-initials-missing' } when agent-initials fields present but no initials saved — no silent failure
- Existing 'initials' (client-initials) branch and 'agent-signature' branch left completely untouched — zero regressions

## Task Commits

Each task was committed atomically:

1. **Task 1: preparePdf() — add agentInitialsData param and embed at agent-initials field coordinates** - `fae1cf1` (feat)
2. **Task 2: prepare route — fetch agentInitialsData in same query, 422 guard, pass to preparePdf** - `c876579` (feat)

**Plan metadata:** (included in final docs commit)

## Files Created/Modified
- `teressa-copeland-homes/src/lib/pdf/prepare-document.ts` - Added agentInitialsData 6th param, embedPng block before loop, agent-initials drawImage branch in loop
- `teressa-copeland-homes/src/app/api/documents/[id]/prepare/route.ts` - Added agentInitialsData to DB query columns, agentInitialsData variable, hasAgentInitialsFields 422 guard, agentInitialsData as 6th arg to preparePdf()

## Decisions Made
- agentInitialsData defaults to null (6th optional param) so existing call sites compile unchanged until Task 2 updates the prepare route
- Placed agent-initials drawImage branch immediately after agent-signature branch — consistent ordering, client-initials branch untouched
- DB query extended via columns selector (not a second query) — single round-trip matches Phase 11 Plan 02 pattern

## Deviations from Plan

None - plan executed exactly as written.

## Issues Encountered
None.

## User Setup Required
None - no external service configuration required.

## Next Phase Readiness
- Plan 03 is pure human E2E verification: visit profile to draw/save initials, place agent-initials field on a document, prepare it, verify PNG embedded at correct coordinates in the output PDF
- 422 guard ready to test: prepare with no initials saved should return { error: 'agent-initials-missing' }
- Client-initials regression check: purple Initials placeholder still draws at prepare time; client still prompted during signing

## Self-Check: PASSED
- FOUND: teressa-copeland-homes/src/lib/pdf/prepare-document.ts
- FOUND: teressa-copeland-homes/src/app/api/documents/[id]/prepare/route.ts
- FOUND: .planning/phases/11.1-agent-and-client-initials/11.1-02-SUMMARY.md
- FOUND: commit fae1cf1 (Task 1)
- FOUND: commit c876579 (Task 2)

---
*Phase: 11.1-agent-and-client-initials*
*Completed: 2026-03-21*
