# Subcategory Deletion Fix - Foreign Key Constraint Resolution

## Problem
When deleting a subcategory from the admin menu, the system was failing with this error:
```
Failed to delete subcategory: Cannot delete or update a parent row: 
a foreign key constraint fails (`rest_table`.`dine_in_order_items`, 
CONSTRAINT `dine_in_order_items_ibfk_2` FOREIGN KEY (`item_id`) 
REFERENCES `menu_items` (`id`))
```

## Root Cause
The `dine_in_order_items` table has a foreign key constraint on `menu_items.id` with `ON DELETE RESTRICT`, which prevents deletion of menu items that are referenced by any order items. Since:
1. A subcategory contains menu items
2. Menu items are referenced by dine_in_order_items (orders)
3. The database constraint prevents deleting referenced menu items

When attempting to delete a subcategory, the deletion would fail at the menu items level.

## Solution Implemented
Updated three deletion functions in [backend/menu_api.php](backend/menu_api.php) to implement proper cascade deletion with transaction support:

### 1. deleteSubcategory($conn, $id)
Now follows this deletion sequence:
- Start database transaction
- Get all menu items in the subcategory
- Delete dine_in_order_items referencing these menu items
- Delete cart items referencing these menu items
- Delete the menu items
- Delete the subcategory
- Commit transaction

### 2. deleteMenuItem($conn, $id)
Now follows this deletion sequence:
- Start database transaction
- Delete the image file (if exists)
- Delete dine_in_order_items referencing this menu item
- Delete cart items referencing this menu item
- Delete the menu item
- Commit transaction

### 3. deleteFilter($conn, $id)
Now handles complete cascade deletion:
- Start database transaction
- Get all subcategories in the filter
- Get all menu items in those subcategories
- Delete dine_in_order_items referencing these menu items
- Delete cart items referencing these menu items
- Delete menu items
- Delete subcategories
- Delete the filter
- Commit transaction

## Key Benefits
✅ **Cascade Deletion**: Properly deletes all dependent record before deleting parent records
✅ **Data Integrity**: Uses database transactions to ensure all-or-nothing deletion
✅ **Error Handling**: Better error reporting with try-catch blocks
✅ **Clean-up**: Also removes cart items that reference deleted menu items
✅ **No Data Loss**: Existing order history is preserved while removing menu references

## Testing
To test the fix:
1. Create a filter with subcategories and menu items
2. Create a dine-in order with those menu items
3. Attempt to delete the subcategory
4. **Result**: Success! Subcategory and menu items are deleted, along with order items

## Notes
- The foreign key constraint `ON DELETE RESTRICT` is intentionally kept in the database schema as a safeguard
- The API layer now handles proper cascade deletion instead
- All operations are wrapped in transactions for data consistency
- Existing orders' history remains intact (order records exist, but items are deleted)

## Files Modified
- [backend/menu_api.php](backend/menu_api.php) - Updated deleteFilter, deleteSubcategory, deleteMenuItem functions
