# To-Do: Manual Patient Creation

This document outlines the steps required to implement the manual creation of patient profiles by clinic admins. This allows clinics to manage patients who do not yet have an account in the patient application.

## Phase 1: Core Implementation (No User Link)

### 1. Update Data Models and Types (`types/patient/index.ts`)

- [ ] Modify `PatientProfile` to make `userRef` optional.
- [ ] Modify `CreatePatientProfileData` to make `userRef` optional.
- [ ] Add a new field `isManual: boolean` to `PatientProfile` and `CreatePatientProfileData` to flag patients created without a user account.
- [ ] Modify `PatientSensitiveInfo` to make `userRef` optional.
- [ ] Modify `CreatePatientSensitiveInfoData` to make `userRef` optional.
- [ ] Create a new type `CreateManualPatientData` that combines necessary fields from `CreatePatientProfileData` and `CreatePatientSensitiveInfoData`, but tailored for manual creation (e.g., requires `firstName`, `lastName`, `clinicId`).

### 2. Update Validation Schemas (`validations/patient.schema.ts`)

- [ ] Update `patientProfileSchema` to reflect that `userRef` is optional.
- [ ] Update `createPatientProfileSchema` to make `userRef` optional and include `isManual`.
- [ ] Update `patientSensitiveInfoSchema` to reflect that `userRef` is optional.
- [ ] Update `createPatientSensitiveInfoSchema` to make `userRef` optional.
- [ ] Create a new schema `createManualPatientSchema` to validate the input for the new service method.

### 3. Implement New Service Method (`services/patient/patient.service.ts`)

- [ ] Create a new public method: `createManualPatient(data: CreateManualPatientData, requester: RequesterInfo): Promise<PatientProfile>`.
- [ ] The method should perform security checks to ensure the requester is a `clinic_admin`.
- [ ] Inside the method:
  - Generate a new `patientId`.
  - Create a `PatientProfile` object with `isManual: true`, no `userRef`, and link it to the admin's clinic (`requester.associatedClinicId`).
  - Create a `PatientSensitiveInfo` object with the provided patient details.
  - Use a Firestore batch write to create both documents atomically.
  - Return the newly created `PatientProfile`.

### 4. Documentation & README

- [ ] Update the `Api/docs/guides/patient-management.md` to document the new manual creation flow.
- [ ] Create a `README.md` file in `Api/src/services/patient/` explaining the purpose of the service and its key methods, including `createManualPatient`.

## Phase 2: Future Enhancements (Post-MVP)

- [ ] **Claim/Merge Profile:** Implement logic for a patient to claim a manually created profile when they register for the app. This could involve an invitation flow or matching based on personal details.
- [ ] **UI Implementation:** Develop the UI components in the Backoffice/Clinic app for clinic admins to use this new functionality.
