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 validMemberDegrees = map[string]bool{
	"Professor":    true,
	"PhD":          true,
	"PhDCandidate": true,
	"PhDStudent":   true,
	"MSCandidate":  true,
	"MSStudent":    true,
}

var validAlumniDegrees = map[string]bool{
	"PhD": true,
	"MS":  true,
}

type MembersHandler struct {
	repo *repository.MembersRepository
}

func NewMembersHandler(repo *repository.MembersRepository) *MembersHandler {
	return &MembersHandler{repo: repo}
}

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

func (h *MembersHandler) CreateMember(c *gin.Context) {
	var req models.Member
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.NameKo == "" || req.NameEn == "" {
		httpx.BadRequest(c, "name_ko and name_en are required")
		return
	}
	if !validMemberDegrees[req.Degree] {
		httpx.BadRequest(c, "degree must be one of: Professor, PhD, PhDCandidate, PhDStudent, MSCandidate, MSStudent")
		return
	}
	id, err := h.repo.CreateMember(c.Request.Context(), req)
	if err != nil {
		httpx.InternalServerError(c, "Failed to create member: "+err.Error())
		return
	}
	req.ID = id
	httpx.Created(c, req)
}

func (h *MembersHandler) UpdateMember(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil || id <= 0 {
		httpx.BadRequest(c, "Invalid member ID")
		return
	}
	var req models.Member
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.NameKo == "" || req.NameEn == "" {
		httpx.BadRequest(c, "name_ko and name_en are required")
		return
	}
	if !validMemberDegrees[req.Degree] {
		httpx.BadRequest(c, "degree must be one of: Professor, PhD, PhDCandidate, PhDStudent, MSCandidate, MSStudent")
		return
	}
	req.ID = id
	if err := h.repo.UpdateMember(c.Request.Context(), id, req); err != nil {
		httpx.InternalServerError(c, "Failed to update member: "+err.Error())
		return
	}
	httpx.OK(c, req)
}

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

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

func (h *MembersHandler) CreateAlumnus(c *gin.Context) {
	var req models.Alumnus
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.NameKo == "" || req.NameEn == "" || req.GraduatedAt == "" {
		httpx.BadRequest(c, "name_ko, name_en, and graduated_at are required")
		return
	}
	if !validAlumniDegrees[req.Degree] {
		httpx.BadRequest(c, "degree must be one of: PhD, MS")
		return
	}
	id, err := h.repo.CreateAlumnus(c.Request.Context(), req)
	if err != nil {
		httpx.InternalServerError(c, "Failed to create alumnus: "+err.Error())
		return
	}
	req.ID = id
	httpx.Created(c, req)
}

func (h *MembersHandler) UpdateAlumnus(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil || id <= 0 {
		httpx.BadRequest(c, "Invalid alumnus ID")
		return
	}
	var req models.Alumnus
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.NameKo == "" || req.NameEn == "" || req.GraduatedAt == "" {
		httpx.BadRequest(c, "name_ko, name_en, and graduated_at are required")
		return
	}
	if !validAlumniDegrees[req.Degree] {
		httpx.BadRequest(c, "degree must be one of: PhD, MS")
		return
	}
	req.ID = id
	if err := h.repo.UpdateAlumnus(c.Request.Context(), id, req); err != nil {
		httpx.InternalServerError(c, "Failed to update alumnus: "+err.Error())
		return
	}
	httpx.OK(c, req)
}

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