# Contact System Fix - Complete Summary

## Issue Identified
The contact system was returning 500 Internal Server Error due to missing database tables:
- `contacts` table
- `contact_categories` table

## Root Cause
The database migration `000006_create_contact_tables.up.sql` had not been applied to the running database, causing all contact-related endpoints to fail.

## Solution Applied

### 1. Database Tables Created
Executed the following SQL to create missing tables:

```sql
-- Contact Categories Table
CREATE TABLE contact_categories (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL UNIQUE,
    description TEXT,
    display_order INT DEFAULT 0,
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

-- Contacts Table  
CREATE TABLE contacts (
    id SERIAL PRIMARY KEY,
    category_id INT REFERENCES contact_categories(id) ON DELETE SET NULL,
    name VARCHAR(255) NOT NULL,
    position VARCHAR(255),
    email VARCHAR(255),
    phone VARCHAR(100),
    image_url VARCHAR(500),
    description TEXT,
    display_order INT DEFAULT 0,
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

-- Settings Table Extensions (for map and address)
ALTER TABLE settings ADD COLUMN IF NOT EXISTS contact_address VARCHAR(500);
ALTER TABLE settings ADD COLUMN IF NOT EXISTS contact_city VARCHAR(255);
ALTER TABLE settings ADD COLUMN IF NOT EXISTS contact_zip VARCHAR(20);
ALTER TABLE settings ADD COLUMN IF NOT EXISTS contact_country VARCHAR(100);
ALTER TABLE settings ADD COLUMN IF NOT EXISTS contact_phone VARCHAR(100);
ALTER TABLE settings ADD COLUMN IF NOT EXISTS contact_email VARCHAR(255);
ALTER TABLE settings ADD COLUMN IF NOT EXISTS location_latitude DECIMAL(10, 8);
ALTER TABLE settings ADD COLUMN IF NOT EXISTS location_longitude DECIMAL(11, 8);
ALTER TABLE settings ADD COLUMN IF NOT EXISTS map_zoom_level INT DEFAULT 15;
ALTER TABLE settings ADD COLUMN IF NOT EXISTS map_style VARCHAR(255);
ALTER TABLE settings ADD COLUMN IF NOT EXISTS show_map_on_homepage BOOLEAN DEFAULT FALSE;
```

### 2. Verified Components

#### Backend Components ✅
- **ContactInfoController** (`internal/controllers/contact_info_controller.go`)
  - Public endpoints: GET `/api/v1/contacts`, GET `/api/v1/contacts/categories`
  - Admin endpoints: Full CRUD for contacts and categories
  
- **ContactController** (`internal/controllers/contact_controller.go`)
  - Contact form submission: POST `/api/v1/contact`
  - Newsletter subscription: POST `/api/v1/newsletter/subscribe`

- **Routes** (`internal/routes/contact_info_routes.go`)
  - All routes properly registered and secured with middleware

- **Models** (`internal/models/models.go`, `internal/models/contact.go`)
  - Contact
  - ContactCategory
  - ContactMessage

#### Frontend Components ✅
- **ContactsAdminPage** (`frontend/src/pages/admin/ContactsAdminPage.tsx`)
  - Manage contacts and categories
  - Map configuration
  - Address settings

- **ContactPage** (`frontend/src/pages/ContactPage.tsx`)
  - Public contact form
  - Display contact information
  - Show map if configured

- **ContactsSection** (`frontend/src/components/home/ContactsSection.tsx`)
  - Display contacts grouped by category on homepage
  - Show map and address information
  - Collapsible accordion design

- **ContactMap** (`frontend/src/components/home/ContactMap.tsx`)
  - Interactive Leaflet map
  - Support for multiple map styles

### 3. API Endpoints

#### Public Endpoints (No Auth Required)
| Method | Endpoint | Description | Status |
|--------|----------|-------------|--------|
| GET | `/api/v1/contacts` | Get all active contacts grouped by category | ✅ Working |
| GET | `/api/v1/contacts/categories` | Get all active contact categories | ✅ Working |
| POST | `/api/v1/contact` | Submit contact form | ✅ Working |

