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 validPubCategories = map[string]bool{
	"intl-journal-conf":     true,
	"domestic-journal-conf": true,
}

type PublicationsHandler struct {
	repo *repository.PublicationsRepository
}

func NewPublicationsHandler(repo *repository.PublicationsRepository) *PublicationsHandler {
	return &PublicationsHandler{repo: repo}
}

func (h *PublicationsHandler) GetPublications(c *gin.Context) {
	category := c.Query("category")
	if category != "" && !validPubCategories[category] {
		httpx.BadRequest(c, "Invalid category: must be 'intl-journal-conf' or 'domestic-journal-conf'")
		return
	}

	pubs, err := h.repo.GetPublications(c.Request.Context(), category)
	if err != nil {
		httpx.InternalServerError(c, "Failed to retrieve publications: "+err.Error())
		return
	}
	httpx.OK(c, pubs)
}

func (h *PublicationsHandler) CreatePublication(c *gin.Context) {
	var req models.Publication
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.Title == "" || req.Venue == "" || req.PublishedAt == "" {
		httpx.BadRequest(c, "title, venue, and published_at are required")
		return
	}
	if !validPubCategories[req.Category] {
		httpx.BadRequest(c, "category must be 'intl-journal-conf' or 'domestic-journal-conf'")
		return
	}
	id, err := h.repo.CreatePublication(c.Request.Context(), req)
	if err != nil {
		httpx.InternalServerError(c, "Failed to create publication: "+err.Error())
		return
	}
	req.ID = id
	httpx.Created(c, req)
}

func (h *PublicationsHandler) UpdatePublication(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil || id <= 0 {
		httpx.BadRequest(c, "Invalid publication ID")
		return
	}
	var req models.Publication
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.Title == "" || req.Venue == "" || req.PublishedAt == "" {
		httpx.BadRequest(c, "title, venue, and published_at are required")
		return
	}
	if !validPubCategories[req.Category] {
		httpx.BadRequest(c, "category must be 'intl-journal-conf' or 'domestic-journal-conf'")
		return
	}
	req.ID = id
	if err := h.repo.UpdatePublication(c.Request.Context(), id, req); err != nil {
		httpx.InternalServerError(c, "Failed to update publication: "+err.Error())
		return
	}
	httpx.OK(c, req)
}

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

func (h *PublicationsHandler) GetHighlights(c *gin.Context) {
	pubs, err := h.repo.GetHighlights(c.Request.Context())
	if err != nil {
		httpx.InternalServerError(c, "Failed to retrieve highlight publications: "+err.Error())
		return
	}
	httpx.OK(c, pubs)
}

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

func (h *PublicationsHandler) CreatePatent(c *gin.Context) {
	var req models.Patent
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.Title == "" || req.Inventors == "" || req.ApplicationNo == "" || req.ApplicationAt == "" || req.Country == "" || req.PublishedAt == "" {
		httpx.BadRequest(c, "title, inventors, application_no, application_at, country, and published_at are required")
		return
	}
	id, err := h.repo.CreatePatent(c.Request.Context(), req)
	if err != nil {
		httpx.InternalServerError(c, "Failed to create patent: "+err.Error())
		return
	}
	req.ID = id
	httpx.Created(c, req)
}

func (h *PublicationsHandler) UpdatePatent(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil || id <= 0 {
		httpx.BadRequest(c, "Invalid patent ID")
		return
	}
	var req models.Patent
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.Title == "" || req.Inventors == "" || req.ApplicationNo == "" || req.ApplicationAt == "" || req.Country == "" || req.PublishedAt == "" {
		httpx.BadRequest(c, "title, inventors, application_no, application_at, country, and published_at are required")
		return
	}
	req.ID = id
	if err := h.repo.UpdatePatent(c.Request.Context(), id, req); err != nil {
		httpx.InternalServerError(c, "Failed to update patent: "+err.Error())
		return
	}
	httpx.OK(c, req)
}

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