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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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package controllers

import (
	"encoding/json"
	"net/http"
	"strconv"

	"dinacom-11.0-backend/models/dto"
	"dinacom-11.0-backend/services"
	"dinacom-11.0-backend/utils"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

type ReportController interface {
	CreateReport(ctx *gin.Context)
	GetReports(ctx *gin.Context)
	AssignWorker(ctx *gin.Context)
	GetAssignedReports(ctx *gin.Context)
	FinishReport(ctx *gin.Context)
	GetUserReports(ctx *gin.Context)
	GetWorkerAssignedReports(ctx *gin.Context)
	GetWorkerHistory(ctx *gin.Context)
	VerifyReport(ctx *gin.Context)
	GetReportDetail(ctx *gin.Context)
	GetReportImage(ctx *gin.Context)
	GetAllReportsAdmin(ctx *gin.Context)
	GetFullReportDetail(ctx *gin.Context)
	GetUserReportStats(ctx *gin.Context)
}

type reportController struct {
	reportService services.ReportService
}

func NewReportController(reportService services.ReportService) ReportController {
	return &reportController{reportService: reportService}
}

// @Summary Create Report
// @Description Submit a new report with image and location data
// @Tags Report
// @Accept multipart/form-data
// @Produce json
// @Param files formData file true "Image file (JPG, PNG, JPEG, max 32MB)"
// @Param json formData string true "JSON data" default({"longitude": 106.816666, "latitude": -6.200000, "road_name": "Jalan Sudirman", "description": "Lubang besar di tengah jalan"})
// @Security BearerAuth
// @Success 200 {object} dto.ReportResponse
// @Failure 400 {object} map[string]string
// @Router /api/user/report [post]
func (c *reportController) CreateReport(ctx *gin.Context) {
	userIDVal, exists := ctx.Get("user_id")
	if !exists {
		utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized")
		return
	}
	userID := userIDVal.(uuid.UUID)

	file, header, err := ctx.Request.FormFile("files")
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, "Image file is required")
		return
	}
	defer file.Close()

	jsonData := ctx.PostForm("json")
	if jsonData == "" {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, "JSON data is required")
		return
	}

	var req dto.ReportRequest
	if err := json.Unmarshal([]byte(jsonData), &req); err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, "Invalid JSON format")
		return
	}

	response, err := c.reportService.CreateReport(userID, file, header, req)
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Report created successfully", response)
}

// @Summary Get Reports
// @Description Get all completed reports with non-good destruct class
// @Tags Report
// @Produce json
// @Success 200 {array} dto.ReportLocationResponse
// @Failure 500 {object} map[string]string
// @Router /api/get_report [get]
func (c *reportController) GetReports(ctx *gin.Context) {
	reports, err := c.reportService.GetReports()
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Reports retrieved", reports)
}

// @Summary Assign Worker to Report
// @Description Admin assigns a worker to a report
// @Tags Admin
// @Accept json
// @Produce json
// @Param request body dto.AssignWorkerRequest true "Assign Worker Request"
// @Security BearerAuth
// @Success 200 {object} map[string]string
// @Failure 400 {object} map[string]string
// @Router /api/admin/report/assign [patch]
func (c *reportController) AssignWorker(ctx *gin.Context) {
	var req dto.AssignWorkerRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
		return
	}

	message, err := c.reportService.AssignWorker(req)
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, message, nil)
}

// @Summary Get Assigned Workers
// @Description Get all workers with assigned reports
// @Tags Admin
// @Produce json
// @Security BearerAuth
// @Success 200 {array} dto.AssignedWorkerResponse
// @Failure 500 {object} map[string]string
// @Router /api/admin/report/assign [get]
func (c *reportController) GetAssignedReports(ctx *gin.Context) {
	reports, err := c.reportService.GetAssignedReports()
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Assigned reports retrieved", reports)
}

