File size: 24,441 Bytes
483bf6a
 
 
c0722d3
483bf6a
 
51ec421
1537c35
483bf6a
987726a
483bf6a
 
1537c35
483bf6a
 
 
 
 
 
 
 
 
aa6ea88
483bf6a
 
51688a8
 
 
 
483bf6a
 
 
f18c71f
 
483bf6a
f18c71f
483bf6a
 
 
 
 
d428c29
f18c71f
 
 
483bf6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0722d3
 
 
 
 
 
 
 
 
 
483bf6a
 
c0722d3
38100fe
483bf6a
 
 
c0722d3
 
 
 
 
 
 
 
 
483bf6a
c0722d3
483bf6a
 
 
 
c0722d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa6ea88
c0722d3
aa6ea88
51688a8
 
987726a
 
 
b03de03
 
 
 
 
 
 
aa6ea88
 
 
 
 
 
 
 
 
 
51688a8
 
aa6ea88
51688a8
aa6ea88
1537c35
 
 
 
 
 
 
51688a8
1537c35
aa6ea88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51688a8
987726a
 
 
 
 
 
51688a8
aa6ea88
51688a8
483bf6a
 
c0722d3
 
 
 
 
 
 
 
 
 
483bf6a
 
 
 
 
 
c0722d3
 
 
 
 
 
 
 
 
 
 
483bf6a
c0722d3
483bf6a
 
 
 
 
c0722d3
 
 
 
 
 
 
 
 
 
483bf6a
c0722d3
 
483bf6a
 
 
 
c0722d3
 
 
 
 
 
 
 
 
 
 
483bf6a
 
 
c0722d3
38100fe
483bf6a
 
 
f18c71f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0722d3
 
 
 
 
 
 
 
 
 
483bf6a
 
 
 
 
 
f18c71f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0722d3
 
 
 
 
 
 
 
 
 
 
 
 
483bf6a
 
 
 
c0722d3
 
 
 
 
 
 
 
 
 
 
 
483bf6a
 
c0722d3
483bf6a
 
38100fe
483bf6a
 
 
 
 
 
 
 
 
 
 
d428c29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f18c71f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0722d3
 
 
 
 
 
 
 
 
 
483bf6a
 
 
 
 
 
f18c71f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0722d3
 
 
 
 
 
 
 
 
 
483bf6a
c0722d3
 
483bf6a
 
 
 
c0722d3
 
 
 
 
 
 
 
 
 
 
 
 
483bf6a
c0722d3
483bf6a
 
38100fe
483bf6a
 
 
 
 
 
 
 
38100fe
483bf6a
 
 
 
 
 
 
5fc40a1
 
c0722d3
 
 
 
 
 
 
 
 
51ec421
c0722d3
 
51688a8
 
c0722d3
1b2c396
 
 
 
c0722d3
51688a8
 
c0722d3
 
51ec421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0722d3
 
 
51688a8
 
c0722d3
 
 
 
 
 
 
 
 
 
 
5fc40a1
 
 
 
c0722d3
5fc40a1
 
 
 
c0722d3
5fc40a1
 
 
 
c0722d3
 
 
 
 
 
 
 
 
 
5fc40a1
c0722d3
 
5fc40a1
 
 
c0722d3
 
 
 
 
 
 
 
 
 
5fc40a1
c0722d3
5fc40a1
 
 
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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
package controllers

