# Profile Update Request System Implementation

## Overview
This system allows vendors to submit profile update requests that require admin approval before being applied to their profile.

## Database Structure

### Table: `profile_update_requests`
- `id` - Primary key
- `vendor_id` - Foreign key to users table
- `profile_data` - JSON field containing profile updates (logo, commercial_register_number, commercial_register_file, translations)
- `location_data` - JSON field containing location updates (country_id, city_id, lat, lng, name, property_number, details, url)
- `status` - Enum: pending, approved, rejected (default: pending)
- `admin_note` - Text field for rejection reason
- `timestamps`

## Files Created

### Models
- `app/Models/ProfileUpdateRequest.php` - Main model with approve/reject methods
- `app/Enums/ProfileUpdateRequestStatus.php` - Status enum

### Controllers
- `app/Http/Controllers/Api/Dashboard/Admin/ProfileUpdateRequestController.php`
  - `index()` - List all requests with filters
  - `show()` - Show request details with current profile data for comparison
  - `approve()` - Approve request and update vendor profile
  - `reject()` - Reject request with reason
  - `destroy()` - Delete request

- `app/Http/Controllers/Api/Dashboard/Vendor/ProfileUpdateRequestController.php`
  - `index()` - List vendor's own requests
  - `show()` - Show request details
  - `store()` - Create new request (only one pending request allowed)
  - `destroy()` - Delete own pending request

### Resources
- `app/Http/Resources/Api/Dashboard/Admin/ProfileUpdateRequest/ProfileUpdateRequestResource.php`
- `app/Http/Resources/Api/Dashboard/Admin/ProfileUpdateRequest/ProfileUpdateRequestDetailsResource.php`
- `app/Http/Resources/Api/Dashboard/Vendor/ProfileUpdateRequest/ProfileUpdateRequestResource.php`
- `app/Http/Resources/Api/Dashboard/Vendor/ProfileUpdateRequest/ProfileUpdateRequestDetailsResource.php`

### Requests
- `app/Http/Requests/Api/Dashboard/Vendor/Profile/ProfileUpdateRequestRequest.php`
  - Validates logo, commercial_register_number, commercial_register_file
  - Validates location data (country_id, city_id, lat, lng, name, property_number, details, url)
  - Validates translations for all configured locales (name, description)

### Filters
- `app/Filter/ProfileUpdateRequestFilter.php`
  - Filter by status
  - Filter by vendor_id
  - Search by vendor name/email

### Migrations
- `database/migrations/2026_04_20_120000_create_profile_update_requests_table.php`

### Notifications
Added to `app/Services/General/NotificationMessages.php`:
- `adminProfileUpdateRequest()` - Notifies supervisors when vendor creates/updates request
- `vendorProfileUpdateRequest()` - Notifies vendor when request is approved/rejected

## API Routes

### Admin Routes (`/api/dashboard/admin/profile-update-requests`)
- `GET /` - List all requests
- `GET /{id}` - Show request details
- `POST /{id}/approve` - Approve request
- `POST /{id}/reject` - Reject request (requires `reason` field)
- `DELETE /{id}` - Delete request

### Vendor Routes (`/api/dashboard/vendor/profile-update-requests`)
- `GET /` - List own requests
- `GET /{id}` - Show request details
- `POST /` - Create new request
- `DELETE /{id}` - Delete own pending request

## Request Payload Example

```json
{
  "logo": 123,
  "commercial_register_number": "CR-123456",
  "commercial_register_file": 456,
  "ar": {
    "name": "اسم المتجر",
    "description": "وصف المتجر"
  },
  "en": {
    "name": "Store Name",
    "description": "Store Description"
  },
  "location": {
    "country_id": 1,
    "city_id": 5,
    "lat": 24.7136,
    "lng": 46.6753,
    "name": "Main Branch",
    "property_number": "1234",
    "details": "Building details",
    "url": "https://maps.google.com/..."
  }
}
```

## Workflow

1. **Vendor Creates Request**
   - Vendor submits profile update request via POST endpoint
   - System checks if vendor already has a pending request
   - Request is stored with status "pending"
   - Notification sent to all supervisors

2. **Admin Reviews Request**
   - Admin views list of pending requests
   - Admin can see both current profile data and requested changes
   - Admin can approve or reject

3. **Admin Approves**
   - Profile data is updated (logo, commercial_register_number, commercial_register_file, translations)
   - Location is updated or created
   - Status changed to "approved"
   - Notifications sent to vendor and supervisors

4. **Admin Rejects**
   - Admin provides rejection reason
   - Status changed to "rejected"
   - Notification sent to vendor with reason

## Notifications

### When Vendor Creates Request
- **To**: All supervisors
- **Type**: `profile-update-request`
- **Title**: "New profile update request"
- **Body**: "A vendor has submitted a profile update request."

### When Admin Approves
- **To**: Vendor + Supervisors
- **Type**: `profile-update-request`
- **Title**: "Profile update request approved"
- **Body**: "Your profile update request has been approved." (vendor) / "A profile update request has been approved." (supervisors)

### When Admin Rejects
- **To**: Vendor
- **Type**: `profile-update-request`
- **Title**: "Profile update request rejected"
- **Body**: {admin_note}

## Features

- ✅ Only one pending request per vendor at a time
- ✅ Vendor can only view/delete their own requests
- ✅ Admin can view all requests with filters
- ✅ Profile and location data stored as JSON for comparison
- ✅ Automatic profile update on approval
- ✅ Notifications to supervisors and vendors
- ✅ Rejection with reason
- ✅ Multi-language support for profile translations
- ✅ Location management (create or update)

## Next Steps

1. Run the migration:
   ```bash
   php artisan migrate
   ```

2. Test the endpoints using Postman or your API client

3. Add translations to language files for notification messages

4. Consider adding email notifications alongside push notifications
