# Umami Website Creation Not Working - Debug Steps

## Issue
The Umami website is not being created during setup, resulting in `400 Bad Request` errors from the tracking script.

## Debug Steps

### 1. Check Backend Logs
Look at your backend console/terminal output when you access the setup page. You should see one of these messages:

**Success:**
```
INFO: Umami website ensured for dev domain localhost (ID: abc-123-def)
```

**Failure:**
```
ERROR: Auto-ensure Umami website (dev) failed: <error details>
```

### 2. Test Umami Connection Manually

Open a new PowerShell window and test the connection:

```powershell
# Test 1: Login to Umami
$body = @{
    username = "admin"
    password = "eevRQ6h3G@!c#y4A1T"
} | ConvertTo-Json

$response = Invoke-RestMethod -Uri "https://umami.tdvorak.dev/api/auth/login" `
    -Method Post `
    -ContentType "application/json" `
    -Body $body

Write-Output "Token received: $($response.token.Substring(0,50))..."

# Test 2: List existing websites
$token = $response.token
$headers = @{
    Authorization = "Bearer $token"
}

$websites = Invoke-RestMethod -Uri "https://umami.tdvorak.dev/api/websites" `
    -Headers $headers `
    -Method Get

Write-Output "Existing websites:"
$websites.data | Format-Table name, domain, id
```

### 3. Check Environment Variables

Verify your `.env` file has all required values:
```bash
UMAMI_URL=https://umami.tdvorak.dev
UMAMI_USERNAME=admin
UMAMI_PASSWORD=eevRQ6h3G@!c#y4A1T
UMAMI_WEBSITE_ID=  # Empty (will be auto-created)
APP_ENV=development  # Important: allows localhost
```

### 4. Restart Backend

After verifying `.env`, restart your backend:
```bash
# Stop current backend (Ctrl+C)
# Then restart
go run main.go
# or
./fotbal-club
```

### 5. Test Auto-Creation via API

After backend restart, test the endpoint directly:

```powershell
# Call the config endpoint to trigger auto-creation
Invoke-RestMethod -Uri "http://localhost:8080/api/v1/umami/config" `
    -Method Get

# Should return:
# {
#   "enabled": true,
#   "website_id": "abc-123-def",
#   "script_url": "https://umami.tdvorak.dev/script.js"
# }
```

### 6. Manual Creation via Admin Panel

If auto-creation fails, use the manual UI:

1. Login to your admin panel
2. Go to **Settings** → **Umami Analytics** tab
3. Fill in:
   - Name: "My Football Club"
   - Domain: "localhost" (for development)
4. Click "Vytvořit webovou stránku" (Create Website)
5. Copy the Website ID
6. Add to `.env`: `UMAMI_WEBSITE_ID=<the-id-you-got>`
7. Restart backend

## Common Issues

### Issue: "authentication failed"
**Cause:** Umami credentials are wrong
**Fix:** Verify UMAMI_USERNAME and UMAMI_PASSWORD in `.env` match your Umami admin credentials

### Issue: "domain already exists"
**Cause:** Website for "localhost" was created before
**Solution:** 
1. Login to Umami dashboard (https://umami.tdvorak.dev)
2. Find the existing website
3. Copy its ID
4. Add to `.env`: `UMAMI_WEBSITE_ID=<existing-id>`
5. Restart backend

### Issue: Silent failure (no logs)
**Cause:** Backend is not reaching the auto-creation code
**Fix:** 
1. Ensure you're calling `/api/v1/umami/config` (not just loading the page)
2. Check if `APP_ENV=development` in `.env`
3. Verify backend is running and accessible

### Issue: "400 Bad Request" from script.js
**Cause:** Website ID doesn't exist in Umami
**Fix:** This is the symptom, not the cause. Follow steps 1-5 above to create the website first.

## Next Steps

1. **Check backend logs** - This will tell you exactly what's failing
2. **Test PowerShell connection** - Verifies Umami credentials work
3. **Try manual creation** - Bypasses auto-creation issues
4. **Report back** - Share the error message from backend logs

## Expected Backend Log Output

When working correctly, you should see:
```
INFO: Successfully authenticated with Umami (new token issued)
INFO: Umami website ensured for dev domain localhost (ID: 12345678-abcd-1234-abcd-123456789012)
INFO: GET /api/v1/umami/config 200
```

When failing, you'll see:
```
ERROR: Auto-ensure Umami website (dev) failed: authentication failed with status 401
```
or
```
ERROR: Auto-ensure Umami website (dev) failed: failed to create website: <details>
```