import (
	"errors"
	"net/http"
	"strconv"
	"time"

	"abdanhafidz.com/go-boilerplate/models/dto"
	entity "abdanhafidz.com/go-boilerplate/models/entity"
	http_error "abdanhafidz.com/go-boilerplate/models/error"
	"abdanhafidz.com/go-boilerplate/services"
	"abdanhafidz.com/go-boilerplate/utils"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

type AcademyController interface {
	// Academy
	CreateAcademy(ctx *gin.Context)
	GetAcademy(ctx *gin.Context)
	GetAcademyDetail(ctx *gin.Context)
	ListAcademy(ctx *gin.Context)
	UpdateAcademy(ctx *gin.Context)
	DeleteAcademy(ctx *gin.Context)
	JoinAcademyByCode(ctx *gin.Context)
	AssignAccountToAcademy(ctx *gin.Context)
	UnassignAccountFromAcademy(ctx *gin.Context)
	ListAssignmentsByAcademy(ctx *gin.Context)

	// Material
	GetMaterial(ctx *gin.Context)
	ListMaterialsByAcademy(ctx *gin.Context)
	GetMaterialDetail(ctx *gin.Context)
	CreateMaterial(ctx *gin.Context)
	UpdateMaterial(ctx *gin.Context)
	DeleteMaterial(ctx *gin.Context)

	// Content
	CreateContent(ctx *gin.Context)
	GetContent(ctx *gin.Context)
	ListContentsByAcademy(ctx *gin.Context)
	ListContentsByMaterial(ctx *gin.Context)
	GetContentDetail(ctx *gin.Context)
	UpdateContent(ctx *gin.Context)
	DeleteContent(ctx *gin.Context)

	// Progress
	UpdateContentProgress(ctx *gin.Context)
}

type academyController struct {
	academyService services.AcademyService
}

func NewAcademyController(academyService services.AcademyService) AcademyController {
	return &academyController{academyService}
}

// ================= ACADEMY =================
// GetAcademy godoc
// @Summary      Get Academy by Slug
// @Description  Retrieve academy details using its slug
// @Tags         Academy
// @Accept       json
// @Produce      json
// @Param        academy_slug  path      string  true  "Academy Slug"
// @Success      200           {object}  dto.SuccessResponse[any]
// @Failure      400           {object}  dto.ErrorResponse
// @Router       /api/v1/academy/{academy_slug} [get]
func (c *academyController) GetAcademy(ctx *gin.Context) {
	academySlug := ctx.Param("academy_slug")
	accountId := ParseAccountId(ctx)
	res, err := c.academyService.GetAcademyResponse(ctx.Request.Context(), accountId, academySlug)
	ResponseJSON(ctx, gin.H{"academy_slug": academySlug}, res, err)
}

// GetAcademyDetail godoc
// @Summary      Get Academy Detail by ID
// @Description  Retrieve detailed academy information using its ID
// @Tags         Academy
// @Accept       json
// @Produce      json
// @Param        id   path      string  true  "Academy ID"
// @Success      200  {object}  dto.SuccessResponse[entity.Academy]
// @Failure      400  {object}  dto.ErrorResponse
func (c *academyController) GetAcademyDetail(ctx *gin.Context) {
	id := ParseUUID(ctx, "id")
	res, err := c.academyService.GetAcademyDetail(ctx.Request.Context(), id)
	ResponseJSON(ctx, gin.H{"id": id}, res, err)
}

// ListAcademy godoc
// @Summary      List Academies
// @Description  Retrieve a paginated list of academies with optional filters
// @Tags         Academy
// @Accept       json
// @Produce      json
// @Param        limit           query     int     false  "Number of items per page"            default(10)
// @Param        page            query     int     false  "Page number"                         default(1)
// @Param        search          query     string  false  "Search term for academy title/name"
// @Param        sortBy          query     string  false  "Field to sort by"
// @Param        order           query     string  false  "Sort order (asc or desc)"
// @Param        registerStatus  query     int     false  "Filter by registration status"
// @Param        status          query     string  false  "Filter by academy status"
// @Success      200             {object}  dto.SuccessResponse[[]entity.Academy]
// @Failure	  400             {object}  dto.ErrorResponse
// @Router       /api/v1/academy [get]

func (c *academyController) ListAcademy(ctx *gin.Context) {
	accountId := ParseAccountId(ctx)

	limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
	page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
	search := ctx.DefaultQuery("search", "")
	sortBy := ctx.DefaultQuery("sortBy", "")
	order := ctx.DefaultQuery("order", "")

	var registerStatus *int
	if val := ctx.Query("registerStatus"); val != "" {
		if i, err := strconv.Atoi(val); err == nil {
			registerStatus = &i
		}
	}
	isModified := false

	if limit < 1 {
		limit = 10
		isModified = true
	} else if limit > 50 {
		limit = 50
		isModified = true
	}

	if page < 1 {
		page = 1
		isModified = true
	}

	var status *string
	if val := ctx.Query("status"); val != "" {
		if val == entity.StatusNotStarted || val == entity.StatusInProgress || val == entity.StatusFinished {
			status = &val
		}
	}

	offset := (page - 1) * limit
	p := entity.Pagination{Limit: limit, Offset: offset, Search: search, SortBy: sortBy, Order: order, RegisterStatus: registerStatus, Status: status}
	list, total, err := c.academyService.ListAcademy(ctx.Request.Context(), accountId, p)

	if err != nil {
		ResponseJSON[any, any](ctx, nil, nil, err)
		return
	}

	var totalPages int
	if total == 0 {
		totalPages = 1
	} else {
		totalPages = int((total + int64(limit) - 1) / int64(limit))
	}

	if page > totalPages {
		page = totalPages
		offset = (page - 1) * limit
		p.Offset = offset
		list, total, err = c.academyService.ListAcademy(ctx.Request.Context(), accountId, p)
		isModified = true
	}

	meta := gin.H{
		"totalItems":  total,
		"totalPages":  totalPages,
		"currentPage": page,
	}
	if isModified {
		ctx.Status(http.StatusAccepted)
	}

	ResponseJSON(ctx, meta, list, err)
}

// CreateAcademy godoc
// @Summary      Create Academy
// @Description  Create a new academy
// @Tags         Academy
// @Accept       json
// @Produce      json
// @Param        request  body      dto.CreateAcademyRequest  true  "Create Academy Request"
// @Success      200      {object}  dto.SuccessResponse[entity.Academy]
// @Failure      400      {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) CreateAcademy(ctx *gin.Context) {
	req := RequestJSON[dto.CreateAcademyRequest](ctx)
	res, err := c.academyService.CreateAcademy(ctx.Request.Context(), req)
	ResponseJSON(ctx, req, res, err)
}

