# Findings: Backend Integration Research

## Date: 2026-01-21

## Project Structure
- **Package Manager:** bun
- **Base URL:** https://api.mixvideo.bowong.cc
- **Owner ID:** t0m9cketSQdCA6cHXI9mXQLJPM9LDIw5
- **SDK Location:** node_modules/@repo/sdk

## @repo/sdk Analysis

### Architecture
- **Pattern:** Dependency Injection with Better Auth integration
- **Controllers:** 23 total (Activity, Template, Project, Chat, File, etc.)
- **Access Methods:**
  1. Better Auth style: `authClient.loomart.xxx`
  2. DI style: `root.get(Controller)`

### Key Controllers
1. **TemplateController** - Template CRUD, workflow execution
2. **CategoryController** - Category management with templates
3. **ProjectController** - Project management
4. **ChatController** - Chat functionality
5. **FileController** - File management
6. **ActivityController** - Activity tracking
7. **TemplateSocialController** - Likes, views, social features
8. **TemplateGenerationController** - Generation tracking
9. **AICharacterController** - AI character management
10. **AigcController** - AIGC operations

## Current State Management

### Zustand Stores (Only 1)
- **useCategoriesStore** (hooks/use-categories.ts)
  - Global cache for categories
  - Has loading/error/hasLoaded states
  - Prevents redundant API calls

### Custom Hooks Pattern (15 hooks)
Most screens use local state via custom hooks:
- use-templates.ts - Template list with pagination
- use-template-detail.ts - Single template
- use-template-actions.ts - Like, view actions
- use-template-generations.ts - Generation history
- use-activates.ts - Activity list
- use-tags.ts - Tag management
- use-search-history.ts - Search history

## API Integration Patterns

### Pattern 1: Better Auth Client (Primary)
```typescript
import { authClient, loomart } from '@/lib/auth'
const result = await loomart.template.list(params)
```

### Pattern 2: DI Container
```typescript
import { root } from '@repo/core'
import { TemplateController } from '@repo/sdk'
const template = root.get(TemplateController)
const result = await template.list(params)
```

### Pattern 3: Error Handling
```typescript
const { data, error } = await handleError(async () => await apiCall())
```

## Frontend Screens Inventory

### Tab Screens (5)
| Screen | Path | Needs API | SDK Support | Notes |
|--------|------|-----------|-------------|-------|
| Home | (tabs)/index.tsx | ✅ Categories, Templates | ✅ TemplateController, CategoryController | Has basic integration |
| Video | (tabs)/video.tsx | ✅ Video list | ✅ TemplateController | Already uses useTemplates |
| Message | (tabs)/message.tsx | ✅ Chat/messages | ❌ No list API | ChatController只有chat()和listModels()，没有消息列表接口 |
| My Profile | (tabs)/my.tsx | ✅ User data | ⚠️ 需确认 | 需要用户信息接口 |

### Main Screens (15)
| Screen | Path | Needs API | Notes |
|--------|------|-----------|-------|
| Auth | auth.tsx | ✅ Login/signup | Has Better Auth |
| Membership | membership.tsx | ✅ Subscription | Needs Stripe |
| Template Detail | templateDetail.tsx | ✅ Template data | Has integration |
| Generate Video | generateVideo.tsx | ✅ Generation API | Needs progress |
| Generation Record | generationRecord.tsx | ✅ History list | Needs pagination |
| Works List | worksList.tsx | ✅ User works | Needs pagination |
| Search Template | searchTemplate.tsx | ✅ Search API | Needs debounce |
| Search Results | searchResults.tsx | ✅ Results list | Needs pagination |
| Search Works | searchWorks.tsx | ✅ Works search | Needs pagination |
| Search Works Results | searchWorksResults.tsx | ✅ Results list | Needs pagination |
| Channels | channels.tsx | ✅ Category data | Needs refresh |
| Change Password | changePassword.tsx | ✅ Auth API | Has integration |
| Privacy | privacy.tsx | ❌ Static | No API needed |
| Terms | terms.tsx | ❌ Static | No API needed |

## Key Discoveries

1. **Minimal Global State:** Only 1 Zustand store (categories), rest uses local state
2. **Hook-Based Architecture:** 15 custom hooks handle API calls
3. **Existing Patterns:** Some screens already have API integration
4. **Missing Features:**
   - Loading states inconsistent across screens
   - No pull-to-refresh on most screens
   - Pagination exists in hooks but not all screens use it
   - No error retry mechanisms
   - No optimistic updates
5. **SDK限制：**
   - ❌ ChatController没有消息列表接口（只有chat()和listModels()）
   - ❌ 没有用户信息相关的Controller
   - ✅ Video页面已正确使用TemplateController（不需要单独的视频hook）

## Technical Constraints

1. **Multi-tenant:** All requests need `x-ownerid` header
2. **Authentication:** Token stored in expo-secure-store
3. **Design Width:** 375px (need to convert styles to Tailwind)
4. **No Redux:** Project uses minimal global state approach
5. **Better Auth:** Primary auth method, SDK integrated as plugin
