# Global Sections Management Feature

## Overview

Added ability to manage multiple header/footer global sections with delete and set default functionality.

## Features Added

### 1. Default Section Marking

- **New Column**: `is_default` (boolean) added to `global_sections` table
- **Purpose**: Mark which header/footer section is currently active on the website
- **Badge Display**: Default sections show a blue "Default" badge in the UI

### 2. Delete Global Sections

- **Feature**: Users can delete global sections from the globals management page
- **Protection**:
  - Cannot delete if it's the only section of that type (must have at least 1 header and 1 footer)
  - If deleting the default section, automatically sets another section as default
- **UI**: Red trash icon button with confirmation dialog

### 3. Set Default Section

- **Feature**: Users can switch which header/footer is currently active
- **Button**: "Set Default" button with star icon (only shows for non-default sections)
- **Behavior**: Automatically unsets other sections of the same type when setting a new default

## Implementation Details

### Database Changes

**Migration**: `2025_11_03_114302_add_is_default_to_global_sections_table.php`

```sql
ALTER TABLE global_sections ADD is_default BOOLEAN DEFAULT FALSE;

-- Auto-set first non-custom section as default for each website/type
UPDATE global_sections gs1
INNER JOIN (
    SELECT MIN(id) as id, website_id, type
    FROM global_sections
    WHERE is_custom = 0
    GROUP BY website_id, type
) gs2 ON gs1.id = gs2.id
SET gs1.is_default = 1;
```

### Model Changes

**File**: `app/Models/GlobalSection.php`

- Added `is_default` to `$fillable` array
- Added `is_default` to `$casts` array (boolean)

### Routes Added

**File**: `routes/web.php` (lines 562-563)

```php
Route::delete('/globals/{global_section}', [GlobalSectionController::class, 'destroy'])->name('globals.destroy');
Route::post('/globals/{global_section}/set-default', [GlobalSectionController::class, 'setDefault'])->name('globals.set-default');
```

### Controller Methods

**File**: `app/Http/Controllers/GlobalSectionController.php` (lines 336-396)

#### `destroy()` Method (lines 339-373)

- Checks if user has permission
- Prevents deletion if it's the only section of that type
- Auto-sets another section as default if deleting the default
- Returns success/error message

#### `setDefault()` Method (lines 378-396)

- Checks if user has permission
- Unsets all other defaults of the same type
- Sets the selected section as default
- Returns success message

### View Changes

**File**: `resources/views/globals/index.blade.php`

**Changes**:
1. Added error alert display (lines 19-24)
2. Updated section display to show "Default" badge (lines 70-72)
3. Added button group with Edit, Set Default, and Delete buttons (lines 79-106)
4. Added JavaScript confirmation for delete (lines 139-146)
5. Updated help text to explain default sections (lines 133-134)

## User Interface

### Globals Management Page (`/websites/{id}/globals`)

**For each global section, users see**:
- Section name with icon
- "Default" badge if it's the default
- Type badge (Custom Header/Footer)
- Creation date
- Action buttons:
  - **Edit**: Edit the section content
  - **Set Default**: Make this section the default (only visible for non-default sections)
  - **Delete**: Remove the section (with confirmation)

### Example Display

```
[Icon] Header Name [Default Badge]
       [Custom Header] - Created 2 hours ago
       [Edit] [Set Default] [Delete]
```

## Usage Examples

### Setting a Different Header as Default

1. Go to `/websites/{id}/globals`
2. Find the header you want to use
3. Click "Set Default" button
4. The badge will move to the newly selected header

### Deleting a Duplicate Section

1. Go to `/websites/{id}/globals`
2. Find the duplicate section
3. Click the trash icon
4. Confirm deletion in the dialog
5. If it was the default, another section automatically becomes default

### Protection Against Invalid States

**Scenario 1**: Try to delete the only header
- **Result**: Error message "Cannot delete the only header section. At least one must exist."

**Scenario 2**: Delete the default footer when others exist
- **Result**: Footer deleted, and the next available footer is automatically set as default

## Related Files

1. **Migration**: `database/migrations/2025_11_03_114302_add_is_default_to_global_sections_table.php`
2. **Model**: `app/Models/GlobalSection.php` (lines 12-23)
3. **Controller**: `app/Http/Controllers/GlobalSectionController.php` (lines 336-396)
4. **Routes**: `routes/web.php` (lines 562-563)
5. **View**: `resources/views/globals/index.blade.php`

## Future Enhancements

Potential improvements:
1. Add ability to preview different headers/footers before setting as default
2. Add A/B testing capability for headers/footers
3. Add scheduling to switch headers/footers at specific times
4. Add analytics to track which header/footer performs better

## Testing

### Manual Test Cases

1. **Test Delete Protection**
   - Create website with only 1 header and 1 footer
   - Try to delete either → Should show error

2. **Test Default Switch**
   - Create 2 headers
   - Set second as default
   - Verify first header now shows "Set Default" button
   - Verify second header shows "Default" badge

3. **Test Delete Default**
   - Create 2 footers, one is default
   - Delete the default footer
   - Verify other footer automatically becomes default

4. **Test Multiple Sections**
   - Create 3 headers
   - Set different ones as default
   - Verify only one shows "Default" badge at a time

## Status

✅ **COMPLETE** - Feature fully implemented and ready to use

- Database migration: ✅ Run successfully
- Model updates: ✅ Complete
- Controller methods: ✅ Implemented
- Routes: ✅ Added
- View updates: ✅ Complete
- JavaScript: ✅ Implemented
- Testing: ⏳ Ready for user testing
