# Deploying Client Approval System to cPanel Production

## Quick Deployment Steps

### Option 1: Using Python Script (Recommended)

1. Upload the deployment script to your server
2. SSH into your server
3. Navigate to your project directory
4. Run:

```bash
python deploy_client_approval_to_production.py
```

This script will:
- Check which fields already exist
- Deploy missing fields using Django migrations or direct SQL
- Record the migration in django_migrations table

### Option 2: Direct SQL (Alternative)

If Django migrations don't work, you can run the SQL directly in phpMyAdmin:

1. Open phpMyAdmin in cPanel
2. Select your database
3. Go to SQL tab
4. Paste and run the contents of `deploy_to_cpanel.sql`
5. Verify fields were added

### Option 3: Django Management Commands

```bash
# Check migration status
python manage.py showmigrations users

# Try to apply migrations
python manage.py migrate users

# If it fails with conflicts, use fake migration
python manage.py migrate users 0023_add_client_approval_fields --fake
```

## What Gets Added

The following fields will be added to the `users` table:

```sql
- approval_reason (TEXT, NULL)
- approved_at (DATETIME, NULL)
- approved_by_id (CHAR(32), NULL, Foreign Key)
- rejection_reason (TEXT, NULL)
- rejected_at (DATETIME, NULL)
- rejected_by_id (CHAR(32), NULL, Foreign Key)
```

## Upload Required Files

Make sure these files are uploaded to production:

### Views & URLs
- `users/views.py` - Updated with approval views
- `users/urls.py` - Updated with approval routes

### Templates
- `templates/users/pending_clients.html` - NEW
- `templates/users/client_list.html` - Updated
- `templates/base.html` - Updated

### Migration
- `users/migrations/0023_add_client_approval_fields.py` - NEW

### Deployment Scripts
- `deploy_client_approval_to_production.py` - Recommended
- `deploy_to_cpanel.sql` - SQL fallback

## Verification Steps

After deployment, verify:

1. **Check Fields in Database**
   ```sql
   DESCRIBE users;
   ```
   Look for: `approved_by_id`, `approved_at`, `approval_reason`, `rejected_by_id`, `rejected_at`, `rejection_reason`

2. **Check Migration Recorded**
   ```sql
   SELECT * FROM django_migrations 
   WHERE app = 'users' AND name = '0023_add_client_approval_fields';
   ```

3. **Test in Browser**
   - Login as admin
   - Navigate to Clients page
   - Click "Pending Approvals" button
   - Verify it loads without errors

## Troubleshooting

### Error: "Duplicate column name 'approval_reason'"
**Solution**: Fields already exist. No action needed, deployment is complete.

### Error: "Conflicting migrations detected"
**Solution**: The production database has a different migration structure. Use the Python deployment script which handles this automatically.

### Error: "Unknown column 'users.approved_by_id'"
**Solution**: Fields were not added. Run the deployment script again or apply SQL manually.

### Pending Approvals Button Not Showing
**Solution**: 
1. Clear browser cache
2. Verify you're logged in as admin
3. Check file permissions on template files
4. Restart Python/application server

### Cannot Approve Clients
**Solution**:
1. Verify fields exist in database
2. Check error logs
3. Verify admin permissions
4. Test database connection

## Rollback (If Needed)

If you need to rollback:

```sql
-- Remove approval fields
ALTER TABLE `users` 
DROP COLUMN `approval_reason`,
DROP COLUMN `approved_at`,
DROP COLUMN `approved_by_id`,
DROP COLUMN `rejection_reason`,
DROP COLUMN `rejected_at`,
DROP COLUMN `rejected_by_id`;

-- Remove migration record
DELETE FROM django_migrations 
WHERE app = 'users' AND name = '0023_add_client_approval_fields';
```

## Post-Deployment Checklist

- [ ] All approval fields added to database
- [ ] Migration recorded in django_migrations
- [ ] Pending Approvals button visible on Clients page
- [ ] Can view pending clients list
- [ ] Can approve a client
- [ ] Can reject a client (with reason)
- [ ] Audit logs show approval/rejection actions
- [ ] Clients receive notifications
- [ ] No errors in logs

## Support

If you encounter issues:
1. Check application error logs
2. Check database for field existence
3. Verify Django migration status
4. Test with a fresh client registration

The deployment script will automatically detect your environment and use the best deployment method available.

