# DeactivateUnit

## Permission Scope

unit

## Overview

DeactivateUnit disables a unit of measure from being used in new product assignments and quantity conversions while preserving all historical data. This command supports scenarios such as discontinuing a measurement unit or consolidating units within a category.

## Business Rules

- Target unit must exist in the system
- Target unit must be in Active status
- Reference unit cannot be deactivated - must first change reference unit to another active unit in the category
- Deactivating an already inactive unit returns success with no state change
- Historical transactions and products using this unit are not affected
- Deactivation prevents the unit from being used in new product assignments or conversions

## Process Flow

```mermaid
flowchart TD
    A[Receive deactivate request] --> B{Unit exists?}
    B -->|No| C[Return error: not found]
    B -->|Yes| D{Is reference unit?}
    D -->|Yes| E[Return error: cannot deactivate reference unit]
    D -->|No| F{Current status?}
    F -->|Inactive| G[Return success: already inactive]
    F -->|Active| H[Update status to Inactive]
    H --> I[Return deactivated unit]
```

## External Dependencies

- None

## Error Scenarios

- **UNIT_NOT_FOUND**: Specified unit ID does not exist
- **CANNOT_DEACTIVATE_REFERENCE_UNIT**: Attempting to deactivate the category's reference unit, must change reference unit first

## Test Cases

- throws when unit doesn't exist
- throws when attempting to deactivate reference unit
- returns unit unchanged when already inactive
- deactivates active non-reference unit
