# Simple Answer: When Does Logo Attach to Profile?

## 🎯 Direct Answer

**Logo attaches to Profile when admin approves the request.**

NOT when:
- ❌ Vendor uploads the file
- ❌ Vendor creates ProfileUpdateRequest

YES when:
- ✅ Admin approves → `$profile->update()` is called → UploadMediaObserver attaches it

---

## 📊 Timeline

```
Time 1: Vendor uploads logo
┌─────────────────────────────────────────────┐
│ POST /api/media                             │
│ file: logo.jpg                              │
└─────────────────────────────────────────────┘
                    ↓
        Media Record Created
┌─────────────────────────────────────────────┐
│ media table:                                │
│ id: abc123                                  │
│ model_type: App\Models\Profile              │
│ model_id: NULL          ← NOT attached!     │
│ option: NULL                                │
│ is_attached: false      ← NOT attached!     │
└─────────────────────────────────────────────┘


Time 2: Vendor creates request
┌─────────────────────────────────────────────┐
│ POST /api/dashboard/vendor/                 │
│      profile-update-requests                │
│ {                                           │
│   "logo": "abc123"                          │
│ }                                           │
└─────────────────────────────────────────────┘
                    ↓
    ProfileUpdateRequest Created
┌─────────────────────────────────────────────┐
│ profile_update_requests table:              │
│ id: 1                                       │
│ vendor_id: 10                               │
│ profile_data: {"logo": "abc123"}            │
│ status: pending                             │
└─────────────────────────────────────────────┘
                    ↓
        Media Still NOT Attached!
┌─────────────────────────────────────────────┐
│ media table:                                │
│ id: abc123                                  │
│ model_id: NULL          ← Still NULL!       │
│ is_attached: false      ← Still false!      │
└─────────────────────────────────────────────┘


Time 3: Admin approves
┌─────────────────────────────────────────────┐
│ POST /api/dashboard/admin/                  │
│      profile-update-requests/1/approve      │
└─────────────────────────────────────────────┘
                    ↓
        Code Execution
┌─────────────────────────────────────────────┐
│ ProfileUpdateRequest::approve()             │
│                                             │
│ 1. request()->merge(['logo' => 'abc123'])  │
│ 2. $profile->update([...])                 │
│ 3. UploadMediaObserver triggered!          │
│ 4. Media attached to profile               │
└─────────────────────────────────────────────┘
                    ↓
        Media NOW Attached!
┌─────────────────────────────────────────────┐
│ media table:                                │
│ id: abc123                                  │
│ model_id: 5             ← Profile ID!       │
│ model_type: App\Models\Profile              │
│ option: logo            ← Logo field!       │
│ is_attached: true       ← NOW true!         │
└─────────────────────────────────────────────┘
```

---

## 🔑 Key Code

### In ProfileUpdateRequest::approve()

```php
public function approve(): void
{
    $profile = $this->vendor->profile;  // Get profile (id = 5)
    
    // THIS IS THE MAGIC LINE!
    // Put media ID into request so Observer can see it
    request()->merge(['logo' => $this->profile_data['logo']]);
    
    // When we update profile, Observer is triggered
    $profile->update([
        'commercial_register_number' => $this->profile_data['commercial_register_number']
    ]);
    
    // UploadMediaObserver::saved() runs automatically
    // It sees 'logo' in request
    // It attaches media to profile
}
```

### What UploadMediaObserver Does

```php
public function saved(Model $model): void
{
    // $model = Profile (id = 5)
    
    // Get media from request
    $mediaInput = request()->only(['logo', 'commercial_register_file']);
    // $mediaInput = ['logo' => 'abc123']
    
    foreach ($mediaInput as $option => $mediaId) {
        // $option = 'logo'
        // $mediaId = 'abc123'
        
        // Delete old logo
        Media::where('model_id', 5)
             ->where('option', 'logo')
             ->delete();
        
        // Attach new logo
        Media::find('abc123')->update([
            'model_id'    => 5,                    // Profile ID
            'model_type'  => 'App\Models\Profile',
            'option'      => 'logo',
            'is_attached' => true,
        ]);
    }
}
```

---

## 🎯 Summary

**Question:** When does logo attach to Profile or ProfileUpdateRequest?

**Answer:**

1. **Profile:** Logo attaches to Profile when admin approves
2. **ProfileUpdateRequest:** Logo NEVER attaches to ProfileUpdateRequest (it's just stored as JSON)

**Why?**
- ProfileUpdateRequest doesn't use MediaTrait
- ProfileUpdateRequest just stores media IDs in JSON
- Profile uses MediaTrait, so media can attach to it
- Attachment happens on approval when `$profile->update()` is called

---

## 📝 In Simple Terms

Think of it like this:

1. **Upload:** You take a photo and save it to your phone
2. **Create Request:** You write down the photo's name on a piece of paper
3. **Approval:** You actually set the photo as your profile picture

The photo only becomes your profile picture at step 3!
