# Patient Import Guide - Remote Monitoring

## Overview

The `ImportRemoteMonitoringPatients` command allows you to bulk import patient data from a CSV file into the remote monitoring system. This command creates patient user accounts, insurance records, and optionally patient intake records.

## Command Signature

```bash
php artisan patients:import-remote-monitoring {file} [options]
```

## Arguments

- `file` (required): Path to the CSV file containing patient data

## Options

- `--staff-id=ID`: Staff member ID to assign as the intake creator (required if using `--create-intake`)
- `--create-intake`: Automatically create patient intake records for each patient
- `--dry-run`: Preview what would be imported without actually importing

## CSV File Format

The CSV file must have the following columns (in any order):

| Column | Required | Description |
|--------|----------|-------------|
| name | Yes | Patient's full name |
| Email Address | No | Patient's email address |
| phone | No | Patient's phone number |
| DOB | No | Date of birth (format: MM/DD/YY) |
| address | No | Patient's address |
| conditions | No | Medical conditions (comma-separated) |
| meds | No | Current medications |
| insurance company | No | Insurance provider name |
| Policy Number | No | Insurance policy number |
| Group Number | No | Insurance group number |
| Plan Type HMO PPO HMO-POS | No | Insurance plan type |

### Supported Conditions

The import command recognizes the following conditions:
- **Hypertension**: "high blood pressure", "hypertension"
- **Diabetes**: "diabetes"
- **Heart Failure**: "heart", "cardiac"
- **Chronic Pain**: "chronic pain", "pain"
- **Arthritis**: "arthritis"
- **COPD/Asthma**: "copd", "asthma"

### Supported Plan Types

- HMO
- PPO
- POS
- EPO
- HDHP

## Usage Examples

### 1. Dry Run (Preview Import)

```bash
php artisan patients:import-remote-monitoring app/Console/for-import.csv --dry-run
```

This shows what would be imported without making any database changes.

### 2. Basic Import (Create Patients & Insurance Only)

```bash
php artisan patients:import-remote-monitoring app/Console/for-import.csv
```

This creates:
- Patient user accounts with role "patient"
- Insurance records for each patient

### 3. Import with Patient Intake Records

```bash
php artisan patients:import-remote-monitoring app/Console/for-import.csv --staff-id=5 --create-intake
```

This creates:
- Patient user accounts
- Insurance records
- Patient intake records (assigned to staff member ID 5)

The intake records automatically:
- Parse medical conditions from the CSV
- Determine the appropriate service path (RPM, RTM, or CCM)
- Set default values for tech qualification (smartphone: true, devices: true, caregiver: false)

## What Gets Created

### 1. User Account (Patient)

Each patient gets a user account with:
- Name, email, phone, date of birth, address
- Role: "patient"
- Status: "active"
- Auto-generated password (16 random characters)

If email is missing, a temporary email is generated: `{slug-name}@temp.local`

### 2. Insurance Record

If insurance information is provided:
- Provider name
- Policy number
- Group number
- Plan type (normalized to HMO/PPO/POS/EPO)
- Status: "active"

### 3. Patient Intake Record (Optional)

If `--create-intake` flag is used:
- Condition flags based on CSV data
- Service path determination (CCM, RPM, or RTM)
- Tech qualification defaults
- Assigned to specified staff member

## Service Path Determination

The system automatically determines the appropriate service path based on conditions:

- **CCM** (Chronic Care Management): 2 or more conditions
- **RPM** (Remote Physiologic Monitoring): Hypertension, Diabetes, or Heart Failure
- **RTM** (Remote Therapeutic Monitoring): Chronic Pain, Arthritis, COPD, or Asthma

## Import Results

The command outputs:
- ✓ Success message with patient name and ID
- ⊘ Skipped records with reason
- ✗ Error messages with details
- Summary: Total imported, skipped, and errors

## Example Output

```
✓ Darlene Boss (1778)
✓ Gary Gubbs (1779)
✓ Jasmine Lea (1780)
...

Import Summary:
  Imported: 23
  Skipped: 0
```

## Error Handling

The command handles:
- Missing required fields (name)
- Invalid date formats (falls back to null)
- Duplicate emails (updates existing records)
- Database transaction rollback on errors

## Next Steps After Import

After importing patients, the next steps in the remote monitoring workflow are:

1. **Doctor Order** - Doctor creates physician orders for patients
2. **Insurance Verification** - Staff verifies insurance coverage
3. **Doctor Approval** - Doctor approves the order
4. **Patient Enrollment** - Patient begins remote monitoring

See `REMOTE_MONITORING_WORKFLOW.md` for detailed workflow information.

## Troubleshooting

### "File not found" error
- Verify the file path is correct and relative to the project root
- Example: `app/Console/for-import.csv`

### "Unknown column 'role'" error
- Ensure the application has been migrated: `php artisan migrate`
- Check that Spatie permission package is installed

### Duplicate email errors
- The command uses `updateOrCreate`, so duplicate emails will update existing records
- To prevent this, ensure email addresses are unique in your CSV

### Missing staff ID
- If using `--create-intake`, you must provide `--staff-id`
- Find staff member ID: `php artisan tinker` then `User::where('role', 'staff')->first()`