// UpdateAcademy godoc
// @Summary      Update Academy
// @Description  Update an existing academy
// @Tags         Academy
// @Accept       json
// @Produce      json
// @Param        id       path      string                     true  "Academy ID"
// @Param        request  body      dto.UpdateAcademyRequest  true  "Update Academy Request"
// @Success      200      {object}  dto.SuccessResponse[entity.Academy]
// @Failure      400      {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) UpdateAcademy(ctx *gin.Context) {
	id := ParseUUID(ctx, "id")
	req := RequestJSON[dto.UpdateAcademyRequest](ctx)
	res, err := c.academyService.UpdateAcademy(ctx.Request.Context(), id, req)
	ResponseJSON(ctx, req, res, err)
}

// DeleteAcademy godoc
// @Summary      Delete Academy
// @Description  Delete an existing academy
// @Tags         Academy
// @Accept       json
// @Produce      json
// @Param        id   path      string  true  "Academy ID"
// @Success      200  {object}  dto.SuccessResponse[any]
// @Failure      400  {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) DeleteAcademy(ctx *gin.Context) {
	id := ParseUUID(ctx, "id")
	err := c.academyService.DeleteAcademy(ctx.Request.Context(), id)
	ResponseJSON(ctx, gin.H{"id": id}, gin.H{"deleted": true}, err)
}

// ================= MATERIAL =================
// GetMaterial godoc
// @Summary      Get Material by Slug
// @Description  Retrieve material details using its slug
// @Tags         Material
// @Accept       json
// @Produce      json
// @Param        academy_slug   path      string  true  "Academy Slug"
// @Param        material_slug  path      string  true  "Material Slug"
// @Success      200            {object}  dto.SuccessResponse[dto.MaterialDetailResponse]
// @Failure      400            {object}  dto.ErrorResponse
// @Router       /api/v1/academy/{academy_slug}/{material_slug} [get]
func (c *academyController) GetMaterial(ctx *gin.Context) {
	academySlug := ctx.Param("academy_slug")
	materialSlug := ctx.Param("material_slug")
	accountId := ParseAccountId(ctx)
	res, err := c.academyService.GetMaterialResponse(ctx.Request.Context(), accountId, academySlug, materialSlug)
	ResponseJSON(ctx, gin.H{"academy_slug": academySlug, "material_slug": materialSlug}, res, err)
}

