package controller

import (
	"ai_xhs/common"
	"ai_xhs/service"

	"github.com/gin-gonic/gin"
)

type XHSController struct {
	service *service.XHSService
}

func NewXHSController() *XHSController {
	return &XHSController{
		service: &service.XHSService{},
	}
}

// SendCode 发送小红书验证码
func (ctrl *XHSController) SendCode(c *gin.Context) {
	var req struct {
		Phone       string `json:"phone" binding:"required"`
		CountryCode string `json:"country_code"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		common.Error(c, common.CodeInvalidParams, "参数错误：手机号不能为空")
		return
	}

	// 调用Service层发送验证码
	result, err := ctrl.service.SendVerificationCode(req.Phone, req.CountryCode)
	if err != nil {
		common.Error(c, common.CodeInternalError, "调用Python服务失败: "+err.Error())
		return
	}

	// 判断Python服务返回的结果
	if result.Code != 0 {
		common.Error(c, result.Code, result.Message)
		return
	}

	common.SuccessWithMessage(c, result.Message, result.Data)
}

// VerifyCode 验证小红书验证码并登录
func (ctrl *XHSController) VerifyCode(c *gin.Context) {
	var req struct {
		Phone       string `json:"phone" binding:"required"`
		Code        string `json:"code" binding:"required"`
		CountryCode string `json:"country_code"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		common.Error(c, common.CodeInvalidParams, "参数错误：手机号和验证码不能为空")
		return
	}

	// 调用Service层验证登录
	result, err := ctrl.service.VerifyLogin(req.Phone, req.Code, req.CountryCode)
	if err != nil {
		common.Error(c, common.CodeInternalError, "调用Python服务失败: "+err.Error())
		return
	}

	// 判断Python服务返回的结果
	if result.Code != 0 {
		common.Error(c, result.Code, result.Message)
		return
	}

	common.SuccessWithMessage(c, result.Message, result.Data)
}
