# InsuRo AI 주제 추천 — Edge Function → 아누 서버 전환

## 작업 레벨: Lv.2

## 프로젝트
- InsuRo: `/home/jay/projects/InsuRo`
- 서버: `/home/jay/projects/InsuRo/server`

## 문제
"AI 주제 추천" 기능이 Supabase Edge Function(`suggest-topics`)을 호출하며, 내부적으로 Gemini API(`gemini-2.0-flash-lite`)를 사용. GOOGLE_AI_API_KEY 미설정으로 에러 발생.

## 수정 사항

### 1. 서버에 suggest-topics 엔드포인트 추가
`server/main.py`에 새 엔드포인트:

```python
@app.post("/api/insuro/suggest-topics")
@limiter.limit("10/minute")
async def suggest_topics(
    request: Request,
    body: SuggestTopicsRequest,
    payload: dict = Depends(verify_jwt),
):
    """AI 주제 추천 — claude CLI(haiku)로 처리."""
    channel = body.channel or "naver-blog"
    settings = body.settings or {}
    
    context_parts = []
    if settings.get("insuranceCategory"):
        context_parts.append(f"보험 종류: {settings['insuranceCategory']}")
    if settings.get("targetAge"):
        context_parts.append(f"타겟 연령: {settings['targetAge']}")
    # ... 나머지 설정도 포함
    
    context = ", ".join(context_parts) if context_parts else "보험 관련 일반 콘텐츠"
    
    prompt = f"""보험 설계사를 위한 {channel} 콘텐츠 주제를 6개 추천해주세요.

컨텍스트: {context}

규칙:
- 15~40자 사이
- 검색 친화적 (키워드 포함)
- {channel} 특성에 맞춤
- 최신 트렌드 반영
- JSON 배열로 출력: ["주제1", "주제2", ...]
"""
    
    result = subprocess.run(
        ["claude", "-p", prompt, "--model", "haiku", "--output-format", "json"],
        capture_output=True, text=True, timeout=15,
        cwd="/tmp",
    )
    
    topics = json.loads(result.stdout)
    return {"topics": topics if isinstance(topics, list) else []}
```

### 2. 프론트엔드 호출 변경
`src/pages/Generate.tsx` (116-142줄):

```tsx
// 기존: Edge Function 호출
const { data } = await supabase.functions.invoke("suggest-topics", { body: {...} });

// 변경: 아누 서버 직접 호출
const res = await fetch(`${INSURO_API_BASE}/api/insuro/suggest-topics`, {
  method: "POST",
  headers: { "Content-Type": "application/json", "Authorization": `Bearer ${token}` },
  body: JSON.stringify({ channel, settings: { ...currentSettings } }),
});
const data = await res.json();
```

### 3. 플랜 제한 유지
- 기존: Edge Function에서 `sort_order >= 3` (Pro 이상) 체크
- 변경: 서버에서 `require_plan("프로")` 또는 `require_feature("ai_topic_suggest")` 적용

## affected_files
- `server/main.py` (수정 — suggest-topics 엔드포인트 추가)
- `src/pages/Generate.tsx` (수정 — Edge Function → 서버 API 호출)

## 검증 시나리오
1. AI 주제 추천 클릭 → 6개 주제 정상 표시 (에러 없음)
2. 채널/설정에 맞는 주제가 추천됨
3. Free/Basic 플랜 → 접근 제한 메시지
4. npm run build 성공
5. 서버 curl 테스트: POST /api/insuro/suggest-topics → 200 응답