// ListMaterialsByAcademy godoc
// @Summary      List Materials by Academy
// @Description  Retrieve a list of materials for a specific academy
// @Tags         Material
// @Accept       json
// @Produce      json
// @Param        id  path      string  true  "Academy ID"
// @Success      200 {object}  dto.SuccessResponse[[]entity.AcademyMaterial]
// @Failure      400 {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) ListMaterialsByAcademy(ctx *gin.Context) {
	id := ParseUUID(ctx, "id")
	res, err := c.academyService.ListMaterialsByAcademy(ctx.Request.Context(), id)
	ResponseJSON(ctx, gin.H{"academy_id": id}, res, err)
}

// GetMaterialDetail godoc
// @Summary      Get Material Detail by ID
// @Description  Retrieve detailed material information using its ID
// @Tags         Material
// @Accept       json
// @Produce      json
// @Param        id  path      string  true  "Material ID"
// @Success      200 {object}  dto.SuccessResponse[entity.AcademyMaterial]
// @Failure      400 {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) GetMaterialDetail(ctx *gin.Context) {
	id := ParseUUID(ctx, "id")
	res, err := c.academyService.GetMaterialDetail(ctx.Request.Context(), id)
	ResponseJSON(ctx, gin.H{"id": id}, res, err)
}

// CreateMaterial godoc
// @Summary      Create Material
// @Description  Create a new material for an academy
// @Tags         Material
// @Accept       json
// @Produce      json
// @Param        request  body      dto.CreateMaterialRequest  true  "Create Material Request"
// @Success      200      {object}  dto.SuccessResponse[entity.AcademyMaterial]
// @Failure      400      {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) CreateMaterial(ctx *gin.Context) {
	req := RequestJSON[dto.CreateMaterialRequest](ctx)
	res, err := c.academyService.CreateMaterial(ctx.Request.Context(), req)
	ResponseJSON(ctx, req, res, err)
}

// UpdateMaterial godoc
// @Summary      Update Material
// @Description  Update an existing material
// @Tags         Material
// @Accept       json
// @Produce      json
// @Param        id       path      string                     true  "Material ID"
// @Param        request  body      dto.UpdateMaterialRequest  true  "Update Material Request"
// @Success      200      {object}  dto.SuccessResponse[entity.AcademyMaterial]
// @Failure      400      {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) UpdateMaterial(ctx *gin.Context) {
	id := ParseUUID(ctx, "id")
	req := RequestJSON[dto.UpdateMaterialRequest](ctx)
	res, err := c.academyService.UpdateMaterial(ctx.Request.Context(), id, req)
	ResponseJSON(ctx, req, res, err)
}

// DeleteMaterial godoc
// @Summary      Delete Material
// @Description  Delete an existing material
// @Tags         Material
// @Accept       json
// @Produce      json
// @Param        id   path      string  true  "Material ID"
// @Success      200  {object}  dto.SuccessResponse[any]
// @Failure      400  {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) DeleteMaterial(ctx *gin.Context) {
	id := ParseUUID(ctx, "id")
	err := c.academyService.DeleteMaterial(ctx.Request.Context(), id)
	ResponseJSON(ctx, gin.H{"id": id}, gin.H{"deleted": true}, err)
}

// ================= CONTENT =================
// GetContent godoc
// @Summary      Get Content by Order
// @Description  Retrieve content details using its order within a material
// @Tags         Content
// @Accept       json
// @Produce      json
// @Param        academy_slug   path      string  true  "Academy Slug"
// @Param        material_slug  path      string  true  "Material Slug"
// @Param        order          path      int     true  "Content Order"
// @Success      200            {object}  dto.SuccessResponse[entity.AcademyContent]
// @Failure      400            {object}  dto.ErrorResponse
// @Router       /api/v1/academy/{academy_slug}/{material_slug}/{order} [get]

