# CV and Cover Letter Feature - Complete Implementation Guide

## Feature Architecture

```
┌─────────────────────────────────────────────────────────────────┐
│                     CURA JOB APPLICATION SYSTEM                 │
└─────────────────────────────────────────────────────────────────┘

NURSE WORKFLOW
══════════════════════════════════════════════════════════════════

Step 1: Upload Documents
┌──────────────────────────────────────────────┐
│ Nurse Dashboard → My Profile → Documents    │
│                                              │
│ Input:                                       │
│ - Document Type (CV, License, NCLEX, etc)  │
│ - File Upload (PDF, DOC, DOCX, JPG, PNG)   │
│ - Max 5MB                                   │
│                                              │
│ Stored in:                                   │
│ - storage/documents/{uuid}                  │
│ - nurse_documents table                     │
│ - Status: "uploaded"                        │
└──────────────────────────────────────────────┘
                    ↓
            Database Record:
┌──────────────────────────────────────────────┐
│ nurse_documents:                             │
│ - id: 1                                      │
│ - nurse_profile_id: 5                        │
│ - type: "CV"                                 │
│ - original_filename: "john_cv.pdf"          │
│ - file_path: "documents/..."                │
│ - status: "uploaded"                        │
│ - created_at: 2024-12-05 10:30:00           │
└──────────────────────────────────────────────┘
                    ↓

Step 2: Browse and Apply for Jobs
┌──────────────────────────────────────────────┐
│ Jobs Page → View Job Details → Application  │
│                                              │
│ Application Form:                            │
│ ┌──────────────────────────────────────┐    │
│ │ Select CV (Required):                │    │
│ │ [CV - john_cv.pdf (uploaded) ▼]     │    │
│ │                                      │    │
│ │ Cover Letter (Optional):             │    │
│ │ [Dear Hiring Manager,...]           │    │
│ │                                      │    │
│ │ [Apply Now Button]                  │    │
│ └──────────────────────────────────────┘    │
└──────────────────────────────────────────────┘
                    ↓
            Validation:
        ✓ cv_document_id exists in database
        ✓ Job posting is open
        ✓ Nurse hasn't applied yet
                    ↓
            Application Created:
┌──────────────────────────────────────────────┐
│ job_applications:                            │
│ - id: 42                                     │
│ - job_posting_id: 10                        │
│ - user_id: 5 (nurse)                        │
│ - cv_document_id: 1 ← FOREIGN KEY           │
│ - cover_letter: "Dear Hiring Manager..."   │
│ - status: "applied"                         │
│ - applied_at: 2024-12-05 14:45:00           │
└──────────────────────────────────────────────┘


EMPLOYER WORKFLOW
══════════════════════════════════════════════════════════════════

Step 1: View Applications
┌──────────────────────────────────────────────┐
│ Employer Dashboard → Applications List       │
│                                              │
│ Shows:                                       │
│ - Applicant name                             │
│ - Cover letter preview (160 chars)          │
│ - Application status                        │
│ - [View Details Button]                    │
└──────────────────────────────────────────────┘
                    ↓

Step 2: View Application Detail
┌──────────────────────────────────────────────┐
│ Application Detail Page                     │
│                                              │
│ HEADER:                                      │
│ [Job Title] | Applicant: John Smith | App1 │
│                                              │
│ CV SECTION:                                  │
│ ┌──────────────────────────────────────┐    │
│ │ 📄 CV                                │    │
│ │ john_cv.pdf                          │    │
│ │ Uploaded: Dec 5, 2024                │    │
│ │ [View Document] ← Opens in new tab   │    │
│ └──────────────────────────────────────┘    │
│                                              │
│ COVER LETTER SECTION:                        │
│ ┌──────────────────────────────────────┐    │
│ │ Dear Hiring Manager,                 │    │
│ │                                      │    │
│ │ I am excited to apply for the RN    │    │
│ │ position at your facility. With 8   │    │
│ │ years of experience in ICU care... │    │
│ └──────────────────────────────────────┘    │
│                                              │
│ STATUS MANAGEMENT:                          │
│ ┌──────────────────────────────────────┐    │
│ │ Status: [applied ▼]                  │    │
│ │ Notes: [Internal hiring notes...]    │    │
│ │ [Save Button]                        │    │
│ └──────────────────────────────────────┘    │
│                                              │
│ INTERNAL NOTES:                              │
│ ┌──────────────────────────────────────┐    │
│ │ Strong candidate. Schedule interview.│    │
│ │ - Added by Sarah (2 hours ago)       │    │
│ │ [Delete]                             │    │
│ │                                      │    │
│ │ [Add New Note...]                   │    │
│ └──────────────────────────────────────┘    │
└──────────────────────────────────────────────┘
                    ↓
            Employer Can:
        ✓ Read applicant's CV (download)
        ✓ Read applicant's cover letter
        ✓ Change application status
        ✓ Add internal notes
        ✓ Track hiring progress


DATABASE RELATIONSHIPS
══════════════════════════════════════════════════════════════════

┌─────────────────────┐
│   nurse_documents   │
├─────────────────────┤
│ id                  │ (Primary Key)
│ nurse_profile_id    │ → nurse_profiles
│ type                │ (CV, License, NCLEX, etc)
│ original_filename   │ (john_cv.pdf)
│ file_path           │ (storage path)
│ status              │ (uploaded/verified)
│ created_at          │
└─────────────────────┘
          ↑
          │ Foreign Key
          │ cv_document_id
          │
┌─────────────────────────┐
│   job_applications      │
├─────────────────────────┤
│ id                      │ (Primary Key)
│ job_posting_id          │ → job_postings
│ user_id                 │ → users (nurse)
│ cv_document_id          │ → nurse_documents ✓
│ cover_letter            │ (optional text)
│ cover_letter_text       │ (duplicate for compat)
│ status                  │ (applied/hired/etc)
│ applied_at              │
│ notes (deprecated)      │
│ created_at              │
└─────────────────────────┘
          ↑
          │ Application
          │ records
          │
┌─────────────────────┐
│   job_postings      │
├─────────────────────┤
│ id                  │
│ employer_id         │
│ title               │
│ description         │
│ status              │ (open/published)
│ ...                 │
└─────────────────────┘


FILE SYSTEM LAYOUT
══════════════════════════════════════════════════════════════════

storage/
└── documents/
    ├── {uuid1}/
    │   └── john_cv.pdf              ← Nurse-uploaded CV
    ├── {uuid2}/
    │   └── nursing_license.pdf      ← Nurse-uploaded License
    └── {uuid3}/
        └── nclex_score.pdf          ← Nurse-uploaded NCLEX


VIEW TEMPLATES
══════════════════════════════════════════════════════════════════

NURSE SIDE:
─────────
1. resources/views/nurse/profile/show.blade.php
   - Document upload form
   - Document type selector
   - File input with validation
   - Document list with status badges
   - Delete buttons

2. resources/views/jobs/show.blade.php
   - CV selection dropdown (required)
   - Warning if no CV uploaded
   - Link to upload documents
   - Cover letter textarea (optional)
   - Apply button

EMPLOYER SIDE:
──────────────
1. resources/views/employer/applications/show.blade.php
   - Applicant CV display:
     * Document type icon (📄)
     * Original filename
     * Upload date
     * "View Document" link
   - Cover letter display:
     * Full text with whitespace preserved
     * Only shown if exists
   - Application notes system:
     * Add new notes
     * View all notes with timestamps
     * Delete notes


CONTROLLERS & LOGIC
══════════════════════════════════════════════════════════════════

1. Nurse\ApplicationController@store()
   - Validates cv_document_id exists in database
   - Checks job posting is open
   - Prevents duplicate applications
   - Creates JobApplication with cv_document_id
   - Returns JSON or redirect with success message

2. Nurse\DocumentController (implied)
   - Handles document upload
   - Stores in storage/documents/
   - Creates nurse_documents record
   - Sets initial status to "uploaded"

3. Employer\ApplicationController (implied)
   - Fetches JobApplication with cvDocument eager-loaded
   - Passes to view with all relationships loaded
   - Allows status updates
   - Allows notes management


VALIDATION & ERROR HANDLING
══════════════════════════════════════════════════════════════════

Client Side (JavaScript/Blade):
- CV selection required (select dropdown disabled until chosen)
- File upload validation (type and size)
- Form submission prevention if CV not selected

Server Side (Laravel):
- cv_document_id must exist in nurse_documents table
- cv_document_id must belong to authenticated nurse
- Job posting must be open/published
- Duplicate application prevention (unique constraint)
- File storage with proper permissions


SEQUENCE DIAGRAM
══════════════════════════════════════════════════════════════════

Nurse                        Server                    Database
  │                            │                           │
  ├──1. Upload CV────────────→ │                           │
  │                            ├──2. Store file──────────→ │
  │                            │                           │
  │                            ├──3. Create record────────→ │
  │                            │     (nurse_documents)     │
  │                            ←──4. Success response──────┤
  │                            │                           │
  │                            │                           │
  ├──5. Click "Browse Jobs"──→ │                           │
  │                            ├──6. Query CV list────────→ │
  │                            │                           │
  │                            ←──7. Return CVs──────────┤
  │                            │                           │
  ├──8. Select CV + Letter──→  │                           │
  │                            ├──9. Validate cv_id──────→ │
  │                            │                           │
  │                            ├──10. Create app record──→ │
  │                            │      (job_applications)   │
  │                            ←──11. Success──────────────┤
  │ ←──12. Confirm submit──────┤                           │
  │                            │                           │
  │                            │                           │
Employer                       Server                    Database
  │                            │                           │
  ├──1. View applications───→  │                           │
  │                            ├──2. Query applications──→ │
  │                            │                           │
  │                            ←──3. Return list──────────┤
  │                            │                           │
  ├──4. Click application───→  │                           │
  │                            ├──5. Query app detail────→ │
  │                            │     WITH cvDocument       │
  │                            ├──6. Join nurse_documents→ │
  │                            │                           │
  │                            ←──7. Return full data────┤
  │ ←──8. Display CV+Letter───┤                           │
  │                            │                           │
  ├──9. Click "View Doc"────→  │                           │
  │                            ├──10. Retrieve file──────→ │
  │                            │                           │
  │                            ←──11. Return file─────────┤
  │ ←──12. Download/Open──────┤                           │


TESTING CHECKLIST
══════════════════════════════════════════════════════════════════

Nurse User Tests:
  ☑ Can upload CV to profile
  ☑ Can upload multiple documents
  ☑ Can see uploaded documents in list
  ☑ Can delete documents
  ☑ Can select CV when applying for job
  ☑ Cannot apply without selecting CV
  ☑ Can write optional cover letter
  ☑ Application saved successfully
  ☑ Receives confirmation message

Employer User Tests:
  ☑ Can see application list
  ☑ Can click to view application details
  ☑ Can see CV document section
  ☑ Can see CV filename
  ☑ Can see CV upload date
  ☑ Can click "View Document" button
  ☑ Document opens in new tab
  ☑ Can see cover letter text
  ☑ Can change application status
  ☑ Can add internal notes
  ☑ Can delete internal notes

Database Tests:
  ☑ cv_document_id column exists
  ☑ Foreign key constraint works
  ☑ cv_document_id saved when application created
  ☑ Relationship loads correctly
  ☑ Old applications show "No CV" message

Edge Cases:
  ☑ Nurse with no documents sees warning
  ☑ Missing CV shows appropriate message
  ☑ Missing cover letter doesn't break display
  ☑ Deleted document doesn't break application view
  ☑ File not found gracefully handled


DEPLOYMENT CHECKLIST
══════════════════════════════════════════════════════════════════

Pre-deployment:
  ☑ Run migrations: php artisan migrate
  ☑ Test file upload permissions on storage/
  ☑ Verify storage symlink exists
  ☑ Check max upload size in php.ini
  ☑ Test with multiple file types
  ☑ Verify error messages display correctly

Post-deployment:
  ☑ Monitor file storage usage
  ☑ Check application logs for errors
  ☑ Verify CVs are accessible to employers
  ☑ Test file downloads work
  ☑ Verify virus scanning (if applicable)
  ☑ Set up cleanup for deleted documents
```

---

## Feature Status Summary

| Component | Status | Notes |
|-----------|--------|-------|
| Database Schema | ✅ Complete | cv_document_id column exists |
| Nurse Upload UI | ✅ Complete | Type selector, validation, list |
| Job Application Form | ✅ Complete | CV selection required, cover letter optional |
| Application Detail Page | ✅ Complete | CV display with download link, cover letter |
| Backend Validation | ✅ Complete | Proper foreign key and existence checks |
| File Storage | ✅ Complete | Files stored in storage/documents/ |
| Error Handling | ✅ Complete | Graceful fallbacks for missing documents |
| User Experience | ✅ Complete | Clear messaging, professional styling |

---

## Next Steps (Optional Enhancements)

1. **CV in Applicants List**: Add CV preview/download to applicants list view
2. **Email Notifications**: Send CV to employer when application submitted
3. **Document Verification**: Admin interface to verify documents
4. **Document Expiration**: Set expiration dates for licenses/certs
5. **Document Signing**: Digital signature for certain documents
6. **CV Analytics**: Track which CVs get more interviews
7. **Cover Letter Templates**: Suggest template for nurses
8. **Document Search**: Full-text search in documents

