package handlers

import (
	"strconv"

	"git.godopu.com/lab/landing_page/backend/internal/httpx"
	"git.godopu.com/lab/landing_page/backend/internal/models"
	"git.godopu.com/lab/landing_page/backend/internal/repository"
	"github.com/gin-gonic/gin"
)

var validTerms = map[string]bool{
	"Spring": true,
	"Fall":   true,
}

type LecturesHandler struct {
	repo *repository.LecturesRepository
}

func NewLecturesHandler(repo *repository.LecturesRepository) *LecturesHandler {
	return &LecturesHandler{repo: repo}
}

func (h *LecturesHandler) GetSemesters(c *gin.Context) {
	semesters, err := h.repo.GetSemestersWithCourses(c.Request.Context())
	if err != nil {
		httpx.InternalServerError(c, "Failed to retrieve semesters: "+err.Error())
		return
	}
	httpx.OK(c, semesters)
}

func (h *LecturesHandler) CreateSemester(c *gin.Context) {
	var req models.Semester
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.Year < 1970 || !validTerms[req.Term] {
		httpx.BadRequest(c, "Valid year and term ('Spring' or 'Fall') are required")
		return
	}
	id, err := h.repo.CreateSemester(c.Request.Context(), req)
	if err != nil {
		httpx.InternalServerError(c, "Failed to create semester: "+err.Error())
		return
	}
	req.ID = id
	httpx.Created(c, req)
}

func (h *LecturesHandler) UpdateSemester(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil || id <= 0 {
		httpx.BadRequest(c, "Invalid semester ID")
		return
	}
	var req models.Semester
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.Year < 1970 || !validTerms[req.Term] {
		httpx.BadRequest(c, "Valid year and term ('Spring' or 'Fall') are required")
		return
	}
	req.ID = id
	if err := h.repo.UpdateSemester(c.Request.Context(), id, req); err != nil {
		httpx.InternalServerError(c, "Failed to update semester: "+err.Error())
		return
	}
	httpx.OK(c, req)
}

func (h *LecturesHandler) DeleteSemester(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil || id <= 0 {
		httpx.BadRequest(c, "Invalid semester ID")
		return
	}
	if err := h.repo.DeleteSemester(c.Request.Context(), id); err != nil {
		httpx.InternalServerError(c, "Failed to delete semester: "+err.Error())
		return
	}
	httpx.NoContent(c)
}

func (h *LecturesHandler) CreateCourse(c *gin.Context) {
	semesterID, err := strconv.Atoi(c.Param("semesterId"))
	if err != nil || semesterID <= 0 {
		httpx.BadRequest(c, "Invalid semesterId parameter")
		return
	}
	var req models.Course
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.Code == "" || req.NameEn == "" {
		httpx.BadRequest(c, "code and name_en are required")
		return
	}
	req.SemesterID = semesterID
	id, err := h.repo.CreateCourse(c.Request.Context(), req)
	if err != nil {
		httpx.InternalServerError(c, "Failed to create course: "+err.Error())
		return
	}
	req.ID = id
	httpx.Created(c, req)
}

func (h *LecturesHandler) UpdateCourse(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil || id <= 0 {
		httpx.BadRequest(c, "Invalid course ID")
		return
	}
	var req models.Course
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.Code == "" || req.NameEn == "" {
		httpx.BadRequest(c, "code and name_en are required")
		return
	}
	req.ID = id
	if err := h.repo.UpdateCourse(c.Request.Context(), id, req); err != nil {
		httpx.InternalServerError(c, "Failed to update course: "+err.Error())
		return
	}
	httpx.OK(c, req)
}

func (h *LecturesHandler) DeleteCourse(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil || id <= 0 {
		httpx.BadRequest(c, "Invalid course ID")
		return
	}
	if err := h.repo.DeleteCourse(c.Request.Context(), id); err != nil {
		httpx.InternalServerError(c, "Failed to delete course: "+err.Error())
		return
	}
	httpx.NoContent(c)
}