func (c *academyController) GetContent(ctx *gin.Context) {
	accountId := ParseAccountId(ctx)
	academySlug := ctx.Param("academy_slug")
	materialSlug := ctx.Param("material_slug")

	orderID64, err := strconv.ParseUint(ctx.Param("order"), 10, 64)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid 'order' parameter. Must be a positive integer."})
		return
	}
	order := uint(orderID64)

	res, err := c.academyService.GetContent(ctx.Request.Context(), accountId, academySlug, materialSlug, order)
	ResponseJSON(ctx, gin.H{"academy_slug": academySlug, "material_slug": materialSlug, "content_order": order}, res, err)
}

// ListContentsByMaterial godoc
// @Summary      List Contents by Academy
// @Description  Retrieve a paginated list of all contents in a specific academy
// @Tags         Content
// @Accept       json
// @Produce      json
// @Param        academy_slug  path      string  true   "Academy Slug"
// @Param        limit         query     int     false  "Items per page"  default(10)
// @Param        page          query     int     false  "Page number"     default(1)
// @Param        search        query     string  false  "Search by content title/body or material title"
// @Param        sortBy        query     string  false  "Sort field (title, order, created_at, material_title)"
// @Param        orderBy       query     string  false  "Sort direction (asc / desc)"
// @Success      200           {object}  dto.SuccessResponse[[]entity.AcademyContent]
// @Failure      400           {object}  dto.ErrorResponse
// @Router       /api/v1/academy/{academy_slug}/contents [get]
func (c *academyController) ListContentsByAcademy(ctx *gin.Context) {
	academySlug := ctx.Param("academy_slug")
	accountId := ParseAccountId(ctx)

	limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
	page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
	search := ctx.DefaultQuery("search", "")
	sortBy := ctx.DefaultQuery("sortBy", "")
	order := ctx.DefaultQuery("orderBy", "")
	if order == "" {
		order = ctx.DefaultQuery("order", "")
	}

	if limit < 1 {
		limit = 10
	} else if limit > 100 {
		limit = 100
	}
	if page < 1 {
		page = 1
	}

	offset := (page - 1) * limit
	p := entity.Pagination{Limit: limit, Offset: offset, Search: search, SortBy: sortBy, Order: order}

	list, total, err := c.academyService.ListContentsByAcademy(ctx.Request.Context(), accountId, academySlug, p)
	if err != nil {
		ResponseJSON[any, any](ctx, nil, nil, err)
		return
	}

	totalPages := int((total + int64(limit) - 1) / int64(limit))
	if total == 0 {
		totalPages = 1
	}
	if page > totalPages {
		page = totalPages
	}

	meta := gin.H{
		"academy_slug": academySlug,
		"totalItems":   total,
		"totalPages":   totalPages,
		"currentPage":  page,
		"limit":        limit,
	}
	ResponseJSON(ctx, meta, list, nil)
}

// ListContentsByMaterial godoc
// @Summary      List Contents by Material
// @Description  Retrieve a list of contents for a specific material
// @Tags         Content
// @Accept       json
// @Produce      json
// @Param        id  path      string  true  "Material ID"
// @Success      200 {object}  dto.SuccessResponse[[]entity.AcademyContent]
// @Failure      400 {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) ListContentsByMaterial(ctx *gin.Context) {
	id := ParseUUID(ctx, "id")
	res, err := c.academyService.ListContentsByMaterial(ctx.Request.Context(), id)
	ResponseJSON(ctx, gin.H{"material_id": id}, res, err)
}

// GetContentDetail godoc
// @Summary      Get Content Detail by ID
// @Description  Retrieve detailed content information using its ID
// @Tags         Content
// @Accept       json
// @Produce      json
// @Param        id  path      string  true  "Content ID"
// @Success      200 {object}  dto.SuccessResponse[entity.AcademyContent]
// @Failure      400 {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) GetContentDetail(ctx *gin.Context) {
	id := ParseUUID(ctx, "id")
	res, err := c.academyService.GetContentDetail(ctx.Request.Context(), id)
	ResponseJSON(ctx, gin.H{"id": id}, res, err)
}

