# Fix Verification for Team Logo Issue

## Problem Summary
- **Issue**: Teams with stale `logoRef` values (`system-default/logo`) persist across force reseeds
- **Root Cause**: `clearExistingRacingData()` didn't clear `racing_teams` table
- **Impact**: API returns `/media/default/logo.png` instead of `/media/teams/{id}/logo`

## Fix Applied
Updated `adapters/bootstrap/SeedRacingData.ts` `clearExistingRacingData()` method to:

1. **Clear team join requests** - Before deleting teams
2. **Clear team memberships** - Before deleting teams  
3. **Clear teams** - The critical fix (teams have stale logoRef)
4. **Clear related racing data** - Results, standings, races, etc.

## Key Changes
```typescript
// Before (incomplete):
private async clearExistingRacingData(): Promise<void> {
    // Only cleared drivers and leagues
    // Missing: teams, team_memberships, team_join_requests
}

// After (complete):
private async clearExistingRacingData(): Promise<void> {
    // Clear stats
    await this.seedDeps.driverStatsRepository.clear();
    await this.seedDeps.teamStatsRepository.clear();
    
    // Clear race registrations
    const races = await this.seedDeps.raceRepository.findAll();
    for (const race of races) {
        await this.seedDeps.raceRegistrationRepository.clearRaceRegistrations(race.id.toString());
    }
    
    // Clear team join requests
    const teams = await this.seedDeps.teamRepository.findAll();
    for (const team of teams) {
        const joinRequests = await this.seedDeps.teamMembershipRepository.getJoinRequests(team.id.toString());
        for (const request of joinRequests) {
            await this.seedDeps.teamMembershipRepository.removeJoinRequest(request.id);
        }
    }
    
    // Clear team memberships
    for (const team of teams) {
        const memberships = await this.seedDeps.teamMembershipRepository.getTeamMembers(team.id.toString());
        for (const membership of memberships) {
            await this.seedDeps.teamMembershipRepository.removeMembership(team.id.toString(), membership.driverId.toString());
        }
    }
    
    // Clear teams (CRITICAL FIX)
    for (const team of teams) {
        await this.seedDeps.teamRepository.delete(team.id.toString());
    }
    
    // Clear other racing data...
    // Results, standings, races, league memberships, etc.
}
```

## Expected Behavior After Fix

### Before Fix:
1. Start dev with `GRIDPILOT_API_FORCE_RESEED=1`
2. Teams from previous seed remain with `logoRef: {"type":"system-default","variant":"logo"}`
3. `GET /teams/all` returns `"logoUrl": "/media/default/logo.png"`

### After Fix:
1. Start dev with `GRIDPILOT_API_FORCE_RESEED=1`
2. All racing data cleared including teams
3. New teams seeded with `logoRef: MediaReference.generated('team', teamId)`
4. `GET /teams/all` returns `"logoUrl": "/media/teams/{id}/logo"`

## Verification Steps

1. **Start with existing data**: Run API with force reseed enabled
2. **Check database**: Verify `racing_teams` table is cleared
3. **Verify new data**: Teams should have generated logoRef
4. **Test API**: `/teams/all` should return correct logo URLs

## Files Modified
- `adapters/bootstrap/SeedRacingData.ts` - Enhanced `clearExistingRacingData()` method

## Related Code
- `RacingTeamFactory.createTeams()` - Sets `logoRef: MediaReference.generated('team', teamId)`
- `AllTeamsPresenter.present()` - Derives `logoUrl` from `logoRef`
- `MediaController.debugResolve()` - Validates media resolution