# DeleteShiftPattern

## Permission Scope

shiftPattern

## Overview

deleteShiftPattern removes a シフトパターン that is no longer offered by planners. Because Shifts may reference a pattern by `shiftPatternId` to record how they were instantiated, deletion is rejected while any Shift still references the pattern, preserving the historical link between a Shift and its originating template.

## Business Rules

- A pattern may only be deleted if **no** Shift references it via `shiftPatternId`
- Deletion removes the pattern including its embedded `segments` (they have no independent existence outside their parent pattern)
- Deletion is permanent; there is no soft-delete or effective-dating for ShiftPattern (it is not effective-dated data, ADR-013)
- Once deleted, the pattern's `code` may be reused by a new pattern (no historical `code` reservation is implied by this model)

## Process Flow

```mermaid
flowchart TD
    A[Receive shiftPatternId] --> B{Pattern exists?}
    B -->|No| BX[Return error: SHIFT_PATTERN_NOT_FOUND]
    B -->|Yes| C{Any Shift references<br/>this shiftPatternId?}
    C -->|Yes| CX[Return error: SHIFT_PATTERN_IN_USE]
    C -->|No| D[Delete pattern with<br/>its embedded segments]
    D --> E[Return success]
```

## External Dependencies

- [shiftSchedule::Shift](../model/Shift.md) model - checked for any Shift still referencing this pattern via shiftPatternId

## Error Scenarios

- **SHIFT_PATTERN_NOT_FOUND**: no ShiftPattern exists for the given id
- **SHIFT_PATTERN_IN_USE**: At least one Shift still references this pattern; deletion is rejected

## Test Cases

- deletes a pattern with no referencing Shifts and cascades to its segments
- throws SHIFT_PATTERN_IN_USE when a Shift references the pattern via shiftPatternId
- throws SHIFT_PATTERN_NOT_FOUND when the pattern does not exist
- a deleted pattern's code can be reused by a subsequently created pattern
