# Implementation Plan

## 1. Align Task Metadata

- Update task title/description if needed so the Trellis task reflects `健康数据管理` instead of care-service workspace wording.
- Keep the existing task directory unless a rename is explicitly requested.

## 2. Schema And Migration

- Add health enums and tables in `modules/core/server/schema.ts`.
- Add indexes/unique constraints:
  - profile unique `(organizationId, elderId)`
  - recent vital lookups by `(organizationId, elderId, recordedAt)`
  - review queues by `(organizationId, status, severity)`
- Run `pnpm db:generate`.
- Review generated migration SQL.

## 3. Types And Permissions

- Add `health:read` and `health:manage` to `modules/core/types.ts`.
- Add permission definitions and default role grants in `modules/core/server/permissions.ts`.
- Add health DTOs, labels, validators, and formatting helpers in `modules/health/types.ts`.

## 4. Server Operations

- Create `modules/health/server/operations.ts`.
- Implement batched list helper for the admin page.
- Implement mutation helpers for:
  - profile upsert
  - vital create
  - chronic condition create
  - review status update
- Ensure all helpers require an organization ID and validate elder/review ownership.

## 5. API Routes

- Add `/api/health/admin`.
- Add `/api/health/profiles/[elderId]`.
- Add `/api/health/vitals`.
- Add `/api/health/chronic-conditions`.
- Add `/api/health/reviews/[id]`.
- Use `requirePermission`, structured `jsonSuccess` / `jsonFailure`, and audit logs.

## 6. Admin Page UI

- Add `app/(app)/app/settings/health/page.tsx`.
- Add `app/(app)/app/[organizationSlug]/settings/health/page.tsx` delegating to the unscoped route pattern.
- Add `modules/health/components/HealthAdminClient.tsx`.
- Use existing UI adapters for buttons, cards, dialogs, inputs, selects, tables, and badges.
- Add management nav and breadcrumbs.

## 7. Default Workspace Data

- Carefully inspect current uncommitted `modules/core/server/default-workspace-data.ts`.
- Add health seed records only if compatible with existing default seed flow.
- Tie seed records to seeded elders and organization.

## 8. Verification

- Run `pnpm lint`.
- Run `pnpm type-check`.
- Run `pnpm build`.
- If a database is available, run `pnpm db:migrate` and manually test:
  - scoped management page loads.
  - health profile upsert persists.
  - vital record create persists.
  - abnormal review update persists.
  - insufficient permission returns `403`.

## Rollback Points

- Before migration generation: schema changes can be removed directly.
- After migration generation: remove generated migration files and schema changes together.
- After UI/API addition: remove `modules/health`, health API routes, settings health pages, nav/breadcrumb additions, and health permissions.
