File size: 7,159 Bytes
495529d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5f65c7
 
 
 
 
 
 
495529d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"""
Ticket Comments API - Team Collaboration

Endpoints for:
1. Create comments on tickets
2. Update comments (by author)
3. Delete comments (by author or PM)
4. List comments with filtering
5. Get comment replies (threading)

Authorization:
- All authenticated users can create comments
- Only author can edit their own comments
- Author or PM can delete comments
"""

from fastapi import APIRouter, Depends, status, Query
from sqlalchemy.orm import Session
from typing import Optional, List
from uuid import UUID

from app.api.deps import get_db, get_current_user
from app.models.user import User
from app.services.ticket_comment_service import TicketCommentService
from app.schemas.ticket_comment import (
    TicketCommentCreate,
    TicketCommentUpdate,
    TicketCommentResponse,
    TicketCommentListResponse,
    COMMENT_TYPES
)

router = APIRouter()


# ============================================
# CREATE COMMENT
# ============================================

@router.post(
    "/tickets/{ticket_id}/comments",
    response_model=TicketCommentResponse,
    status_code=status.HTTP_201_CREATED,
    summary="Create comment on ticket",
    description="""
    Create a new comment on a ticket.
    
    **Features:**
    - Internal comments (team only) or external (client-visible)
    - Threading: Reply to other comments using parent_comment_id
    - Mentions: Tag users with mentioned_user_ids
    - Attachments: Link documents/images with attachment_document_ids
    
    **Image Attachments:**
    - Upload images first via POST /api/v1/documents/upload
    - Then include document IDs in attachment_document_ids array
    - Images are automatically detected and displayed inline
    - Supports multiple images per comment
    
    **Comment Types:**
    - note: General note or observation
    - issue: Problem or blocker
    - resolution: Solution or fix
    - question: Question for team
    - update: Status update
    
    **Authorization:** All authenticated users
    """
)
def create_comment(
    ticket_id: UUID,
    data: TicketCommentCreate,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """Create a new comment on a ticket"""
    return TicketCommentService.create_comment(
        ticket_id=ticket_id,
        data=data,
        current_user=current_user,
        db=db
    )


# ============================================
# UPDATE COMMENT
# ============================================

@router.put(
    "/comments/{comment_id}",
    response_model=TicketCommentResponse,
    summary="Update comment",
    description="""
    Update a comment (only by original author).
    
    **Edit Tracking:**
    - is_edited flag set to true
    - edited_at timestamp recorded
    - edited_by_user_id tracked
    
    **Authorization:** Comment author only
    """
)
def update_comment(
    comment_id: UUID,
    data: TicketCommentUpdate,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """Update a comment (author only)"""
    return TicketCommentService.update_comment(
        comment_id=comment_id,
        data=data,
        current_user=current_user,
        db=db
    )


# ============================================
# DELETE COMMENT
# ============================================

@router.delete(
    "/comments/{comment_id}",
    status_code=status.HTTP_200_OK,
    summary="Delete comment",
    description="""
    Delete a comment (soft delete).
    
    **Authorization:** 
    - Comment author can delete their own comments
    - Project managers can delete any comment
    - Platform admins can delete any comment
    """
)
def delete_comment(
    comment_id: UUID,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """Delete a comment (soft delete)"""
    return TicketCommentService.delete_comment(
        comment_id=comment_id,
        current_user=current_user,
        db=db
    )


# ============================================
# GET SINGLE COMMENT
# ============================================

@router.get(
    "/comments/{comment_id}",
    response_model=TicketCommentResponse,
    summary="Get comment by ID",
    description="""
    Get a single comment by ID.
    
    **Returns:**
    - Comment details
    - Author information
    - Edit history
    - Reply count
    
    **Authorization:** All authenticated users
    """
)
def get_comment(
    comment_id: UUID,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """Get a single comment"""
    return TicketCommentService.get_comment(
        comment_id=comment_id,
        db=db
    )


# ============================================
# LIST COMMENTS
# ============================================

@router.get(
    "/tickets/{ticket_id}/comments",
    response_model=TicketCommentListResponse,
    summary="List ticket comments",
    description="""
    List all comments for a ticket with pagination and filtering.
    
    **Filtering:**
    - is_internal: Show only internal or external comments
    - comment_type: Filter by comment type (note, issue, resolution, etc.)
    - parent_only: Show only top-level comments (exclude replies)
    
    **Pagination:**
    - page: Page number (1-indexed)
    - page_size: Items per page (default 50, max 100)
    
    **Sorting:**
    - Comments sorted by created_at DESC (newest first)
    
    **Authorization:** All authenticated users
    """
)
def list_comments(
    ticket_id: UUID,
    page: int = Query(1, ge=1, description="Page number"),
    page_size: int = Query(50, ge=1, le=100, description="Items per page"),
    is_internal: Optional[bool] = Query(None, description="Filter by internal/external"),
    comment_type: Optional[str] = Query(None, description=f"Filter by type: {', '.join(COMMENT_TYPES)}"),
    parent_only: bool = Query(False, description="Show only top-level comments"),
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """List comments for a ticket"""
    return TicketCommentService.list_comments(
        ticket_id=ticket_id,
        page=page,
        page_size=page_size,
        is_internal=is_internal,
        comment_type=comment_type,
        parent_only=parent_only,
        db=db
    )


# ============================================
# GET COMMENT REPLIES
# ============================================

@router.get(
    "/comments/{comment_id}/replies",
    response_model=List[TicketCommentResponse],
    summary="Get comment replies",
    description="""
    Get all replies to a specific comment (threading).
    
    **Use Case:**
    - Load replies when user expands a comment thread
    - Show conversation history
    
    **Sorting:**
    - Replies sorted by created_at ASC (oldest first)
    
    **Authorization:** All authenticated users
    """
)
def get_comment_replies(
    comment_id: UUID,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """Get all replies to a comment"""
    return TicketCommentService.get_comment_replies(
        comment_id=comment_id,
        db=db
    )