# Category Product Auto-Update Feature

## Overview
When a subcategory is moved to a different parent category, all products belonging to that subcategory will automatically update their parent category reference.

## How It Works

### Database Structure
```
categories table:
- id
- parent_id (references categories.id)
- level (1 = parent, 2 = subcategory)

products table:
- id
- category_id (references parent category)
- sub_category_id (references subcategory)
```

### Automatic Update Logic

When a subcategory's `parent_id` is changed:
1. The system detects the change using Laravel's model events
2. Finds all products where `sub_category_id` matches the updated subcategory
3. Updates those products' `category_id` to the new parent category

## Example Scenario

### Before Update:
```
Categories:
- Electronics (id: 1, parent_id: null)
- Home & Garden (id: 2, parent_id: null)
- Smartphones (id: 10, parent_id: 1) // subcategory of Electronics

Products:
- iPhone 15 (category_id: 1, sub_category_id: 10)
- Samsung Galaxy (category_id: 1, sub_category_id: 10)
```

### After Moving Subcategory:
```sql
UPDATE categories SET parent_id = 2 WHERE id = 10;
-- Smartphones is now under Home & Garden
```

### Result:
```
Categories:
- Electronics (id: 1, parent_id: null)
- Home & Garden (id: 2, parent_id: null)
- Smartphones (id: 10, parent_id: 2) // NOW under Home & Garden

Products (automatically updated):
- iPhone 15 (category_id: 2, sub_category_id: 10) // category_id changed from 1 to 2
- Samsung Galaxy (category_id: 2, sub_category_id: 10) // category_id changed from 1 to 2
```

## Implementation Details

### Location
`app/Models/Category.php`

### Code
```php
protected static function booted(): void
{
    // When a category is updated
    static::updating(function (Category $category) {
        // Check if parent_id has changed
        if ($category->isDirty('parent_id')) {
            $oldParentId = $category->getOriginal('parent_id');
            $newParentId = $category->parent_id;
            
            // Update all products that have this category as sub_category_id
            // Set their category_id to the new parent_id
            if ($newParentId) {
                Product::where('sub_category_id', $category->id)
                    ->update(['category_id' => $newParentId]);
            }
        }
    });
}
```

## Benefits

1. **Data Consistency**: Products always have the correct parent category
2. **Automatic**: No manual intervention needed
3. **Efficient**: Uses a single bulk update query
4. **Transparent**: Happens automatically when updating categories

## Use Cases

- Reorganizing category structure
- Moving subcategories between parent categories
- Category hierarchy maintenance
- Product catalog restructuring

## Notes

- Only triggers when `parent_id` actually changes (uses `isDirty()`)
- Only updates if new `parent_id` is not null
- Uses bulk update for efficiency
- Happens within the same database transaction as the category update