// CreateContent godoc
// @Summary      Create Content
// @Description  Create a new content for a material
// @Tags         Content
// @Accept       json
// @Produce      json
// @Param        request  body      dto.CreateContentRequest  true  "Create Content Request"
// @Success      200      {object}  dto.SuccessResponse[entity.AcademyContent]
// @Failure      400      {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) CreateContent(ctx *gin.Context) {
	req := RequestJSON[dto.CreateContentRequest](ctx)
	res, err := c.academyService.CreateContent(ctx.Request.Context(), req)
	ResponseJSON(ctx, req, res, err)
}

// UpdateContent godoc
// @Summary      Update Content
// @Description  Update an existing content
// @Tags         Content
// @Accept       json
// @Produce      json
// @Param        id       path      string                     true  "Content ID"
// @Param        request  body      dto.UpdateContentRequest  true  "Update Content Request"
// @Success      200      {object}  dto.SuccessResponse[entity.AcademyContent]
// @Failure      400      {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) UpdateContent(ctx *gin.Context) {
	id := ParseUUID(ctx, "id")
	req := RequestJSON[dto.UpdateContentRequest](ctx)
	res, err := c.academyService.UpdateContent(ctx.Request.Context(), id, req)
	ResponseJSON(ctx, req, res, err)
}

// DeleteContent godoc
// @Summary      Delete Content
// @Description  Delete an existing content
// @Tags         Content
// @Accept       json
// @Produce      json
// @Param        id   path      string  true  "Content ID"
// @Success      200  {object}  dto.SuccessResponse[any]
// @Failure      400  {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) DeleteContent(ctx *gin.Context) {
	id := ParseUUID(ctx, "id")
	err := c.academyService.DeleteContent(ctx.Request.Context(), id)
	ResponseJSON(ctx, gin.H{"id": id}, gin.H{"deleted": true}, err)
}

// ================= PROGRESS =================
// UpdateContentProgress godoc
// @Summary      Update Content Progress
// @Description  Update the progress of a content within a material
// @Tags         Progress
// @Accept       json
// @Produce      json
// @Param        academy_slug   path      string  true  "Academy Slug"
// @Param        material_slug  path      string  true  "Material Slug"
// @Param        order          path      int     true  "Content Order"
// @Success      200            {object}  dto.SuccessResponse[any]
// @Failure      400            {object}  dto.ErrorResponse
// @Security     BearerAuth
// @Router       /api/v1/academy/{academy_slug}/{material_slug}/{order} [post]
func (c *academyController) UpdateContentProgress(ctx *gin.Context) {
	accountId := ParseAccountId(ctx)
	academySlug := ctx.Param("academy_slug")
	materialSlug := ctx.Param("material_slug")

	orderID64, err := strconv.ParseUint(ctx.Param("order"), 10, 64)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid 'order' parameter. Must be a positive integer."})
		return
	}
	order := uint(orderID64)

	contentProgress, materialProgress, academyProgress, err := c.academyService.UpdateContentProgress(ctx.Request.Context(), accountId, academySlug, materialSlug, order)

	res := gin.H{
		"content_progress":  contentProgress,
		"material_progress": materialProgress,
		"academy_progress":  academyProgress,
	}

	ResponseJSON(ctx, gin.H{"academy_slug": academySlug, "material_slug": materialSlug, "content_order": order}, res, err)
}

