package handlers

import (
	"fmt"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/niangaodev/art-code/models"
	"github.com/niangaodev/art-code/repositories"
	"github.com/niangaodev/art-code/utils"
)

// SubmitInquiry 提交咨询
func SubmitInquiry(c *gin.Context) {
	var inquiry models.Inquiry
	if err := c.ShouldBindJSON(&inquiry); err != nil {
		utils.Error(c, 400, "Invalid request")
		return
	}

	if err := repositories.CreateInquiry(&inquiry); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.SuccessWithMsg(c, "Inquiry submitted successfully", nil)
}

// GetEmailSuffixes 获取邮箱后缀
func GetEmailSuffixes(c *gin.Context) {
	suffixes, err := repositories.GetEmailSuffixes()
	if err != nil {
		utils.ServerError(c, err)
		return
	}
	if suffixes == nil {
		utils.Success(c, []interface{}{})
	} else {
		utils.Success(c, suffixes)
	}
}

// AdminGetInquiries 获取咨询列表
func AdminGetInquiries(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "50"))
	if page < 1 {
		page = 1
	}
	if pageSize < 1 {
		pageSize = 50
	}

	filter := repositories.AdminInquiryFilter{
		Keyword:   c.Query("keyword"),
		StartDate: c.Query("startDate"),
		EndDate:   c.Query("endDate"),
		Status:    -1,
	}
	if statusStr := c.Query("status"); statusStr != "" {
		if v, err := strconv.Atoi(statusStr); err == nil {
			filter.Status = v
		}
	}

	inquiries, total, err := repositories.GetInquiriesAdmin(page, pageSize, filter)
	if err != nil {
		utils.ServerError(c, err)
		return
	}
	if inquiries == nil {
		inquiries = []models.Inquiry{}
	}
	utils.Success(c, gin.H{
		"list":  inquiries,
		"total": total,
		"page":  page,
		"size":  pageSize,
	})
}

// AdminUpdateInquiryStatus 更新咨询状态
func AdminUpdateInquiryStatus(c *gin.Context) {
	idStr := c.Param("id")
	var id uint
	if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil {
		utils.Error(c, 400, "Invalid ID")
		return
	}

	var req struct {
		Status int `json:"status"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		utils.Error(c, 400, "Invalid request")
		return
	}

	if err := repositories.UpdateInquiryStatus(id, req.Status); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.SuccessWithMsg(c, "Status updated successfully", nil)
}

// AdminGetEmailSuffixes 获取所有邮箱后缀
func AdminGetEmailSuffixes(c *gin.Context) {
	suffixes, err := repositories.AdminGetEmailSuffixes()
	if err != nil {
		utils.ServerError(c, err)
		return
	}
	if suffixes == nil {
		utils.Success(c, []interface{}{})
	} else {
		utils.Success(c, suffixes)
	}
}

// AdminCreateEmailSuffix 创建邮箱后缀
func AdminCreateEmailSuffix(c *gin.Context) {
	var s models.EmailSuffix
	if err := c.ShouldBindJSON(&s); err != nil {
		utils.Error(c, 400, "Invalid request")
		return
	}

	if err := repositories.CreateEmailSuffix(&s); err != nil {
		utils.ServerError(c, err)
		return
	}
	utils.SuccessWithMsg(c, "Email suffix created successfully", nil)
}

// AdminUpdateEmailSuffix 更新邮箱后缀
func AdminUpdateEmailSuffix(c *gin.Context) {
	idStr := c.Param("id")
	var id uint
	if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil {
		utils.Error(c, 400, "Invalid ID")
		return
	}

	var s models.EmailSuffix
	if err := c.ShouldBindJSON(&s); err != nil {
		utils.Error(c, 400, "Invalid request")
		return
	}
	s.ID = id

	if err := repositories.UpdateEmailSuffix(&s); err != nil {
		utils.ServerError(c, err)
		return
	}
	utils.SuccessWithMsg(c, "Email suffix updated successfully", nil)
}

// AdminDeleteEmailSuffix 删除邮箱后缀
func AdminDeleteEmailSuffix(c *gin.Context) {
	idStr := c.Param("id")
	var id uint
	if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil {
		utils.Error(c, 400, "Invalid ID")
		return
	}

	if err := repositories.DeleteEmailSuffix(id); err != nil {
		utils.ServerError(c, err)
		return
	}
	utils.SuccessWithMsg(c, "Email suffix deleted successfully", nil)
}

// ProxyRequest (Optional, moved from runner if needed or kept there)
func ProxyRequest(c *gin.Context) {
	// ... implementation same as in runner.go if duplicate, otherwise remove
	// Assuming it's in runner.go, removing here if present in original read
}
