# Soft Delete System Documentation

## Overview
The Payonline backend-v2 system implements comprehensive soft delete functionality for user management. This allows administrators to:
- Temporarily delete users (soft delete)
- View deleted users
- Restore deleted users
- Permanently delete users
- Get statistics about user deletions

## Available Endpoints

### 1. Get Users with Status Filter
**Endpoint:** `GET /api/user/admin/users`
**Parameters:**
- `status` (optional): `active` (default), `deleted`, `all`
- `search_phrase` (optional): Search by email or phone
- `kyc` (optional): Filter by KYC status

**Examples:**
```bash
# Get active users (default)
GET /api/user/admin/users

# Get only deleted users
GET /api/user/admin/users?status=deleted

# Get all users (active + deleted)
GET /api/user/admin/users?status=all

# Search deleted users
GET /api/user/admin/users?status=deleted&search_phrase=john@example.com
```

### 2. Get Deleted Users Only
**Endpoint:** `GET /api/user/admin/users/deleted`
**Parameters:**
- `search_phrase` (optional): Search by email or phone
- `kyc` (optional): Filter by KYC status

### 3. Get All Users Including Trashed
**Endpoint:** `GET /api/user/admin/users/all-with-trashed`
**Parameters:**
- `search_phrase` (optional): Search by email or phone
- `kyc` (optional): Filter by KYC status
- `status` (optional): `active`, `deleted`, or `all`

### 4. Show Specific User
**Endpoint:** `GET /api/user/admin/users/{id}`
**Description:** Get a specific user by ID (works for both active and deleted users)
**Response includes:**
- User details with relationships
- KYC information
- Deletion status information

### 5. Soft Delete User
**Endpoint:** `DELETE /api/user/admin/users/{id}/soft-delete`
**Description:** Soft delete a user (admin action)

### 6. Restore User
**Endpoint:** `POST /api/user/admin/users/{id}/restore`
**Description:** Restore a soft-deleted user

### 7. Permanently Delete User
**Endpoint:** `DELETE /api/user/admin/users/{id}/permanent-delete`
**Description:** Permanently delete a soft-deleted user (irreversible)
**⚠️ Warning:** This action cannot be undone

### 8. Get User Statistics
**Endpoint:** `GET /api/user/admin/users/statistics`
**Description:** Get statistics about user deletions
**Response:**
```json
{
    "status": "success",
    "message": "User statistics retrieved successfully!",
    "data": {
        "active_users": 150,
        "deleted_users": 25,
        "total_users": 175,
        "deletion_rate": 14.29
    }
}
```

### 9. User Self-Delete (Existing)
**Endpoint:** `DELETE /api/user/delete`
**Description:** Allows authenticated users to soft delete their own account

## Database Schema

The system uses Laravel's `SoftDeletes` trait which adds:
- `deleted_at` timestamp column to the users table
- Automatic filtering of deleted records in queries
- Methods for working with trashed records

## Key Features

### 1. Soft Delete Behavior
- Deleted users are not actually removed from the database
- They are marked with a `deleted_at` timestamp
- By default, queries exclude soft-deleted records
- Relationships are preserved

### 2. Query Scopes
- `User::onlyTrashed()` - Only deleted users
- `User::withTrashed()` - Both active and deleted users
- Default queries automatically exclude deleted users

### 3. Restoration
- Soft-deleted users can be restored
- All relationships remain intact
- `deleted_at` timestamp is set to null

### 4. Permanent Deletion
- Force delete permanently removes records
- This action is irreversible
- Should be used with extreme caution

## Security & Permissions

All soft delete management endpoints require:
- Authentication (`auth:sanctum`)
- Admin privileges (`AdminMiddleware`)

## Logging

The system logs important soft delete operations:
- User deletions (with admin ID)
- User restorations (with admin ID)
- Permanent deletions (with warnings)
- Errors during operations

## Frontend Integration

### Status Indicators
When displaying users, check the `deletion_info` in the response:
```json
{
    "deletion_info": {
        "is_deleted": true,
        "deleted_at": "2023-10-22T10:30:00.000000Z",
        "can_restore": true,
        "can_permanently_delete": true
    }
}
```

### UI Recommendations
1. **User List:**
   - Add status filter dropdown (Active, Deleted, All)
   - Show deleted users with visual indicators (e.g., grayed out)
   - Display deletion timestamp

2. **Action Buttons:**
   - For active users: "Delete" button (soft delete)
   - For deleted users: "Restore" and "Permanently Delete" buttons
   - Confirmation dialogs for destructive actions

3. **Statistics Dashboard:**
   - Show user statistics from the statistics endpoint
   - Display deletion rate as a metric

## Error Handling

The system handles various error scenarios:
- User not found (404)
- Already deleted users
- Permission denied (403)
- Server errors (500)

All errors are logged with detailed information for debugging.

## Best Practices

1. **Always use soft delete first** - Reserve permanent deletion for extreme cases
2. **Implement confirmation dialogs** - Especially for permanent deletions
3. **Log all admin actions** - For audit trails
4. **Regular cleanup** - Consider archiving very old deleted records
5. **Backup before permanent deletion** - For additional safety

## Migration Information

The soft delete functionality was added via migration:
`2023_08_27_144518_add_soft_delete_to_users_table.php`

This migration adds the `deleted_at` timestamp column to the users table.