// JoinAcademyByCode godoc
// @Summary      Join Academy by Code
// @Description  Join an academy using a unique code
// @Tags         Academy
// @Accept       json
// @Produce      json
// @Param        request  body      dto.JoinAcademyByCodeRequest  true  "Join Academy by Code Request"
// @Success      200      {object}  dto.SuccessResponse[dto.AcademyMiniDetailResponse]
// @Failure      400      {object}  dto.ErrorResponse
// @Failure      402      {object}  dto.RegisterAcademyPaymentActionRequiredResponse
// @Security     BearerAuth
// @Router       /api/v1/academy/join [post]
func (c *academyController) JoinAcademyByCode(ctx *gin.Context) {
	req := RequestJSON[dto.JoinAcademyByCodeRequest](ctx)

	if err := utils.ValidateCode(req.Code); err != nil {
		ResponseJSON[any, any](ctx, req, nil, http_error.INVALID_CODE)
		return
	}

	accountId := ParseAccountId(ctx)
	res, err := c.academyService.JoinByCode(ctx.Request.Context(), accountId, req.Code)

	if errors.Is(err, http_error.PAYMENT_REQUIRED) {
		ctx.JSON(http.StatusPaymentRequired, dto.RegisterAcademyPaymentActionRequiredResponse{
			Data: dto.RegisterAcademyPaymentActionRequiredData{
				AccountID:          res.Payment.AccountId.String(),
				Amount:             res.Payment.Amount,
				AcademyID:          res.Payment.AcademyId.String(),
				ExpiredAt:          res.Payment.ExpiredAt.Format(time.RFC3339),
				ID:                 res.Payment.Id.String(),
				InvoiceID:          res.Payment.InvoiceId,
				InvoiceURL:         res.Payment.InvoiceUrl,
				Status:             res.Payment.Status,
				TransactionAt:      res.Payment.TransactionAt.Format(time.RFC3339),
				MayarTransactionID: res.Payment.ExternalId,
			},
			Message:  http_error.PAYMENT_REQUIRED.Error(),
			MetaData: req.Code,
			Status:   "action_required",
		})
		return
	}

	ResponseJSON(ctx, req, res, err)
}

// AssignAccountToAcademy godoc
// @Summary      Assign Account to Academy
// @Description  Assign an account to an academy
// @Tags         Academy
// @Accept       json
// @Produce      json
// @Param        request  body      dto.AssignRequest  true  "Assign Account to Academy Request"
// @Success      200      {object}  dto.SuccessResponse[entity.AcademyAssign]
// @Failure      400      {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) AssignAccountToAcademy(ctx *gin.Context) {
	req := RequestJSON[dto.AssignRequest](ctx)
	academyId, errA := uuid.Parse(req.AcademyId)
	accountId, errB := uuid.Parse(req.AccountId)

	if errA != nil || errB != nil {
		ResponseJSON[any, any](ctx, nil, nil, http_error.BAD_REQUEST_ERROR)
		return
	}

	res, err := c.academyService.AssignAccountToAcademy(ctx.Request.Context(), academyId, accountId)
	ResponseJSON(ctx, req, res, err)
}

// UnassignAccountFromAcademy godoc
// @Summary      Unassign Account from Academy
// @Description  Unassign an account from an academy
// @Tags         Academy
// @Accept       json
// @Produce      json
// @Param        id   path      string  true  "Assignment ID"
// @Success      200  {object}  dto.SuccessResponse[any]
// @Failure      400  {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) UnassignAccountFromAcademy(ctx *gin.Context) {
	id := ParseUUID(ctx, "id")
	err := c.academyService.UnassignAccountFromAcademy(ctx.Request.Context(), id)
	ResponseJSON(ctx, gin.H{"id": id}, gin.H{"deleted": true}, err)
}

// ListAssignmentsByAcademy godoc
// @Summary      List Assignments by Academy
// @Description  Retrieve a list of assignments for a specific academy
// @Tags         Academy
// @Accept       json
// @Produce      json
// @Param        academy_id  path	  string  true  "Academy ID"
// @Success      200         {object}  dto.SuccessResponse[[]entity.AcademyAssign]
// @Failure      400         {object}  dto.ErrorResponse
// @Security     BearerAuth
func (c *academyController) ListAssignmentsByAcademy(ctx *gin.Context) {
	academyId := ParseUUID(ctx, "academy_id")
	res, err := c.academyService.ListAssignmentsByAcademy(ctx.Request.Context(), academyId)
	ResponseJSON(ctx, gin.H{"academy_id": academyId}, res, err)
}