# CreateWorkSchedule

## Permission Scope

workSchedule

## Overview

createWorkSchedule opens the initial planned-time baseline for an Assignment. It records scheduled daily and weekly minutes without duplicating the employment regime held by `WorkerEmployment`.

## Business Rules

- The Assignment must exist and its generation must cover `effectiveStart`; a baseline cannot open on a date the person was not posted
- Every WorkSchedule references one Assignment
- `scheduledDailyMinutes` and `scheduledWeeklyMinutes` are required and non-negative
- The created generation has `effectiveEnd = null` and begins a new lineage
- No existing generation for the Assignment may overlap the open-ended generation being created

## Process Flow

```mermaid
flowchart TD
    A[Receive Assignment, planned minutes, effectiveStart] --> B{Required fields present?}
    B -- No --> BX[Return MISSING_REQUIRED_FIELD]
    B -- Yes --> C{Minutes non-negative?}
    C -- No --> CX[Return NEGATIVE_MINUTES]
    C -- Yes --> D{Overlapping generation exists?}
    D -- Yes --> DX[Return WORK_SCHEDULE_ALREADY_EXISTS]
    D -- No --> E[Insert open generation]
```

## External Dependencies

None — `assignmentId` references this module's own Assignment, read directly for the effective-date check.

## Error Scenarios

- **ASSIGNMENT_NOT_EFFECTIVE**: the referenced Assignment does not exist or is not effective on the relevant date
- **MISSING_REQUIRED_FIELD**: a required field is missing
- **NEGATIVE_MINUTES**: scheduledDailyMinutes or scheduledWeeklyMinutes is negative
- **WORK_SCHEDULE_ALREADY_EXISTS**: a generation already exists whose range overlaps the new open-ended generation

## Test Cases

- throws ASSIGNMENT_NOT_EFFECTIVE when the Assignment does not cover the effective date
- creates a schedule with planned minutes
- throws NEGATIVE_MINUTES when scheduled minutes are negative
- throws WORK_SCHEDULE_ALREADY_EXISTS when a generation overlaps the requested start
- rejects a backdated create while a later generation is still open
