# Company Data Deletion Command

## Overview
The `company:delete-data` command safely deletes all data related to a specific company from both MySQL and Chex databases.

## Usage

### Basic Usage
```bash
php artisan company:delete-data {company_id}
```

### Options
- `--dry-run`: Show what would be deleted without actually deleting
- `--force`: Skip confirmation prompt
- `--connection={mysql|chex}`: Specify which database connection to use (default: mysql)

### Examples

#### Dry Run (Recommended First)
```bash
php artisan company:delete-data 123 --dry-run
```

#### Force Delete (Skip Confirmation)
```bash
php artisan company:delete-data 123 --force
```

#### Delete from Chex Database
```bash
php artisan company:delete-data 123 --connection=chex
```

#### Full Example with All Options
```bash
php artisan company:delete-data 123 --dry-run --connection=chex
```

## Deletion Strategy

The command follows a careful 4-step deletion process:

### Step 1: Direct Company Data
Deletes records from tables that have a direct `company_id` column:
- `company_insights`
- `teams`
- `users`
- `attendances`
- `daily_activities`
- `theme_settings`
- `work_patterns` (company type only)

### Step 2: Related Data Through Relationships
Deletes data that's related to the company's users and teams:
- **User-related**: `check_ins`, `attendance_logs`, `immunisations`, `team_users`
- **Team-related**: `team_users`, `company_insight_teams`
- **Media**: User avatars and company photos (morph relationships)

### Step 3: Orphaned Records
Cleans up any remaining orphaned records:
- Media records that no longer have a valid parent

### Step 4: Company Deletion
Finally deletes the company record itself.

## Safety Features

### Transaction Protection
All deletions are wrapped in database transactions to ensure atomicity.

### Confirmation Prompt
By default, the command asks for confirmation before proceeding (can be skipped with `--force`).

### Dry Run Mode
The `--dry-run` option shows exactly what would be deleted without actually deleting anything.

### Error Handling
- Comprehensive error logging
- Rollback on any failure
- Detailed error messages

## Database Connections

### MySQL (Default)
Contains the main application data:
- Companies, Users, Teams
- Attendance, Check-ins
- Company insights, etc.

### Chex
Contains secondary data:
- Task management data
- Additional company-related information

## Output Examples

### Dry Run Output
```
Company: Acme Corp (ID: 123)
Connection: mysql
DRY RUN MODE - No data will be deleted
Found 5 records in company_insights
Found 3 records in teams
Found 25 records in users
Found 150 records in attendances
Found 75 records in check_ins
Found 10 records in media_App\Models_User

=== DELETION SUMMARY ===
company_insights: 5 records
teams: 3 records
users: 25 records
attendances: 150 records
check_ins: 75 records
media_App\Models_User: 10 records

Total records: 268
This was a dry run. No actual data was deleted.
```

### Actual Deletion Output
```
Company: Acme Corp (ID: 123)
Connection: mysql
Found 5 records in company_insights
Found 3 records in teams
Found 25 records in users
Found 150 records in attendances
Found 75 records in check_ins
Found 10 records in media_App\Models_User

=== DELETION SUMMARY ===
company_insights: 5 records
teams: 3 records
users: 25 records
attendances: 150 records
check_ins: 75 records
media_App\Models_User: 10 records

Total records: 268
Company data deletion completed successfully
```

## Best Practices

1. **Always use dry-run first**: Verify what will be deleted before actual deletion
2. **Delete from both databases**: Run the command for both mysql and chex connections
3. **Backup before deletion**: Ensure you have recent backups before proceeding
4. **Test in staging**: Test the command in a staging environment first

## Error Recovery

If the command fails:
1. Check the error message for details
2. Review Laravel logs for full stack trace
3. The database transaction should have rolled back any partial deletions
4. Verify data integrity before re-running

## Notes

- The command respects soft deletes where applicable
- Media files on disk are not deleted (only database records)
- The command is idempotent - running it multiple times won't cause errors
- All deletions are logged for audit purposes
