# InsuWiki 출처/신뢰도 체계 Phase 1: 타입 확장 + Sync 듀얼 라이트

## Lv.4 한정승인 — 설계서 기반 구현

## 설계서 참조 (필수 읽기)
`/home/jay/workspace/memory/specs/insuwiki/source-authority-design.md`

## Phase 1 범위 (3일)

### 1. Firestore 타입 확장
- 파일: `/home/jay/projects/insuwiki/nextapp/src/types/firestore.ts`
- Document 인터페이스에 7개 optional 필드 추가:
  ```typescript
  sourceType?: SourceType;
  verificationStatus?: 'unverified' | 'auto_passed' | 'expert_verified';
  authorityTier?: number;
  sourceMeta?: SourceMeta;
  sourceRef?: SourceRef;
  question?: string;
  answer?: string;
  ```
- SourceType 타입 정의 (9개 값):
  `'user_written' | 'kakao_expert' | 'kakao_community' | 'youtube_summary' | 'policy_analysis' | 'newsletter_digest' | 'expert_verified' | 'regulation' | 'court_ruling'`
- SourceMeta, SourceRef 인터페이스 정의

### 2. Sync 듀얼 라이트 구현
- 파일: `/home/jay/workspace/dashboard/server.py` (POST /api/wiki/sync-firestore)
- 현재: `db.collection("wiki").document(eid).set(...)` 단일 쓰기
- 변경: `wiki`(원본) + `documents`(앱 표시) 동시 WriteBatch
- ID 프리픽스: `wiki__{sourceType}__{originalId}`
- documents 컬렉션 필수 필드 매핑:
  ```python
  doc_data = {
      "title": ...,
      "content": f"## 질문\n{question}\n\n## 답변\n{answer}",
      "visibility": "public",  # rejected → "private"
      "authorId": "system_kakao_qa",
      "authorName": "카카오 Q&A",
      "docType": "wiki",
      "category": _map_category(entry_category),  # 보상→casualty 등
      "sourceType": _determine_source_type(entry),
      "verificationStatus": "expert_verified" if approved else "unverified",
      "authorityTier": 3.5,  # kakao_expert 기본
      "sourceRef": {"channelName": source_chat, ...},
      "question": ...,
      "answer": ...,
      "searchKeywords": _generate_keywords(title, question, answer),
      "version": 1,
      "createdAt": SERVER_TIMESTAMP,
      "updatedAt": SERVER_TIMESTAMP,
  }
  ```

### 3. Firestore 인덱스 추가
- `documents` 컬렉션에 복합 인덱스:
  - `(sourceType, visibility, updatedAt)`
  - `(verificationStatus, visibility, updatedAt)`

### 4. Firestore Rules 업데이트
- 새 필드에 대한 읽기 권한 설정
- `system_kakao_qa` authorId에 대한 처리

## 테스트
1. 대시보드에서 insight-001 승인 → sync → Firestore documents에 생성 확인
2. 인슈위키 앱에서 Wiki 탭에 표시되는지 확인
3. ID 충돌 없는지 확인 (기존 documents와)
4. rejected 항목은 visibility=private로 처리되는지

## 보고서
`/home/jay/workspace/memory/reports/task-1590.md`에 작성
