# Profile Feature Documentation

## Overview
The Profile feature allows users (both doctors and patients) to manage their personal information, change passwords, and for doctors specifically, upload due diligence documents like medical licenses and digital signatures.

## Features

### 1. Personal Information Tab
All users (doctors and patients) have access to:
- **Profile Photo Upload**: Change profile picture (JPEG, PNG, JPG, GIF, max 2MB)
- **View Personal Details**: Display name, email, phone, and user type (read-only)
- **Change Password**: Update account password with validation
- **Doctor-specific fields**: Specialty, NPI Number, DEA Number (read-only)

### 2. Due Diligence Tab (Doctors Only)
Doctors have an additional tab for uploading credentials:
- **Medical License Upload**: Upload medical license (PDF, JPEG, PNG, max 5MB)
- **Digital Signature Upload**: Upload digital signature (PNG, JPEG, JPG, max 2MB)
- Both documents can be viewed and re-uploaded as needed

## Access Points

### Desktop Navigation
- Click on the user profile section in the sidebar (top of the sidebar)
- A dropdown menu will appear with:
  - "My Profile" - Navigate to profile page
  - "Sign Out" - Log out of the application

### Mobile Navigation
- Bottom navigation bar has a "Profile" icon
- Tap to access the profile page directly

## File Structure

### Controllers
- `app/Http/Controllers/ProfileController.php`
  - `show()`: Display profile page
  - `updatePhoto()`: Handle profile photo uploads
  - `updatePassword()`: Handle password changes
  - `updateDocuments()`: Handle license and signature uploads

### Views
- `resources/views/profile/show.blade.php`: Main profile page with tabs

### Routes
```php
Route::get('/profile', [ProfileController::class, 'show'])->name('profile.show');
Route::post('/profile/photo', [ProfileController::class, 'updatePhoto'])->name('profile.update.photo');
Route::post('/profile/password', [ProfileController::class, 'updatePassword'])->name('profile.update.password');
Route::post('/profile/documents', [ProfileController::class, 'updateDocuments'])->name('profile.update.documents');
```

### Database
Migration: `database/migrations/2026_01_20_125049_add_profile_fields_to_users_table.php`

Added fields to `users` table:
- `user_type` (string, default: 'patient')
- `phone` (string, nullable)
- `profile_photo` (string, nullable)
- `license_file` (string, nullable)
- `signature_file` (string, nullable)
- `npi_number` (string, nullable)
- `dea_number` (string, nullable)
- `specialty` (string, nullable)

### File Storage
Files are stored in:
- Profile photos: `public/uploads/profiles/`
- Licenses: `public/uploads/licenses/`
- Signatures: `public/uploads/signatures/`

## Security Considerations

### File Upload Validation
1. **Profile Photos**:
   - Allowed types: JPEG, PNG, JPG, GIF
   - Max size: 2MB
   - Validated using Laravel's image validation rule

2. **Medical Licenses**:
   - Allowed types: PDF, JPEG, PNG
   - Max size: 5MB
   - Server-side validation

3. **Digital Signatures**:
   - Allowed types: PNG, JPEG, JPG
   - Max size: 2MB
   - Transparent PNG recommended for best quality

### Password Change
- Current password verification required
- New password must be at least 8 characters
- Password confirmation required
- Uses Laravel's password hashing

### File Storage Security
- Files stored outside of web root in production (recommended)
- For prototype, files stored in `public/uploads/` with .gitignore
- In production, use Laravel Storage with proper disk configuration

## Error Handling

### Success Messages
- Profile photo uploaded successfully
- Password changed successfully
- Documents uploaded successfully

### Error Messages
- Invalid file format or size
- Password mismatch
- Current password incorrect
- Upload failure

All messages displayed using Bootstrap alerts with auto-dismiss functionality.

## User Experience

### Responsive Design
- Desktop: Full two-column layout for personal info, single column for documents
- Mobile: Stacked layout, easy touch targets
- Tab navigation works seamlessly on all screen sizes

### Visual Feedback
- Green success alerts for successful operations
- Red error alerts for failures
- Blue info alerts for informational messages
- Form validation messages under each field

