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"
)

type HomeHandler struct {
	repo *repository.HomeRepository
}

func NewHomeHandler(repo *repository.HomeRepository) *HomeHandler {
	return &HomeHandler{repo: repo}
}

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

func (h *HomeHandler) CreateResearchProject(c *gin.Context) {
	var req models.ResearchProject
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.Slug == "" || req.Title == "" || req.Abstract == "" {
		httpx.BadRequest(c, "slug, title, and abstract are required")
		return
	}
	id, err := h.repo.CreateResearchProject(c.Request.Context(), req)
	if err != nil {
		httpx.InternalServerError(c, "Failed to create research project: "+err.Error())
		return
	}
	req.ID = id
	httpx.Created(c, req)
}

func (h *HomeHandler) UpdateResearchProject(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil || id <= 0 {
		httpx.BadRequest(c, "Invalid project ID")
		return
	}
	var req models.ResearchProject
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.Slug == "" || req.Title == "" || req.Abstract == "" {
		httpx.BadRequest(c, "slug, title, and abstract are required")
		return
	}
	req.ID = id
	if err := h.repo.UpdateResearchProject(c.Request.Context(), id, req); err != nil {
		httpx.InternalServerError(c, "Failed to update research project: "+err.Error())
		return
	}
	httpx.OK(c, req)
}

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

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

func (h *HomeHandler) CreateResearchArea(c *gin.Context) {
	var req models.ResearchArea
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.NameEn == "" {
		httpx.BadRequest(c, "name_en is required")
		return
	}
	id, err := h.repo.CreateResearchArea(c.Request.Context(), req)
	if err != nil {
		httpx.InternalServerError(c, "Failed to create research area: "+err.Error())
		return
	}
	req.ID = id
	httpx.Created(c, req)
}

func (h *HomeHandler) UpdateResearchArea(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil || id <= 0 {
		httpx.BadRequest(c, "Invalid area ID")
		return
	}
	var req models.ResearchArea
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.NameEn == "" {
		httpx.BadRequest(c, "name_en is required")
		return
	}
	req.ID = id
	if err := h.repo.UpdateResearchArea(c.Request.Context(), id, req); err != nil {
		httpx.InternalServerError(c, "Failed to update research area: "+err.Error())
		return
	}
	httpx.OK(c, req)
}

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

func (h *HomeHandler) GetStatsSummary(c *gin.Context) {
	stats, err := h.repo.GetStatsSummary(c.Request.Context())
	if err != nil {
		httpx.InternalServerError(c, "Failed to retrieve statistics summary: "+err.Error())
		return
	}
	httpx.OK(c, stats)
}
