# DeactivateAuditPolicy

## Overview

DeactivateAuditPolicy transitions an audit policy from ACTIVE to INACTIVE status, stopping new audit capture for the specified entity, operation type, and scope. Previously recorded audit entries are not affected. Events arriving for this entity + operation combination while no active policy exists will be silently discarded.

This command requires the `manageAuditPolicies` permission at the appropriate scope.

## Business Rules

- Only policies in ACTIVE status can be deactivated
- Deactivation stops new audit capture but does not delete previously recorded audit entries
- Events for the entity + operation + scope combination will be silently discarded while no ACTIVE policy exists
- Caller must hold `manageAuditPolicies` permission at the appropriate scope
- A caller with company-scoped `manageAuditPolicies` can only deactivate policies for their assigned companies
- A caller with global-scoped `manageAuditPolicies` can only deactivate global policies

## Process Flow

```mermaid
flowchart TD
    A[Receive deactivate request] --> B{Caller has manageAuditPolicies?}
    B -->|No| C[Return error: INSUFFICIENT_PERMISSION]
    B -->|Yes| D{Policy exists?}
    D -->|No| E[Return error: POLICY_NOT_FOUND]
    D -->|Yes| D2{Caller scope covers this policy?}
    D2 -->|No| D3[Return error: UNAUTHORIZED]
    D2 -->|Yes| F{Policy in ACTIVE status?}
    F -->|No| G[Return error: INVALID_STATE]
    F -->|Yes| H[Set status to INACTIVE]
    H --> I[Return deactivated policy]
```

## External Dependencies

- None

## Error Scenarios

- **INSUFFICIENT_PERMISSION**: Caller lacks the required command permission (shared error from defineCommand)
- **UNAUTHORIZED**: Caller's permission scope does not cover this policy (e.g., company-scoped caller managing a different company's policy, or global-scoped caller managing a company-scoped policy)
- **POLICY_NOT_FOUND**: Specified policy ID does not exist
- **INVALID_STATE**: Policy is not in ACTIVE status

## Test Cases

- deactivates ACTIVE policy to INACTIVE
- stops new audit capture after deactivation
- does not delete previously recorded audit entries
- rejects deactivation of DRAFT policy
- rejects deactivation of INACTIVE policy
- rejects when policy does not exist
- rejects when caller lacks manageAuditPolicies permission
- rejects when company-scoped caller deactivates policy for a different company
- rejects when global-scoped caller deactivates company-scoped policy