### Accessibility
- Proper ARIA labels on all interactive elements
- Keyboard navigation support
- Screen reader friendly
- High contrast for readability

## Testing Checklist

### Manual Testing
- [ ] Navigate to profile page from sidebar dropdown
- [ ] Navigate to profile page from mobile bottom nav
- [ ] Upload profile photo (test various formats and sizes)
- [ ] View uploaded profile photo in sidebar
- [ ] Change password with valid credentials
- [ ] Try changing password with mismatched confirmation
- [ ] Try changing password with short password (<8 chars)
- [ ] Upload medical license (doctor account only)
- [ ] View uploaded license document
- [ ] Upload digital signature (doctor account only)
- [ ] View uploaded signature
- [ ] Test file size validation (exceed max sizes)
- [ ] Test file type validation (wrong formats)
- [ ] Switch between tabs
- [ ] Test responsive layout on mobile

### Integration Points
- Sidebar dropdown menu displays correctly
- Profile photo updates in sidebar after upload
- Mobile navigation highlights profile when active
- Logout functionality works from dropdown

## Future Enhancements

### Potential Improvements
1. **Profile Editing**: Allow editing of other personal fields
2. **Email Verification**: Implement email change with verification
3. **Two-Factor Authentication**: Add 2FA for enhanced security
4. **Document Verification**: Integrate with third-party verification services
5. **Profile Completeness**: Show progress bar for profile completion
6. **Activity Log**: Track profile changes and document uploads
7. **Digital Signature Tool**: Allow drawing signature directly in browser
8. **License Expiration**: Track and alert for expiring medical licenses

### Production Considerations
1. Move file storage to Laravel Storage with S3 or similar
2. Implement proper authentication middleware
3. Add rate limiting to upload endpoints
4. Implement virus scanning for uploaded files
5. Add CSRF protection (already included in forms)
6. Implement proper user authorization checks
7. Add audit logging for sensitive changes
8. Implement file cleanup for replaced uploads

## Integration with Existing Features

### eRX Prescriptions
- Doctor's signature from profile can be used in prescription documents
- License information displayed on prescription forms
- Profile information pre-fills prescriber details

### Dashboard
- Profile completeness can be shown on dashboard
- Reminders for missing documents

### Notifications
- Email notifications for profile changes
- Alerts for document expiration (future enhancement)

## API Endpoints (if needed)

For future SPA or mobile app integration:
```
GET    /api/profile              - Get user profile
POST   /api/profile/photo        - Upload profile photo
PUT    /api/profile/password     - Update password
POST   /api/profile/documents    - Upload documents
GET    /api/profile/documents    - Get document list
```

## Troubleshooting

### Common Issues

**Issue**: Files not uploading
- Check directory permissions (uploads/ folders should be writable)
- Verify PHP `upload_max_filesize` and `post_max_size` in php.ini
- Check Laravel `max_upload_size` in validation

**Issue**: Profile photo not displaying
- Verify file path is correct in session/database
- Check that file was actually moved to public/uploads/profiles/
- Verify asset() helper is generating correct URL

**Issue**: Dropdown not working
- Ensure Bootstrap JS is loaded
- Check for JavaScript console errors
- Verify data-bs-toggle="dropdown" attribute is present

**Issue**: Password change not working
- Verify password validation rules
- Check that current password verification is implemented
- Ensure password hashing is working correctly

## Migration Commands

To run the migration:
```bash
php artisan migrate
```

To rollback:
```bash
php artisan migrate:rollback
```

## Notes for Production

1. **Database**: Currently using session-based storage for prototype. In production, all data should be stored in the database with proper Eloquent relationships.

2. **Authentication**: Currently using session-based mock authentication. Implement proper Laravel authentication with guards.

3. **File Storage**: Move to Laravel Storage facade with proper disk configuration (S3, DO Spaces, etc.)

4. **Validation**: Add more robust server-side validation for all inputs

5. **Logging**: Implement proper logging for all profile changes and document uploads

6. **Backup**: Implement regular backups of uploaded documents

7. **Compliance**: Ensure HIPAA compliance for medical documents and PHI

8. **Audit Trail**: Log all changes to profile and documents for compliance purposes
