# DeleteAuditPolicy

## Permission Scope

auditPolicy

## Overview

DeleteAuditPolicy permanently removes an audit policy that is in DRAFT status. Only DRAFT policies can be deleted — ACTIVE and INACTIVE policies must be preserved for audit integrity. Deleting a policy does not affect any previously recorded audit entries.

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

## Business Rules

- Only policies in DRAFT status can be deleted
- ACTIVE and INACTIVE policies cannot be deleted
- Deleting a policy does not affect previously recorded audit entries
- Caller must hold `manageAuditPolicies` permission at the appropriate scope
- A caller with company-scoped `manageAuditPolicies` can only delete policies for their assigned companies
- A caller with global-scoped `manageAuditPolicies` can only delete global policies

## Process Flow

```mermaid
flowchart TD
    A[Receive delete 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 DRAFT status?}
    F -->|No| G[Return error: INVALID_STATE]
    F -->|Yes| H[Delete AuditPolicy and associated PolicyFieldRules]
    H --> I[Return success]
```

## External Dependencies

- None

## Error Scenarios

- **INSUFFICIENT_PERMISSION**: Caller lacks the required permission for this operation
- **UNAUTHORIZED**: Caller's permission scope does not cover the target resource
- **POLICY_NOT_FOUND**: Specified policy ID does not exist
- **INVALID_STATE**: Target entity is not in a valid status for this operation

## Test Cases

- deletes DRAFT policy
- deletes associated PolicyFieldRules when deleting policy
- does not affect previously recorded audit entries
- rejects deletion of ACTIVE policy
- rejects deletion of INACTIVE policy
- rejects when policy does not exist
- rejects when caller lacks manageAuditPolicies permission
- rejects when company-scoped caller deletes policy for a different company
- rejects when global-scoped caller deletes company-scoped policy
