···11+# Architectural Issues & Tasks
22+33+## High Priority (Stability & Performance)
44+- [ ] **Fix Critical Memory Usage in `FlacService`**
55+ - **Location**: `src/flac/service.ts`
66+ - **Issue**: `readMetadata` uses `fs.readFile` to load entire audio files (often 20-100MB) into memory just to read headers.
77+ - **Task**: Implement partial reads (open file descriptor -> read buffer) to parse headers and Vorbis comments without loading the binary audio data.
88+99+- [ ] **Implement Pagination for API Endpoints**
1010+ - **Location**: `src/api.ts`
1111+ - **Issue**: `getAlbumList` and `getAllSongs` fetch the entire database table in a single query.
1212+ - **Task**: Add limit/offset or cursor-based pagination parameters to these endpoints and updated the database queries accordingly.
1313+1414+- [ ] **Optimize Library Synchronization**
1515+ - **Location**: `src/file-parser.ts` & `src/sync-library.ts`
1616+ - **Issue**: `readDirectory` and the sync stream load all file paths into memory before processing. `alreadyIndexed` loads the entire file table.
1717+ - **Task**: Refactor to use a streaming directory iterator and batched database lookups to handle large libraries (50k+ songs) without OOM.
1818+1919+## Medium Priority (Reliability & Maintainability)
2020+- [ ] **Standardize API Error Handling**
2121+ - **Location**: `src/api.ts`
2222+ - **Issue**: Inconsistent error handling. Some endpoints map errors explicitly, others rely on `runtime.runPromise` which may cause unhandled promise rejections or generic 500s for domain errors.
2323+ - **Task**: Create a standardized wrapper or middleware that executes Effects and maps specific `TaggedError` types to HTTP status codes (404, 400, etc.).
2424+2525+- [ ] **Decouple API from Database Logic**
2626+ - **Location**: `src/api.ts`
2727+ - **Issue**: API handlers contain `db.query` and `db.select` calls directly ("Active Record" pattern in controllers).
2828+ - **Task**: Extract database queries into a dedicated Repository layer or specific Service methods to enable unit testing without a database connection.
2929+3030+## Low Priority (Code Quality)
3131+- [ ] **Refactor Effect <-> Elysia Adapter**
3232+ - **Location**: `src/api.ts`
3333+ - **Issue**: Manual calls to `runtime.runPromise` in every handler create boilerplate.
3434+ - **Task**: specific utility helper to bind Effect workflows directly to Elysia handlers.
3535+3636+- [ ] **Externalize Configuration**
3737+ - **Location**: `src/index.ts`
3838+ - **Issue**: Server port `3003` is hardcoded.
3939+ - **Task**: Use Effect's `Config` module to make the port configurable via environment variables.