package controller

import (
	"server/common"
	user_service "server/modules/user/service"
	yxDto "server/modules/yx/dto"
	yx_service "server/modules/yx/service"
	"strconv"

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

type UserMajorController struct {
	userScoreService          *user_service.UserScoreService
	yxUserScoreService        *yx_service.YxUserScoreService
	yxCalculationMajorService *yx_service.YxCalculationMajorService
}

func NewUserMajorController() *UserMajorController {
	return &UserMajorController{
		yxUserScoreService:        yx_service.NewYxUserScoreService(),
		userScoreService:          user_service.NewUserScoreService(),
		yxCalculationMajorService: yx_service.NewYxCalculationMajorService(),
	}
}

// RegisterRoutes 注册路由
func (ctrl *UserMajorController) RegisterRoutes(rg *gin.RouterGroup) {
	group := rg.Group("/user/major")
	{
		// group.GET("/", ctrl.GetActive)
		// group.GET("/:id", ctrl.GetByID)
		group.GET("/list", ctrl.List)
		group.GET("/list_by_school", ctrl.ListBySchool)
	}
}

// ListBySchool 获取当前院校下其他专业数据
// @Summary      获取当前院校下其他专业数据
// @Tags         用户专业
// @Param        page  query  int  false  "页码"  default(1)
// @Param        size  query  int  false  "每页数量"  default(10)
// @Param        school_code query string true "院校代码"
// @Param        probability  query  string  false  "录取概率类型(难录取/可冲击/较稳妥/可保底)"  default("")
// @Success      200  {object}  common.Response
// @Router       /user/major/list_by_school [get]
func (ctrl *UserMajorController) ListBySchool(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))
	schoolMajorQuery := yxDto.SchoolMajorQuery{
		Page:        page,
		Size:        size,
		SchoolCode:  c.Query("school_code"),
		Probability: c.DefaultQuery("probability", ""),
		LoginUserId: common.GetLoginUser(c).ID,
	}
	userScoreVO, err := ctrl.userScoreService.GetActiveByID(schoolMajorQuery.LoginUserId)
	if err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	schoolMajorQuery.UserScoreVO = userScoreVO
	items, total, probCount, err := ctrl.yxCalculationMajorService.RecommendMajorList(schoolMajorQuery)
	if err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	newMap := map[string]interface{}{
		"items":     items,
		"total":     total,
		"probCount": probCount,
	}
	common.SuccessPage(c, newMap, total, page, size)
}

// List 获取当前用户可检索列表
// @Summary      获取当前用户可检索列表
// @Tags         用户专业
// @Param        page  query  int  false  "页码"  default(1)
// @Param        size  query  int  false  "每页数量"  default(10)
// @Param		 batch query string false "批次(本科提前批/本科A段/本科B段/本科/高职高专)" default("")
// @Param		 probability  query  string  false  "录取概率类型(难录取/可冲击/较稳妥/可保底)"  default("")
// @Success      200  {object}  common.Response
// @Router       /user/major/list [get]
func (ctrl *UserMajorController) List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))
	schoolMajorQuery := yxDto.SchoolMajorQuery{
		Keyword:     c.DefaultQuery("keyword", ""),
		Page:        page,
		Size:        size,
		SchoolCode:  c.Query("schoolCode"),
		Batch:       c.DefaultQuery("batch", ""),
		Probability: c.DefaultQuery("probability", ""),
		LoginUserId: common.GetLoginUser(c).ID,
	}
	userScoreVO, err := ctrl.userScoreService.GetActiveByID(schoolMajorQuery.LoginUserId)
	if err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	schoolMajorQuery.UserScoreVO = userScoreVO
	items, total, probCount, err := ctrl.yxCalculationMajorService.RecommendMajorList(schoolMajorQuery)
	if err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	newMap := map[string]interface{}{
		"items":     items,
		"total":     total,
		"probCount": probCount,
	}
	common.SuccessPage(c, newMap, total, page, size)
}
