# Corporate Phase 4 Cohorts Audit

## Scope

Phase 4 adds corporate cohorts, bulk trainees, sessions, and attendance on top of the already-established Organization, CorporateProposal, and CorporateAgreement foundation.

## Current Findings

### Official cohort model

Status: Partial, reusable.

Evidence:
- `app/Models/Cohort.php` maps to the existing `course_sessions` table.
- `app/Models/Session.php` also maps to `course_sessions` for legacy compatibility.
- `database/migrations/2026_01_07_015457_create_sessions_table.php` creates `course_sessions`.
- Existing admin cohort/session UI uses `App\Models\Session`.

Decision:
- Reuse `course_sessions` as the official cohort table.
- Do not create a separate `corporate_cohorts` table.

Reason:
- The platform already uses `course_sessions` for registrations, scheduling, teacher assignment, and attendance.
- Reusing it avoids duplicating scheduling and attendance logic.

### Session scheduling

Status: Mostly reusable.

Evidence:
- `app/Domain/Scheduling/SessionScheduleGenerator.php` can preview and generate sessions for `App\Models\Session`.
- Generated records are stored in `attendance_sessions`.
- Existing scheduling supports study days, one/two slots, period type, room, instructor resolution, and conflict checks.
- `app/Livewire/Admin/Cohorts/AdminCohortSessionsPage.php` already provides an admin UI for generating/editing sessions.

Gap:
- The current scheduling UI is reached via normal cohort routes only.
- Corporate cohort detail needs to link to/reuse the same generator for corporate cohorts.

### Attendance

Status: Public/student attendance exists; corporate trainee attendance missing.

Evidence:
- Public/student attendance uses `attendance_records` tied to `enrollments`.
- `app/Livewire/Teacher/TakeAttendancePage.php` loads confirmed `Enrollment` rows by `registration.session_id`.
- `app/Livewire/Instructor/AttendanceTakingPage.php` is a legacy attendance screen also based on registrations/enrollments.

Gap:
- Corporate trainees are not individual enrollments in this phase.
- A separate `corporate_attendance_records` table is needed to avoid breaking individual enrollment attendance.

### Organization employees and enrollments

Status: Existing, but not enough for Phase 4 trainees.

Evidence:
- `app/Models/OrgEmployee.php` exists and is organization-scoped.
- `app/Models/OrgEnrollment.php` exists for organization portal flows.

Decision:
- Keep `OrgEmployee` as a directory/contact model.
- Add `corporate_trainees` for cohort-specific participation.
- Link `corporate_trainees.org_employee_id` when a match exists, but do not force user-account creation.

### Agreements

Status: Ready for Phase 4 entry point.

Evidence:
- `app/Models/CorporateAgreement.php` has statuses including `active`.
- Agreements link to `organization_id`, `corporate_proposal_id`, and optional `b2b_lead_id`.
- Existing workflow makes agreements active only after signature workflow.

Rule:
- Corporate cohort creation must be allowed only from `CorporateAgreement::STATUS_ACTIVE`.

### Admin corporate cohorts UI

Status: Missing.

Needed:
- `/admin/corporate-cohorts`
- `/admin/corporate-agreements/{agreement}/cohorts/create`
- `/admin/corporate-cohorts/{cohort}`

### Bulk trainees

Status: Missing.

Needed:
- `corporate_trainees`
- `corporate_trainee_imports`
- CSV validator/import service.
- Preview before import confirmation.

### Organization portal

Status: Placeholder only.

Evidence:
- `app/Livewire/Org/OrgProgramsPage.php` renders `livewire.org.org-placeholder-page`.

Needed:
- Show selected organization corporate cohorts summary only.
- Keep details limited by organization tenancy and permissions.

## Reuse Plan

- Reuse `course_sessions` via `Cohort`/`Session`.
- Reuse `SessionScheduleGenerator` and `AdminCohortSessionsPage`.
- Reuse `AttendanceAssignmentService` for teacher access.
- Extend `Teacher\TakeAttendancePage` to branch into corporate trainees when `cohort_type=corporate`.
- Reuse RBAC/Spatie permission conventions from prior corporate phases.

## What Must Not Break

- Public enrollment and individual `Enrollment` attendance.
- Existing `Registration` and payment flows.
- Existing admin cohort/session scheduling.
- Existing teacher console access for assigned public cohorts.
- Organization tenancy selection and active membership rules.

## Implementation Phases

1. Add schema fields and corporate trainee/attendance/import tables.
2. Add model relations and services for creating corporate cohorts, validating/importing trainees, and marking corporate attendance.
3. Add admin UI and routes for corporate cohorts.
4. Reuse existing session scheduling route from corporate cohort detail.
5. Extend teacher attendance to support corporate trainees.
6. Add organization portal corporate cohort summary.
7. Add RBAC, policies, translations, tests, and documentation.