// @Summary Finish Report by Worker
// @Description Worker uploads after image and marks report as finished
// @Tags Worker
// @Accept multipart/form-data
// @Produce json
// @Param files formData file true "After image file"
// @Param json formData string true "JSON data" default({"report_id": "uuid-here"})
// @Security BearerAuth
// @Success 200 {object} map[string]string
// @Failure 400 {object} map[string]string
// @Router /api/worker/report [patch]
func (c *reportController) FinishReport(ctx *gin.Context) {
	workerIDVal, exists := ctx.Get("user_id")
	if !exists {
		utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized")
		return
	}
	workerID := workerIDVal.(uuid.UUID)

	file, header, err := ctx.Request.FormFile("files")
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, "Image file is required")
		return
	}
	defer file.Close()

	jsonData := ctx.PostForm("json")
	if jsonData == "" {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, "JSON data is required")
		return
	}

	var req dto.WorkerReportRequest
	if err := json.Unmarshal([]byte(jsonData), &req); err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, "Invalid JSON format")
		return
	}

	if err := c.reportService.FinishReport(workerID, file, header, req.ReportID); err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Report finished successfully", nil)
}

// @Summary Get User's Reports
// @Description Get all reports created by the logged-in user with pagination
// @Tags User
// @Produce json
// @Param page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(10)
// @Security BearerAuth
// @Success 200 {object} dto.PaginatedReportsResponse
// @Failure 401 {object} map[string]string
// @Router /api/user/report/me [get]
func (c *reportController) GetUserReports(ctx *gin.Context) {
	userIDVal, exists := ctx.Get("user_id")
	if !exists {
		utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized")
		return
	}
	userID := userIDVal.(uuid.UUID)

	page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
	if page < 1 {
		page = 1
	}
	if limit < 1 || limit > 100 {
		limit = 10
	}

	response, err := c.reportService.GetUserReports(userID, page, limit)
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "User reports retrieved", response)
}

// @Summary Get Worker's Assigned Reports
// @Description Get all reports assigned to the logged-in worker with pagination
// @Tags Worker
// @Produce json
// @Param page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(10)
// @Security BearerAuth
// @Success 200 {object} dto.PaginatedReportsResponse
// @Failure 401 {object} map[string]string
// @Router /api/worker/report/assign/me [get]
func (c *reportController) GetWorkerAssignedReports(ctx *gin.Context) {
	workerIDVal, exists := ctx.Get("user_id")
	if !exists {
		utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized")
		return
	}
	workerID := workerIDVal.(uuid.UUID)

	page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
	if page < 1 {
		page = 1
	}
	if limit < 1 || limit > 100 {
		limit = 10
	}

	response, err := c.reportService.GetWorkerAssignedReports(workerID, page, limit)
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Worker assigned reports retrieved", response)
}

// @Summary Get Worker's History
// @Description Get worker's completed reports with pagination and status filter
// @Tags Worker
// @Produce json
// @Param page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(10)
// @Param verify_admin query bool false "True: finished, False: Finish by Worker" default(false)
// @Security BearerAuth
// @Success 200 {object} dto.PaginatedReportsResponse
// @Failure 401 {object} map[string]string
// @Router /api/worker/report/history/me [get]
func (c *reportController) GetWorkerHistory(ctx *gin.Context) {
	workerIDVal, exists := ctx.Get("user_id")
	if !exists {
		utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized")
		return
	}
	workerID := workerIDVal.(uuid.UUID)

	page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
	verifyAdmin := ctx.DefaultQuery("verify_admin", "false") == "true"
	if page < 1 {
		page = 1
	}
	if limit < 1 || limit > 100 {
		limit = 10
	}

	response, err := c.reportService.GetWorkerHistory(workerID, verifyAdmin, page, limit)
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Worker history retrieved", response)
}

