# CuCu Note — Team Draft → Main

## Quy trình A-Z

```
1. User tạo Team
   └── Invite members (link/email)

2. Trong Team có 2 vùng:
   ┌─────────────┐     ┌─────────────┐
   │    NHÁP     │     │    CHÍNH    │
   │  (Draft)    │     │   (Main)    │
   └─────────────┘     └─────────────┘

3. Flow:
   Tạo note nháp → Team comment góp ý → Review → Merge vào Chính
```

## Trạng thái triển khai

### Backend ✅ DONE
- [x] Schema: `Team`, `TeamMember`, `TeamMemo` (Pydantic) — `backend/common/team/schemas.py`
- [x] Service: `TeamService` 17 methods — `backend/common/team/services.py`
  - Team CRUD: `create_team`, `list_teams`, `get_team`, `update_team`, `delete_team`
  - Members: `join_team`, `remove_member`, `leave_team`
  - Memos: `create_draft`, `list_drafts`, `list_main`, `merge_to_main`, `unmerge_to_draft`
  - Actions: `pin_memo`, `update_memo`, `delete_memo`
  - Helpers: `_is_member`, `_is_owner`, `_team_to_response`, `_memo_to_response`
- [x] API Routes: 17 endpoints `/api/v1/teams/...` — `backend/api/team_routes.py`
- [x] MongoDB: 3 collections + indexes — `backend/common/mongo_client.py`
  - `cuccu_teams`, `cuccu_team_members`, `cuccu_team_memos`
- [x] Server: router registered — `backend/server.py`
- [x] Test: full A-Z flow passed (create → join → draft → merge → unmerge → delete)

### Frontend ✅ DONE (v1)
- [x] API Service: `frontend/src/service/teamService.ts` — 15 API calls + types
- [x] Teams list page: `frontend/src/pages/Teams.tsx` — create, join, list, delete/leave
- [x] Team workspace: `frontend/src/pages/TeamWorkspace.tsx`
  - Editor giống MemoEditor (cùng classes)
  - Cards giống MemoView (cùng MEMO_CARD_BASE_CLASSES)
  - Tab: Nháp / Chính
  - Pin (ghim lên đầu)
  - Group by tag (#idea, #plan, #blocker...)
  - Actions hover: Pin, Merge, Unmerge, Delete (shadcn Tooltip)
- [x] Routes: `/app/teams` + `/app/teams/:teamId` — `frontend/src/router/index.tsx`
- [x] Sidebar: Teams section + dynamic team list — `frontend/src/components/Navigation.tsx`

### Chưa làm
- [ ] Guest mode (xem file `plan/guest_mode.md`)
- [ ] Comment trên team memos
- [ ] Export → Notion
- [ ] AI tóm tắt team notes
- [ ] Cleanup cronjob cho guest data

## API Endpoints

### Team APIs
| Method | Path | Mô tả | Status |
|--------|------|-------|--------|
| POST | `/api/v1/teams` | Tạo team | ✅ |
| GET | `/api/v1/teams` | List teams của user | ✅ |
| GET | `/api/v1/teams/:id` | Chi tiết team | ✅ |
| PATCH | `/api/v1/teams/:id` | Sửa team | ✅ |
| DELETE | `/api/v1/teams/:id` | Xóa team | ✅ |

### Team Member APIs
| Method | Path | Mô tả | Status |
|--------|------|-------|--------|
| POST | `/api/v1/teams/join` | Join bằng invite code | ✅ |
| GET | `/api/v1/teams/:id/members` | List members | ✅ |
| DELETE | `/api/v1/teams/:id/members/:uid` | Xóa member | ✅ |
| POST | `/api/v1/teams/:id/leave` | Rời team | ✅ |

### Team Memo APIs
| Method | Path | Mô tả | Status |
|--------|------|-------|--------|
| POST | `/api/v1/teams/:id/memos` | Tạo note nháp | ✅ |
| GET | `/api/v1/teams/:id/drafts` | List nháp | ✅ |
| GET | `/api/v1/teams/:id/main` | List bản chính | ✅ |
| PATCH | `/api/v1/teams/:id/memos/:mid` | Sửa memo | ✅ |
| DELETE | `/api/v1/teams/:id/memos/:mid` | Xóa memo | ✅ |
| POST | `/api/v1/teams/:id/memos/:mid/merge` | Merge nháp → chính | ✅ |
| POST | `/api/v1/teams/:id/memos/:mid/unmerge` | Unmerge chính → nháp | ✅ |
| POST | `/api/v1/teams/:id/memos/:mid/pin` | Pin/unpin memo | ✅ |

## Files đã sửa/tạo

### Backend (5 files)
```
backend/common/team/__init__.py        — NEW
backend/common/team/schemas.py         — NEW (Pydantic models)
backend/common/team/services.py        — NEW (TeamService, 17 methods)
backend/api/team_routes.py             — NEW (17 API endpoints)
backend/common/mongo_client.py         — MODIFIED (3 collections + indexes)
backend/server.py                      — MODIFIED (register team_router)
```

### Frontend (5 files)
```
frontend/src/service/teamService.ts    — NEW (API client + types)
frontend/src/pages/Teams.tsx           — NEW (team list page)
frontend/src/pages/TeamWorkspace.tsx   — NEW (workspace: editor, cards, tabs)
frontend/src/router/routes.ts          — MODIFIED (TEAMS route)
frontend/src/router/index.tsx          — MODIFIED (lazy routes)
frontend/src/components/Navigation.tsx — MODIFIED (sidebar team list + imports)
```

## Bugs biết
- Clerk CORS block khi truy cập bằng IP (160.191.50.138) thay vì localhost → cần thêm domain vào Clerk Dashboard
