# Gallery Admin Page Fix

## Problem
The gallery admin page was showing "Chyba při obnově galerie" (Error refreshing gallery) when clicking the "Obnovit z Zonerama" button. No albums were displayed even though data was visible on the frontpage.

## Root Cause
1. **Missing Backend Endpoint**: The admin page was calling `/api/v1/admin/gallery/refresh` which didn't exist in the backend
2. **Single Data Source**: The admin page only loaded from `zonerama_profile.json`, which had `albums: null`
3. **Cache Data Issues**: The Zonerama cache files contained empty/invalid data

## Changes Made

### Backend Changes

#### 1. Added `RefreshFromZonerama` Method (`internal/controllers/gallery_controller.go`)
- New endpoint handler that:
  - Loads the Zonerama URL from settings database
  - Validates it's a Zonerama URL
  - Triggers `services.RefreshZoneramaNow()` in a goroutine (non-blocking)
  - Regenerates flat gallery files after successful fetch
  - Returns immediate response to avoid timeout

#### 2. Added Route (`internal/routes/routes.go`)
- Registered `POST /api/v1/admin/gallery/refresh` endpoint
- Maps to `galleryController.RefreshFromZonerama`

### Frontend Changes

#### 3. Enhanced Admin Page Resilience (`frontend/src/pages/admin/GalleryAdminPage.tsx`)
- Modified `fetchAlbums()` to load from **both** cache sources:
  - `zonerama_profile.json` (primary source)
  - `zonerama_albums.json` (fallback source)
- Filters out empty albums (albums without ID or title)
- Avoids duplicates when combining sources
- Matches the behavior of the frontpage `GallerySection.tsx`

## How It Works Now

1. **Admin clicks "Obnovit z Zonerama"**:
   - Frontend calls `POST /api/v1/admin/gallery/refresh`
   - Backend loads Zonerama URL from settings
   - Backend triggers async refresh in background
   - Returns success immediately

2. **Background Refresh Process**:
   - Fetches profile from Zonerama API (album metadata)
   - Fetches each individual album with photos
   - Saves to `zonerama_albums.json`
   - Updates `zonerama_profile.json`
   - Regenerates `gallery.json` and `zonerama_flat.json`

3. **Admin Page Loads Albums**:
   - Tries both `zonerama_profile.json` and `zonerama_albums.json`
   - Combines and deduplicates results
   - Filters out invalid/empty albums
   - Displays combined album list

## Testing

1. **Build the backend**:
   ```bash
   go build -o bin/fotbal-club.exe
   ```
   ✅ Compiles successfully

2. **Run the server and test**:
   - Navigate to `/admin/galerie`
   - Click "Obnovit z Zonerama"
   - Should show success message
   - Wait a few seconds and refresh page
   - Albums should appear (if Zonerama API responds correctly)

## Configuration Required

Ensure the Zonerama URL is configured in Settings:
1. Go to Admin → Settings
2. Set "Gallery URL" to your Zonerama profile URL
   - Example: `https://eu.zonerama.com/FKKofolaKrnov/1470757`
3. The URL must contain "zonerama.com"

## Known Issues

The current cache files (`zonerama_profile.json` and `zonerama_albums.json`) contain empty data, which suggests:
- The Zonerama external API might be experiencing issues
- The URL format might have changed
- Network connectivity issues

The "Refresh" button will trigger a fresh fetch from the Zonerama API, which should resolve this if the API is working correctly.

## Related Files

- `internal/controllers/gallery_controller.go` - Gallery endpoints
- `internal/routes/routes.go` - Route registration
- `internal/services/prefetch_service.go` - Zonerama fetch logic
- `frontend/src/pages/admin/GalleryAdminPage.tsx` - Admin UI
- `frontend/src/components/home/GallerySection.tsx` - Frontend display
