# Corporate Phase 4: Corporate Cohorts, Trainees, Sessions, Attendance

## Goal

Phase 4 turns an active corporate agreement into an operational training cohort:

`active agreement -> corporate cohort -> trainees -> sessions -> attendance -> Phase 5 reports/finance`

This phase does not create invoices, final PDF reports, or corporate finance automation.

## Data Model

The official cohort table is `course_sessions`.

Corporate fields added to `course_sessions`:
- `cohort_type`: `public` or `corporate`.
- `organization_id`: the owning Organization.
- `corporate_agreement_id`: the active agreement that authorizes the cohort.
- `corporate_proposal_id`: optional link to the commercial proposal.
- `corporate_request_id`: optional link to the original B2B lead.
- `corporate_contact_name`, `corporate_contact_phone_e164`, `corporate_notes`.
- `expected_trainees_count`.

New tables:
- `corporate_trainees`: cohort-specific participants, optionally linked to `org_employees` or `users`.
- `corporate_trainee_imports`: private CSV upload and validation history.
- `corporate_attendance_records`: attendance for corporate trainees, separate from individual enrollment attendance.

## Workflow Rules

- A corporate cohort can only be created from `CorporateAgreement::STATUS_ACTIVE`.
- The cohort is always linked to `organization_id` and `corporate_agreement_id`.
- Trainees are not forced to become `User` accounts in this phase.
- Public enrollment and `attendance_records` remain unchanged.
- Corporate attendance uses `corporate_attendance_records`.

## Admin Routes

- `/admin/corporate-cohorts`
- `/admin/corporate-agreements/{agreement}/cohorts/create`
- `/admin/corporate-cohorts/{cohort}`
- Existing scheduling is reused through `/admin/cohorts/{cohort}/sessions`.

## Import Rules

CSV columns:
- `full_name` required.
- `phone` optional, normalized as Algerian mobile number when present.
- `email` optional and validated.
- `job_title`, `employee_code`, `department` optional.

Validation:
- Maximum 500 rows per preview.
- Duplicate phone/email inside the same CSV is rejected.
- Duplicate phone/email inside the same cohort is rejected.
- Import files are stored on the private disk.

## Sessions

Corporate cohorts reuse the existing `SessionScheduleGenerator` and `attendance_sessions`.

This keeps:
- one or two slots per day,
- period type,
- room selection,
- instructor assignment,
- conflict prevention,
- teacher console visibility.

## Attendance

Teacher attendance now branches by cohort type:
- `public`: loads confirmed `Enrollment` rows and writes `attendance_records`.
- `corporate`: loads `corporate_trainees` and writes `corporate_attendance_records`.

This keeps individual enrollment attendance safe while supporting B2B operations.

## Organization Portal

`/org/programs` now shows only corporate cohorts belonging to the selected organization:
- cohort name,
- course,
- trainee count,
- session count,
- basic attendance count.

It does not expose detailed trainee data beyond the organization context.

## Permissions

Added permissions:
- `corporate_cohorts.view`
- `corporate_cohorts.create`
- `corporate_cohorts.update`
- `corporate_cohorts.cancel`
- `corporate_trainees.view`
- `corporate_trainees.manage`
- `corporate_trainees.import`
- `corporate_attendance.view`
- `corporate_attendance.mark`

Defaults:
- `super_admin` and `admin`: all.
- `support`: view corporate cohorts and trainees.
- `teacher`: view assigned cohorts and mark corporate attendance.
- organization members: organization-scoped summary access through tenancy.

## Phase 5 Remaining Work

- Corporate financial schedules and invoices.
- Exportable attendance/progress reports.
- Signed delivery reports.
- Optional trainee portal accounts.
