# Phase 8: Export Pipeline + Polish

**Status:** ⬜ Not Started
**Parent plan:** [PLAN.md](./PLAN.md)
**Domain:** fullstack

## Objective
Implement PDF/PNG export for all views, add room templates, and perform final integration polish. This is the final Big Bang phase — everything must compile, pass tests, and work end-to-end.

## Tasks

- [ ] Task 1: Implement PNG export for 2D top-down view — export the Konva stage to PNG image via `stage.toDataURL()`. Options: include/exclude grid, include/exclude measurements, scale factor.
- [ ] Task 2: Implement PNG export for each wall projection view — same approach, each projection exports independently.
- [ ] Task 3: Implement PNG export for 3D view — capture Three.js renderer output via `renderer.domElement.toDataURL()`.
- [ ] Task 4: Implement PDF export for single room — compose a PDF page with: room name/dimensions header, 2D top-down view, 4 projection views, optional 3D preview. Use jsPDF with canvas images.
- [ ] Task 5: Implement PDF export for full apartment — multi-page PDF. Cover page with apartment info, then one section per room (same layout as single room export). Table of contents.
- [ ] Task 6: Add export dialog UI — modal with export options: format (PNG/PDF), scope (current view / room / apartment), layers to include, scale, paper size (A4/A3/Letter).
- [ ] Task 7: Create room templates — preset room configurations: bedroom (bed, nightstands, wardrobe), kitchen (counters, table, chairs), bathroom (placeholder shapes), living room (sofa, table, TV stand), office (desk, chair, bookcase). Templates provide initial furniture layout.
- [ ] Task 8: Implement "Apply Template" UI — when creating a room, option to start from a template. Template applies furniture items and suggested room dimensions.
- [ ] Task 9: Implement copy room layout — duplicate a room's element layout (electrical + furniture) to another room in the same or different apartment.
- [ ] Task 10: Final integration — ensure all phases work together end-to-end. Fix any Big Bang integration issues: type mismatches, missing imports, broken references between phases.
- [ ] Task 11: Write comprehensive tests — unit tests for geometry utils, API endpoint tests, component render tests for key UI components. Target 80%+ coverage on critical paths.
- [ ] Task 12: Ensure full build passes — `turbo build` succeeds for all packages. Fix all TypeScript errors. Fix all ESLint warnings.
- [ ] Task 13: Ensure full test suite passes — `turbo test` succeeds.
- [ ] Task 14: Add loading performance optimization — lazy load the 3D view (React.lazy), lazy load export libraries (jsPDF, html2canvas).

## Files to Modify/Create
- `apps/client/src/components/export/ExportDialog.tsx` — export options modal
- `apps/client/src/components/export/exportUtils.ts` — canvas-to-image helpers
- `apps/client/src/components/export/pdfExport.ts` — PDF generation logic
- `apps/client/src/components/export/pngExport.ts` — PNG export logic
- `apps/client/src/components/templates/roomTemplates.ts` — template definitions
- `apps/client/src/components/templates/TemplateSelector.tsx` — template picker UI
- `apps/client/src/components/rooms/CopyRoomDialog.tsx` — copy layout dialog
- `apps/client/src/api/client.ts` — add copy room API call (update)
- `apps/server/src/routes/rooms.ts` — add `POST /api/rooms/:id/copy` endpoint (update)
- Various test files across packages
- Multiple existing files may need fixes for Big Bang integration

## Acceptance Criteria
- PNG export works for 2D view, projection views, and 3D view
- PDF export generates clean, printable documents for single rooms and full apartments
- Room templates provide sensible starting layouts for common room types
- Copy room layout duplicates elements to another room
- Full build passes: `turbo build` with 0 errors
- Full test suite passes: `turbo test` with 0 failures
- All TypeScript strict mode errors resolved
- Application runs end-to-end: create apartment → add rooms → edit with all tools → export PDF

## Notes
- This is the Big Bang final phase — ALL accumulated integration issues must be resolved here
- PDF should be print-quality — use appropriate DPI (300 for print)
- Export should work offline (client-side only, no server dependency)
- Templates are just convenience — users can always start from scratch and build manually
- Test priority: geometry utils > API endpoints > editor interactions > export
- Lazy loading reduces initial bundle size — 3D and export are not needed on first load

## Review Checklist
- [ ] All tasks completed
- [ ] Code follows project conventions
- [ ] No unintended side effects
- [ ] Build passes (REQUIRED — final Big Bang phase)
- [ ] Tests pass (REQUIRED — final Big Bang phase)

## Handoff to Next Phase
<!-- This is the final phase — no handoff needed -->