#### Admin Endpoints (Auth Required)
| Method | Endpoint | Description | Status |
|--------|----------|-------------|--------|
| GET | `/api/v1/admin/contacts` | List all contacts | ✅ Working |
| POST | `/api/v1/admin/contacts` | Create new contact | ✅ Working |
| PUT | `/api/v1/admin/contacts/:id` | Update contact | ✅ Working |
| DELETE | `/api/v1/admin/contacts/:id` | Delete contact | ✅ Working |
| GET | `/api/v1/admin/contacts/categories` | List all categories | ✅ Working |
| POST | `/api/v1/admin/contacts/categories` | Create category | ✅ Working |
| PUT | `/api/v1/admin/contacts/categories/:id` | Update category | ✅ Working |
| DELETE | `/api/v1/admin/contacts/categories/:id` | Delete category | ✅ Working |

### 4. Features Available

#### For Admin Users
1. **Contact Management**
   - Create/Edit/Delete contacts
   - Upload contact photos
   - Assign contacts to categories
   - Set display order
   - Toggle active/inactive status

2. **Category Management**
   - Create/Edit/Delete categories (e.g., "Management", "Coaches", "Staff")
   - Organize contacts by role
   - Set display order

3. **Map & Address Configuration**
   - Set club address details
   - Configure map coordinates (latitude/longitude)
   - Import coordinates from Google Maps, Mapy.cz, or OpenStreetMap links
   - Set map zoom level
   - Choose map style (default, dark, satellite)
   - Toggle map visibility on homepage

4. **Contact Message Management**
   - View messages submitted through contact form
   - Mark as read/unread
   - Respond to inquiries

#### For Public Users
1. **Contact Page** (`/contact`)
   - Submit contact form
   - View contact information
   - See club location on interactive map

2. **Homepage Contact Section**
   - View all active contacts grouped by category
   - Click to email or call contacts
   - See club address and map

### 5. Data Flow

```
Public User → Contact Form → POST /api/v1/contact
                                ↓
                        Save to contact_messages table
                                ↓
                        Send email notification (async)
                                ↓
                        Return success message
```

```
Admin User → Contacts Admin Page → POST /api/v1/admin/contacts
                                      ↓
                              Save to contacts table
                                      ↓
                              Return contact with category
```

### 6. Verification Steps

To verify everything is working:

1. **Access Admin Panel**
   ```
   http://localhost:3000/admin/contacts
   ```

2. **Create a Contact Category**
   - Click "Přidat kategorii"
   - Enter name (e.g., "Vedení")
   - Save

3. **Create a Contact**
   - Click "Přidat kontakt"
   - Fill in details
   - Select category
   - Save

4. **Configure Map**
   - Go to "Mapa a adresa" tab
   - Enter address details
   - Use "Importovat z odkazu" to paste a map link
   - Save settings

5. **Test Public View**
   - Visit homepage: http://localhost:3000
   - Scroll to contact section
   - Verify contacts and map display

6. **Test Contact Form**
   - Visit: http://localhost:3000/contact
   - Fill out form
   - Submit
   - Check Admin Panel → Messages for the submission

### 7. Current Status

✅ **All Fixed!**
- Database tables created
- All API endpoints returning 200 status
- No more 500 errors
- Frontend components render without errors
- Contact form functional
- Admin panel operational

### 8. Next Steps (Optional Enhancements)

- [ ] Add contact form CAPTCHA to prevent spam
- [ ] Add email templates customization
- [ ] Add bulk import for contacts
- [ ] Add contact search/filter in admin
- [ ] Add QR code generation for contact cards
- [ ] Add vCard export functionality

## Maintenance Notes

If you need to reset the database in the future, run:
```bash
docker exec myclub-db psql -U postgres -d fotbal_club -f /path/to/000006_create_contact_tables.up.sql
```

Or manually recreate tables using the SQL provided in section 1 above.
