package plant

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"

	"sundynix-go/global"
	"sundynix-go/model/commom/request"
	"sundynix-go/model/commom/response"
	plantReq "sundynix-go/model/plant/request"
	"sundynix-go/utils/auth"
)

type MyPlantApi struct{}

// AddPlant 添加植物
// @Tags 我的植物
// @Summary 添加植物
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body plantReq.CreateMyPlant true "创建植物"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"添加成功"}"
// @Router /plant/add [post]
func (a *MyPlantApi) AddPlant(c *gin.Context) {
	var req plantReq.CreateMyPlant
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	userId := auth.GetUserId(c)
	err = plantService.AddPlant(req, userId)
	if err != nil {
		global.Logger.Error("添加植物失败", zap.Error(err))
		response.FailWithMsg("添加失败", c)
		return
	}
	response.OkWithMsg("添加成功", c)
}

// PlantPage 植物列表
// @Tags 我的植物
// @Summary 植物列表
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body request.PageInfo true "分页获取植物列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /plant/page [post]
func (a *MyPlantApi) PlantPage(c *gin.Context) {
	var req request.PageInfo
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
	}
	userId := auth.GetUserId(c)
	list, total, err := plantService.PlantPage(req, userId)
	if err != nil {
		global.Logger.Error("获取植物列表失败", zap.Error(err))
		response.FailWithMsg("获取植物列表失败", c)
		return
	}
	response.OkWithData(response.PageResult{
		List:     list,
		Total:    total,
		Page:     req.Current,
		PageSize: req.PageSize,
	}, c)
}

// PlantDetail 植物详情
// @Tags 我的植物
// @Summary ById植物详情
// @Security BearerAuth
// @Produce  application/json
// @Param id query string true "id"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取ById成功"}"
// @Router /plant/detail [get]
func (a *MyPlantApi) PlantDetail(c *gin.Context) {
	id := c.Query("id")
	plant, err := plantService.PlantDetail(id)
	if err != nil {
		response.FailWithMsg("获取失败", c)
		return
	}
	response.OkWithData(plant, c)

}

// UpdatePlant 修改植物
// @Tags 我的植物
// @Summary 修改ById植物
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body plantReq.UpdateMyPlant true "修改ById植物"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改ById成功"}"
// @Router /plant/update [post]
func (a *MyPlantApi) UpdatePlant(c *gin.Context) {
	var req plantReq.UpdateMyPlant
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	err = plantService.UpdatePlant(req)
	if err != nil {
		global.Logger.Error("更新植物失败", zap.Error(err))
		response.FailWithMsg("更新植物失败", c)
		return
	}
	response.OkWithMsg("更新植物成功", c)
}

// TodayTask 今日任务
// @Tags 我的植物
// @Summary 今日任务
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /plant/todayTask [get]
func (a *MyPlantApi) TodayTask(c *gin.Context) {
	userId := auth.GetUserId(c)
	list, err := plantService.TodayTask(userId)
	if err != nil {
		global.Logger.Error("获取今日任务失败", zap.Error(err))
		response.FailWithMsg("获取今日任务失败", c)
		return
	}
	response.OkWithData(response.ListResult{
		List: list,
	}, c)
}

// CompleteTask plantReq.CompleteMyPlant
// @Tags 我的植物
// @Summary 完成任务
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body plantReq.CompleteTask true "完成任务"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"完成任务"}"
// @Router /plant/completeTask [post]
func (a *MyPlantApi) CompleteTask(c *gin.Context) {
	var req plantReq.CompleteTask
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	userId := auth.GetUserId(c)
	res, err := plantService.CompleteTask(req, userId)
	if err != nil {
		global.Logger.Error("完成任务失败", zap.Error(err))
		response.FailWithMsg("完成任务失败", c)
		return
	}
	response.OkWithData(res, c)
}

// DeletePlants
// @Tags 我的植物
// @Summary 删除植物
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body request.IdsReq true "删除植物"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /plant/deletePlant [post]
func (a *MyPlantApi) DeletePlants(c *gin.Context) {
	var req request.IdsReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	err = plantService.DeletePlants(req)
	if err != nil {
		global.Logger.Error("删除植物失败", zap.Error(err))
		response.FailWithMsg("删除植物失败", c)
	}
	response.OkWithMsg("删除植物成功", c)
}

// DeletePlans DeletePlants
// @Tags 我的植物
// @Summary 删除任务
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body request.IdsReq true "删除植物"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /plant/deletePlan [post]
func (a *MyPlantApi) DeletePlans(c *gin.Context) {
	var req request.IdsReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	err = plantService.DeletePlans(req)
	if err != nil {
		global.Logger.Error("删除任务失败", zap.Error(err))
		response.FailWithMsg("删除任务失败", c)
	}
	response.OkWithMsg("删除任务成功", c)
}

// AddCarePlan 添加养护事项
// @Tags 我的植物
// @Summary 添加养护事项
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body plantReq.CreateCarePlan true "添加养护事项"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"添加成功"}"
// @Router /plant/plan/add [post]
func (a *MyPlantApi) AddCarePlan(c *gin.Context) {
	var req plantReq.AddPlans
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	err = plantService.AddCarePlan(req)
	if err != nil {
		global.Logger.Error("添加任务失败", zap.Error(err))
		response.FailWithMsg("添加任务失败", c)
		return
	}
	response.OkWithMsg("添加任务成功", c)
}

// DeletePlan 删除养护事项
// @Tags 我的植物
// @Summary 删除养护事项
// @Security BearerAuth
// @Produce  application/json
// @Param id query string true "id"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /plant/plan/delete [get]
func (a *MyPlantApi) DeletePlan(c *gin.Context) {
	id := c.Query("id")
	err := plantService.DeletePlan(id)
	if err != nil {
		global.Logger.Error("删除任务失败", zap.Error(err))
		response.FailWithMsg("删除任务失败", c)
		return
	}
	response.OkWithMsg("删除任务成功", c)
}

// AddGrowthRecord 添加成长记录
// @Tags 我的植物
// @Summary 添加成长记录
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body plantReq.CreateGrowthRecord true "添加成长记录"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"添加成功"}"
// @Router /plant/growth/add [post]
func (a *MyPlantApi) AddGrowthRecord(c *gin.Context) {
	var req plantReq.CreateGrowthRecord
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	userId := auth.GetUserId(c)
	err = plantService.AddGrowthRecord(req, userId)
	if err != nil {
		global.Logger.Error("添加成长记录失败", zap.Error(err))
		response.FailWithMsg("添加成长记录失败", c)
		return
	}
	response.OkWithMsg("添加成长记录成功", c)
}

// QuickCare 快捷养护记录
// @Tags 我的植物
// @Summary 快捷养护记录（无需预设任务）
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body plantReq.QuickCare true "快捷养护"
// @Router /plant/quickCare [post]
func (a *MyPlantApi) QuickCare(c *gin.Context) {
	var req plantReq.QuickCare
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	userId := auth.GetUserId(c)
	err = plantService.QuickCare(req, userId)
	if err != nil {
		global.Logger.Error("快捷养护记录失败", zap.Error(err))
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.OkWithMsg("养护记录已保存", c)
}
