# ═══════════════════════════════════════════════════════════════
# PHASE 2: APPLICATIONS & WAITING LISTS MODULE
# Module directory: /modules/applications/
# ═══════════════════════════════════════════════════════════════
# READ THE MASTER PROMPT FIRST.

## YOUR TABLES
applications, application_periods, application_documents, application_fees,
application_interviews, application_reviews, application_sponsors,
application_status_history, application_board_votes,
waiting_lists, waiting_list_entries, waiting_list_entry_status_history

## REFERENCE TABLES (read only)
members, membership_types, membership_categories, sys_nationalities,
sys_countries, sys_cities, auth_users, fee_types

## PAGES TO BUILD

### 1. Application Listing (`GET /applications`)
- DataTable: App#, Applicant Name, Phone, Type, Status, Priority, Submission Date, Assigned To, Actions
- Filters: Status, Membership Type, Priority, Date Range, Source
- Status summary cards above: Submitted, Under Review, Board Pending, Approved, Rejected

### 2. Create Application (`GET /applications/create`, `POST /applications`)
- Multi-step form or tabbed form:
  - Step 1: Personal Info (mirrors member fields)
  - Step 2: Contact & Address
  - Step 3: Employment & Education
  - Step 4: Membership Type Selection (shows fees, benefits, requirements)
  - Step 5: Sponsors (Select2 member lookup, minimum count per type)
  - Step 6: Document Upload (dynamic based on selected membership type)
  - Step 7: Declaration checkbox & submit
- Auto-generate application_number via SequenceGenerator
- Calculate total_fees_calculated based on membership_type_fees
- Create application_fees records
- Source: walk_in, online, referral, etc.

### 3. View Application (`GET /applications/{id}`)
- Full application details with status timeline
- Tabs: Details, Documents, Sponsors, Reviews, Interview, Board Vote, Fees
- Action buttons based on current status:
  - Submit (from draft)
  - Assign To (staff dropdown)
  - Start Review
  - Request Documents
  - Schedule Interview
  - Send to Board
  - Approve / Reject
  - Create Member (from approved)
- Each action shows a modal with reason/notes field

### 4. Application Review (`POST /api/applications/{id}/review`)
- Record in application_reviews
- Move status forward/back

### 5. Application Interview (`POST /api/applications/{id}/interview`)
- Schedule, record results, recommendation

### 6. Board Voting (`GET /applications/{id}/board-vote`)
- Show all board members
- Record votes (approve/reject/abstain)
- Calculate result

### 7. Create Member from Application (`POST /applications/{id}/create-member`)
- Call `sp_create_member_from_application`
- Redirect to new member profile

### 8. Application Periods Management
- CRUD for application_periods
- Open/close periods
- Track max_applications

### 9. Waiting List (`GET /waiting-list`)
- DataTable per membership type
- Columns: Queue#, Name, Phone, Registration Date, Priority, Status
- Actions: Offer, Promote, Remove, Move Up/Down
- Create entry form
- Auto-expire offers past deadline

### 10. Waiting List Configuration
- Per membership type: max entries, offer validity days, auto-promote toggle

## BUSINESS LOGIC
- Status machine: draft → submitted → under_review → documents_pending → interview_scheduled → interview_completed → board_pending → approved/rejected/waitlisted
- Cannot skip statuses
- Approved applications can create members (one-time operation)
- Application periods limit when applications can be submitted
- Waiting list queue management with priority ordering
- Sponsor endorsement validation

## DASHBOARD WIDGETS
1. **Pending Applications** — Count by status
2. **Waiting List Overview** — Entries per membership type

## PERMISSIONS
applications.view, applications.create, applications.review, applications.approve
waitlist.view, waitlist.manage