# Catalog ctmdb.* Worker 404 Fix

## Symptom
```
Catalog service HTTP failure for collections/ctmdb.newReleases: 404
Catalog service HTTP failure for collections/ctmdb.popular: 404
Catalog service HTTP failure for collections/ctmdb.topRated: 404
```

## Root Cause
The `ctmdb.*` catalog IDs (from the TMDB Collections Stremio addon) were routed through the Cloudflare catalog worker (`nexstream-catalog.tw24fr.workers.dev`) with `type="collections"`. The worker's route regex only matched `movie|series|tv`, not `collections`. Even after adding `collections` to the regex, the worker's `fetchCatalogWithKey` switch statement didn't handle `ctmdb.*` IDs — returning empty results. The catalogs went through Tier 2 (cloud worker) instead of Tier 1 (local TMDB) because `ctmdb.` wasn't recognized as a native catalog prefix.

## Fix (Android-side — no deploy needed)

### 1. Add `ctmdb.` to native catalog checks
In `AddonRepository.kt`:

**Tier 1 check** (line ~488):
```kotlin
if (catalogId.startsWith("tmdb.") || catalogId.startsWith("ctmdb.") || catalogId.startsWith("mdblist.")) {
```

**isNativeCatalogId** (line ~953):
```kotlin
private fun isNativeCatalogId(catalogId: String): Boolean =
    catalogId.startsWith("tmdb.") ||
        catalogId.startsWith("ctmdb.") ||  // ADDED
        catalogId.startsWith("trakt.") ||
        catalogId.startsWith("anilist.") ||
        catalogId.startsWith("mdblist.")
```

### 2. Normalize `ctmdb.` → `tmdb.` prefix
In `TmdbCatalogRepository.kt` `normalizeCatalogId()`:
```kotlin
private fun normalizeCatalogId(catalogId: String): String = when (catalogId) {
    // ... existing mappings ...
    else -> catalogId.replaceFirst("ctmdb.", "tmdb.")  // was: else -> catalogId
}
```

### Why this works
- `ctmdb.newReleases` → `tmdb.new_releases` → mapped to `tmdb.recent_releases` in existing normalization
- `ctmdb.popular` → `tmdb.popular` — direct match in `fetchCatalogWithKey`
- `ctmdb.topRated` → `tmdb.top_rated` — direct match
- Local TMDB API is faster than cloud worker proxy (no network hop)

### Worker fix (safety net)
Also add `collections` to worker regex in `catalog-worker/src/index.ts`:
```typescript
const match = url.pathname.match(/^\/v1\/catalog\/(movie|series|tv|collections)\/([^/]+)$/);
```
And `CatalogType`:
```typescript
export type CatalogType = 'movie' | 'series' | 'tv' | 'collections';
```

### Flow after fix
```
ctmdb.newReleases → Tier 1 (local TMDB) → TmdbCatalogRepository → normalize → tmdb.recent_releases → fetchCatalogWithKey → /discover/movie
```
