package bundle

import (
	"context"
	"errors"
	"fonchain-fiee/api/bundle"
	"fonchain-fiee/api/cast"
	"fonchain-fiee/pkg/service"

	bundleModel "fonchain-fiee/pkg/model/bundle"
	"fonchain-fiee/pkg/model/login"

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

func BundleExtend(c *gin.Context) {
	var req bundle.BundleExtendRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.BundleProvider.BundleExtend(context.Background(), &req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
}

func BundleExtendRecordsList(c *gin.Context) {
	var req bundle.BundleExtendRecordsListRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.BundleProvider.BundleExtendRecordsList(context.Background(), &req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
}

func GetBundleBalance(c *gin.Context) {
	var req bundle.GetBundleBalanceListReq
	if err := c.ShouldBindJSON(&req); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.BundleProvider.GetBundleBalanceList(context.Background(), &req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
}

func GetUsedRecordList(c *gin.Context) {
	var req bundle.GetUsedRecordListReq
	if err := c.ShouldBindJSON(&req); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.BundleProvider.GetUsedRecordList(context.Background(), &req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
}

func GetWorkDetail(c *gin.Context) {
	var req cast.WorkDetailReq
	if err := c.ShouldBindJSON(&req); err != nil {
		service.Error(c, err)
		return
	}
	// userInfo := login.GetUserInfoFromC(c)
	// res, err := service.BundleProvider.GetBundleBalanceByUserId(context.TODO(), &bundle.GetBundleBalanceByUserIdReq{
	// 	UserId: int32(userInfo.ID),
	// })
	// if err != nil {
	// 	service.Error(c, err)
	// 	return
	// }
	// if res.ExpiredTime <= time.Now().UnixMilli() {
	// 	service.Success(c, gin.H{
	// 		"status":  1,
	// 		"message": "套餐过期请重新购买",
	// 	})
	// 	return
	// }

	workRes, err := service.CastProvider.WorkDetail(context.Background(), &req)
	if err != nil {
		service.Error(c, err)
		return
	}

	// if len(workRes.Images) > 0 && res.ImageConsumptionNumber >= res.ImageNumber { // 图片用尽
	// 	service.Success(c, gin.H{
	// 		"status":  2,
	// 		"message": "增值服务已用尽",
	// 	})
	// 	return
	// } else {
	// 	if res.VideoConsumptionNumber >= res.VideoNumber { // 视频用尽
	// 		service.Success(c, gin.H{
	// 			"status":  2,
	// 			"message": "增值服务已用尽",
	// 		})
	// 		return
	// 	}
	// }

	service.Success(c, workRes)
}

func GetToBeComfirmedWorks(c *gin.Context) {
	var req bundle.ToBeComfirmedWorksReq
	if err := c.ShouldBindJSON(&req); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.BundleProvider.ToBeComfirmedWorks(context.Background(), &req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
}

func GetUserBalance(c *gin.Context) {
	var req bundle.GetBundleBalanceByUserIdReq
	if err := c.ShouldBindJSON(&req); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.BundleProvider.GetBundleBalanceByUserId(context.Background(), &req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
}

func WorkConfirm(c *gin.Context) { // 确认作品并扣除余量
	var req bundleModel.UserWorkConfirmReq

	if err := c.ShouldBindJSON(&req); err != nil {
		service.Error(c, err)
		return
	}

	if req.ConfirmStatus == 2 { // 驳回完直接结束
		res, err := service.CastProvider.UpdateStatus(c, &cast.UpdateStatusReq{
			WorkAction:    cast.WorkActionENUM_CONFIRM,
			WorkUuid:      req.WorkUuid,
			ConfirmRemark: req.ConfirmRemark,
			ConfirmStatus: 2,
		})
		if err != nil {
			service.Error(c, err)
			return
		}
		service.Success(c, res)
	}
	userInfo := login.GetUserInfoFromC(c)
	balanceInfoRes, err := service.BundleProvider.GetBundleBalanceByUserId(context.Background(), &bundle.GetBundleBalanceByUserIdReq{
		UserId: int32(userInfo.ID),
	})
	if err != nil {
		service.Error(c, err)
		return
	}

	wordInfoRes, err := service.CastProvider.WorkDetail(c, &cast.WorkDetailReq{
		WorkUuid: req.WorkUuid,
	})
	if err != nil {
		service.Error(c, err)
		return
	}

	var workCategory int //  1 图文 2 视频

	if len(wordInfoRes.Images) == 0 { // 临时判断
		workCategory = 2
	} else {
		workCategory = 1
	}

	var addBalanceReq bundle.AddBundleBalanceReq
	switch workCategory {
	case 1:
		{
			if balanceInfoRes.ImageConsumptionNumber >= balanceInfoRes.ImageNumber { // 图文余量不足
				service.Error(c, errors.New("图文余量不足"))
				return
			}
			addBalanceReq.ImageConsumptionNumber = 1
		}
	case 2:
		{
			if balanceInfoRes.VideoConsumptionNumber >= balanceInfoRes.VideoNumber { // 视频余量不足
				service.Error(c, errors.New("视频余量不足"))
				return
			}
			addBalanceReq.VideoConsumptionNumber = 1
		}
	default:
		service.Error(c, errors.New("不支持的类型"))
		return
	}
	res, err := service.BundleProvider.AddBundleBalance(c, &addBalanceReq)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
}