// @Summary Verify Report by Admin
// @Description Admin verifies a report with status 'Finish by Worker' to 'finished'
// @Tags Admin
// @Accept json
// @Produce json
// @Param request body dto.VerifyReportRequest true "Verify Report Request"
// @Security BearerAuth
// @Success 200 {object} map[string]string
// @Failure 400 {object} map[string]string
// @Router /api/admin/report/verify [patch]
func (c *reportController) VerifyReport(ctx *gin.Context) {
	var req dto.VerifyReportRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
		return
	}

	if err := c.reportService.VerifyReport(req.ReportID); err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Report verified successfully", nil)
}

// @Summary Get Report Detail
// @Description Get assigned report detail for worker
// @Tags Worker
// @Produce json
// @Param report_id query string true "Report ID"
// @Security BearerAuth
// @Success 200 {object} dto.ReportDetailResponse
// @Failure 400 {object} map[string]string
// @Failure 401 {object} map[string]string
// @Router /api/worker/report/assign/detail [get]
func (c *reportController) GetReportDetail(ctx *gin.Context) {
	workerIDVal, exists := ctx.Get("user_id")
	if !exists {
		utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized")
		return
	}
	workerID := workerIDVal.(uuid.UUID)

	reportID := ctx.Query("report_id")
	if reportID == "" {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, "report_id is required")
		return
	}

	response, err := c.reportService.GetReportDetail(workerID, reportID)
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Report detail retrieved", response)
}

// @Summary Get Report Image
// @Description Get before image URL for assigned report
// @Tags Worker
// @Produce json
// @Param report_id query string true "Report ID"
// @Security BearerAuth
// @Success 200 {object} dto.ReportImageResponse
// @Failure 400 {object} map[string]string
// @Failure 401 {object} map[string]string
// @Router /api/worker/report/assign/image [get]
func (c *reportController) GetReportImage(ctx *gin.Context) {
	workerIDVal, exists := ctx.Get("user_id")
	if !exists {
		utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized")
		return
	}
	workerID := workerIDVal.(uuid.UUID)

	reportID := ctx.Query("report_id")
	if reportID == "" {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, "report_id is required")
		return
	}

	response, err := c.reportService.GetReportImage(workerID, reportID)
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Report image retrieved", response)
}

// @Summary Get All Reports (Admin)
// @Description Get all reports with optional status filter and pagination
// @Tags Admin
// @Produce json
// @Param status query string false "Filter by status (pending, assigned, finish by worker, verified, complete)"
// @Param page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(10)
// @Security BearerAuth
// @Success 200 {object} dto.PaginatedReportsResponse
// @Failure 401 {object} map[string]string
// @Router /api/admin/report [get]
func (c *reportController) GetAllReportsAdmin(ctx *gin.Context) {
	status := ctx.Query("status")
	page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
	if page < 1 {
		page = 1
	}
	if limit < 1 || limit > 100 {
		limit = 10
	}

	response, err := c.reportService.GetAllReportsAdmin(status, page, limit)
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Reports retrieved", response)
}

// @Summary Get Full Report Detail (Admin)
// @Description Get complete report details by report ID
// @Tags Admin
// @Produce json
// @Param report_id query string true "Report ID"
// @Security BearerAuth
// @Success 200 {object} dto.FullReportDetailResponse
// @Failure 400 {object} map[string]string
// @Router /api/admin/report/detail [get]
func (c *reportController) GetFullReportDetail(ctx *gin.Context) {
	reportID := ctx.Query("report_id")
	if reportID == "" {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, "report_id is required")
		return
	}

	response, err := c.reportService.GetFullReportDetail(reportID)
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Report detail retrieved", response)
}

// @Summary Get User Report Stats
// @Description Get report statistics for the authenticated user
// @Tags User
// @Produce json
// @Security BearerAuth
// @Success 200 {object} dto.UserReportStatsResponse
// @Failure 401 {object} map[string]string
// @Router /api/user/report/stats [get]
func (c *reportController) GetUserReportStats(ctx *gin.Context) {
	userIDVal, exists := ctx.Get("user_id")
	if !exists {
		utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized")
		return
	}

	userID := userIDVal.(uuid.UUID)
	stats, err := c.reportService.GetUserReportStats(userID)
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Report stats retrieved", stats)
}