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 validScopes = map[string]bool{
	"international": true,
	"domestic":      true,
}

var validDocStatuses = map[string]bool{
	"IS":         true,
	"TS":         true,
	"TR":         true,
	"CDV":        true,
	"WDTR":       true,
	"InProgress": true,
}

type StandardizationHandler struct {
	repo *repository.StandardizationRepository
}

func NewStandardizationHandler(repo *repository.StandardizationRepository) *StandardizationHandler {
	return &StandardizationHandler{repo: repo}
}

func (h *StandardizationHandler) GetStandardsBodies(c *gin.Context) {
	bodies, err := h.repo.GetStandardsBodiesWithProjects(c.Request.Context())
	if err != nil {
		httpx.InternalServerError(c, "Failed to retrieve standards bodies: "+err.Error())
		return
	}
	httpx.OK(c, bodies)
}

func (h *StandardizationHandler) CreateStandardsBody(c *gin.Context) {
	var req models.StandardsBody
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.Org == "" || req.FullName == "" || req.Period == "" {
		httpx.BadRequest(c, "org, full_name, and period are required")
		return
	}
	if !validScopes[req.Scope] {
		httpx.BadRequest(c, "scope must be 'international' or 'domestic'")
		return
	}
	id, err := h.repo.CreateStandardsBody(c.Request.Context(), req)
	if err != nil {
		httpx.InternalServerError(c, "Failed to create standards body: "+err.Error())
		return
	}
	req.ID = id
	httpx.Created(c, req)
}

func (h *StandardizationHandler) UpdateStandardsBody(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil || id <= 0 {
		httpx.BadRequest(c, "Invalid standards body ID")
		return
	}
	var req models.StandardsBody
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.Org == "" || req.FullName == "" || req.Period == "" {
		httpx.BadRequest(c, "org, full_name, and period are required")
		return
	}
	if !validScopes[req.Scope] {
		httpx.BadRequest(c, "scope must be 'international' or 'domestic'")
		return
	}
	req.ID = id
	if err := h.repo.UpdateStandardsBody(c.Request.Context(), id, req); err != nil {
		httpx.InternalServerError(c, "Failed to update standards body: "+err.Error())
		return
	}
	httpx.OK(c, req)
}

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

func (h *StandardizationHandler) CreateStandardDocument(c *gin.Context) {
	bodyID, err := strconv.Atoi(c.Param("bodyId"))
	if err != nil || bodyID <= 0 {
		httpx.BadRequest(c, "Invalid bodyId parameter")
		return
	}
	var req models.StandardDocument
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.WG == "" || req.ProjectName == "" || req.Title == "" || req.DocRef == "" {
		httpx.BadRequest(c, "wg, project_name, title, and doc_ref are required")
		return
	}
	if !validDocStatuses[req.Status] {
		httpx.BadRequest(c, "status must be one of: IS, TS, TR, CDV, WDTR, InProgress")
		return
	}
	req.BodyID = bodyID
	id, err := h.repo.CreateStandardDocument(c.Request.Context(), req)
	if err != nil {
		httpx.InternalServerError(c, "Failed to create standard document: "+err.Error())
		return
	}
	req.ID = id
	httpx.Created(c, req)
}

func (h *StandardizationHandler) UpdateStandardDocument(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil || id <= 0 {
		httpx.BadRequest(c, "Invalid document ID")
		return
	}
	var req models.StandardDocument
	if err := c.ShouldBindJSON(&req); err != nil {
		httpx.BadRequest(c, "Invalid request body: "+err.Error())
		return
	}
	if req.WG == "" || req.ProjectName == "" || req.Title == "" || req.DocRef == "" {
		httpx.BadRequest(c, "wg, project_name, title, and doc_ref are required")
		return
	}
	if !validDocStatuses[req.Status] {
		httpx.BadRequest(c, "status must be one of: IS, TS, TR, CDV, WDTR, InProgress")
		return
	}
	req.ID = id
	if err := h.repo.UpdateStandardDocument(c.Request.Context(), id, req); err != nil {
		httpx.InternalServerError(c, "Failed to update standard document: "+err.Error())
		return
	}
	httpx.OK(c, req)
}

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