# DeletePipelineStage

## Permission Scope

stage

## Overview

Deletes a stage from a pipeline. The stage must have no items assigned to it. Items must be moved to another stage before deletion.

## Business Rules

- PipelineStage must exist
- PipelineStage's pipeline must not be locked — locked pipelines keep the lane template seeded at creation
- PipelineStage must have no items currently assigned to it
- Remaining stages are unaffected (positions are not automatically re-ordered)

## Process Flow

```mermaid
flowchart TD
    A[Receive deletePipelineStage input] --> B{PipelineStage exists?}
    B -->|No| C[Return STAGE_NOT_FOUND]
    B -->|Yes| BB{Pipeline is locked?}
    BB -->|Yes| CC[Return PIPELINE_LOCKED]
    BB -->|No| D{PipelineStage has items?}
    D -->|Yes| E[Return STAGE_HAS_ITEMS]
    D -->|No| F[Delete stage]
    F --> G[Return deleted PipelineStage]
```

## External Dependencies

- None

## Error Scenarios

- **STAGE_NOT_FOUND**: Specified stage ID does not exist
- **PIPELINE_LOCKED**: Locked pipeline lanes are fixed to the template seeded at creation and cannot be modified
- **STAGE_HAS_ITEMS**: PipelineStage still contains items and cannot be deleted

## Test Cases

- deletes an empty stage
- returns error when stage not found
- returns error when pipeline is locked
- returns error when stage has items
