# Multiple Service Periods Feature - Migration Guide

## Overview
The working hours system has been updated to support multiple service periods per day (e.g., breakfast and dinner services).

## What Changed

### Old Format (Single Period Per Day)
```json
{
  "saturday": {
    "open": "10:00",
    "close": "23:00",
    "is_closed": false
  }
}
```

### New Format (Multiple Periods Per Day)
```json
{
  "saturday": [
    {
      "open": "09:00",
      "close": "12:00",
      "is_closed": false,
      "name": "Kahvaltı"
    },
    {
      "open": "18:00",
      "close": "23:00",
      "is_closed": false,
      "name": "Akşam Servisi"
    }
  ]
}
```

## Backward Compatibility

✅ **Full backward compatibility is maintained!**

The system automatically detects and handles both formats:
- Old format data continues to work without any changes
- The `isOpenAt()` method works with both formats
- The `getServicePeriodsForDate()` method normalizes old format to new format
- Settings controller converts old format to new format on save

## Files Modified

### Backend
1. **app/Models/RestaurantSetting.php**
   - Updated `getDefaultWorkingHours()` to return new format
   - Enhanced `isOpenAt()` to handle both formats
   - Added `getServicePeriodsForDate()` helper method

2. **app/Services/AvailabilityService.php**
   - Updated `getAvailableTimeSlots()` to iterate through all service periods
   - Renamed `getWorkingHoursForDate()` to `getServicePeriodsForDate()`
   - Enhanced `isWithinWorkingHours()` to check all periods

3. **app/Http/Controllers/Admin/SettingsController.php**
   - Added normalization logic to convert old format to new format
   - Validates and processes both formats correctly

4. **app/Http/Controllers/Admin/RestaurantController.php**
   - Updated default settings creation to use new format

### Frontend
5. **resources/views/admin/settings/edit.blade.php**
   - Complete UI redesign for managing multiple service periods
   - Added "Add Service" button for each day
   - Added delete button for additional periods (first period cannot be deleted)
   - Added service name input field
   - Includes JavaScript for dynamic add/remove functionality

## How to Use

### Adding Multiple Service Periods

1. Go to Restaurant Settings page
2. For any day, click the "**+ Servis Ekle**" button
3. Fill in:
   - **Servis Adı**: Name of the service (e.g., "Kahvaltı", "Öğle Yemeği", "Akşam Servisi")
   - **Start Time**: Opening time for this service
   - **End Time**: Closing time for this service
4. Click "Ayarları Kaydet" to save

### Example Configurations

#### Restaurant with Breakfast and Dinner (Saturday)
- **Kahvaltı**: 09:00 - 12:00
- **Akşam Servisi**: 18:00 - 23:00

#### Restaurant with Lunch and Dinner (Weekdays)
- **Öğle Servisi**: 12:00 - 15:00
- **Akşam Servisi**: 18:00 - 23:00

#### 24-Hour Restaurant
- **Gece Servisi**: 00:00 - 06:00
- **Gündüz Servisi**: 06:00 - 24:00

## API Changes

### New Method: getServicePeriodsForDate()
```php
$periods = $restaurant->settings->getServicePeriodsForDate('2024-01-06');
// Returns array of active service periods for that date
```

### Enhanced Method: isOpenAt()
```php
// Works with both old and new formats
$isOpen = $restaurant->settings->isOpenAt('2024-01-06', '10:00');
```

## Testing

Run the test suite to verify everything works:
```bash
php artisan test --filter=RestaurantSettingTest
```

Tests cover:
- ✅ Default working hours structure
- ✅ Old format compatibility
- ✅ Multiple periods functionality
- ✅ Service period retrieval
- ✅ Backward compatibility

## Migration Path for Existing Data

**No action required!** Existing restaurants with old format data will continue to work. When you edit and save settings, they will be automatically converted to the new format.

If you want to manually migrate existing data, you can run this Artisan command (create if needed):

```php
// Example migration script
$restaurants = Restaurant::all();
foreach ($restaurants as $restaurant) {
    $settings = $restaurant->settings;
    $workingHours = $settings->working_hours ?? [];
    
    // Normalize each day
    foreach ($workingHours as $day => $hours) {
        if (!isset($hours[0])) {
            // Convert old format to new
            $workingHours[$day] = [
                [
                    'open' => $hours['open'] ?? '09:00',
                    'close' => $hours['close'] ?? '22:00',
                    'is_closed' => $hours['is_closed'] ?? false,
                    'name' => $hours['name'] ?? 'Ana Servis',
                ]
            ];
        }
    }
    
    $settings->update(['working_hours' => $workingHours]);
}
```

## Benefits

1. **Flexibility**: Support different service types (breakfast, lunch, dinner)
2. **Accurate Availability**: Time slots are generated only within actual service periods
3. **Better UX**: Customers see clear service period names when booking
4. **Backward Compatible**: No breaking changes for existing data
5. **Easy Management**: Simple UI to add/remove service periods

## Troubleshooting

### Issue: Old data not showing multiple periods
**Solution**: Edit and save the settings once to trigger automatic conversion.

### Issue: Time slots not appearing between periods
**Solution**: This is expected behavior. If you have gaps between services (e.g., 12:00-18:00), no slots will be shown during that time.

### Issue: Validation errors when saving
**Solution**: Ensure time format is H:i (e.g., "09:00", "18:00"). The system accepts 24-hour format only.

## Support

For questions or issues, please refer to the code comments in:
- `app/Models/RestaurantSetting.php`
- `app/Services/AvailabilityService.php`
- `resources/views/admin/settings/edit.blade.php`
