package jd_cookie

import (
	"context"
	v1 "kami/api/jd_cookie/v1"
	"kami/internal/consts"
	"kami/internal/dao"
	"kami/internal/model/entity"
	"kami/utility/config"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gconv"
)

// GetCookieHistory Cookie变更历史
func (s *sJdCookie) GetCookieHistory(ctx context.Context, cookieId string, page, size int, changeType consts.CookieChangeType) (list []*v1.CookieHistoryInfo, total int, err error) {
	if cookieId == "" {
		return nil, 0, gerror.New("Cookie ID不能为空")
	}
	if page <= 0 {
		page = 1
	}
	if size <= 0 {
		size = 20
	}

	m := dao.V1JdCookieChangeHistory.Ctx(ctx).DB(config.GetDatabaseV1())
	m = m.Where(dao.V1JdCookieChangeHistory.Columns().CookieId, cookieId)

	// 如果指定了变更类型，添加过滤条件
	if changeType != "" {
		m = m.Where(dao.V1JdCookieChangeHistory.Columns().ChangeType, string(changeType))
	}

	// 查询总数
	total, err = m.Count()
	if err != nil {
		return nil, 0, gerror.Wrap(err, "查询总数失败")
	}

	// 查询列表数据
	var histories []*entity.V1JdCookieChangeHistory
	err = m.Page(page, size).OrderDesc(dao.V1JdCookieChangeHistory.Columns().CreatedAt).Scan(&histories)
	if err != nil {
		return nil, 0, gerror.Wrap(err, "查询Cookie变更历史失败")
	}

	// 转换为响应格式
	list = make([]*v1.CookieHistoryInfo, 0, len(histories))
	for _, history := range histories {
		info := &v1.CookieHistoryInfo{
			HistoryUuid:  history.HistoryUuid,
			CookieId:     history.CookieId,
			ChangeType:   consts.CookieChangeType(history.ChangeType),
			StatusBefore: consts.JdCookieStatus(gconv.Int(history.StatusBefore)),
			StatusAfter:  consts.JdCookieStatus(gconv.Int(history.StatusAfter)),
			OrderId:      history.OrderId,
			UserOrderId:  history.UserOrderId,
			FailureCount: gconv.Int(history.FailureCount),
			Remark:       history.Remark,
			CreatedAt:    history.CreatedAt,
		}
		list = append(list, info)
	}
	return
}

// GetOrderHistory 订单变更历史
func (s *sJdCookie) GetOrderHistory(ctx context.Context, orderId string, page, size int) (list []*v1.OrderHistoryInfo, total int, err error) {
	if orderId == "" {
		return nil, 0, gerror.New("订单号不能为空")
	}
	if page <= 0 {
		page = 1
	}
	if size <= 0 {
		size = 20
	}

	list = make([]*v1.OrderHistoryInfo, 0)

	return s.getUserOrderHistory(ctx, orderId, page, size)
}

// getUserOrderHistory 获取用户订单变更历史
func (s *sJdCookie) getUserOrderHistory(ctx context.Context, orderId string, page, size int) (list []*v1.OrderHistoryInfo, total int, err error) {
	m := dao.V1JdCookieOrderChangeHistory.Ctx(ctx).DB(config.GetDatabaseV1())
	m = m.Where(dao.V1JdCookieOrderChangeHistory.Columns().OrderId, orderId)

	// 查询总数
	total, err = m.Count()
	if err != nil {
		return nil, 0, gerror.Wrap(err, "查询总数失败")
	}

	// 查询列表数据
	var histories []*entity.V1JdCookieOrderChangeHistory
	err = m.Page(page, size).OrderDesc(dao.V1JdCookieOrderChangeHistory.Columns().CreatedAt).Scan(&histories)
	if err != nil {
		return nil, 0, gerror.Wrap(err, "查询用户订单变更历史失败")
	}

	// 转换为响应格式
	list = make([]*v1.OrderHistoryInfo, 0, len(histories))
	for _, history := range histories {
		info := &v1.OrderHistoryInfo{
			HistoryUuid: history.HistoryUuid,
			OrderId:     history.OrderId,
			ChangeType:  consts.OrderChangeType(history.ChangeType),
			JdOrderId:   history.JdOrderId,
			Remark:      history.Remark,
			CreatedAt:   history.CreatedAt,
		}
		list = append(list, info)
	}

	return
}

// getJdOrderHistory 获取京东订单变更历史
func (s *sJdCookie) getJdOrderHistory(ctx context.Context, jdOrderId string, page, size int) (list []*v1.OrderHistoryInfo, total int, err error) {
	m := dao.V1JdCookieJdOrderChangeHistory.Ctx(ctx).DB(config.GetDatabaseV1())
	m = m.Where(dao.V1JdCookieJdOrderChangeHistory.Columns().JdOrderId, jdOrderId)

	// 查询总数
	total, err = m.Count()
	if err != nil {
		return nil, 0, gerror.Wrap(err, "查询总数失败")
	}

	// 查询列表数据
	var histories []*entity.V1JdCookieJdOrderChangeHistory
	err = m.Page(page, size).OrderDesc(dao.V1JdCookieJdOrderChangeHistory.Columns().CreatedAt).Scan(&histories)
	if err != nil {
		return nil, 0, gerror.Wrap(err, "查询京东订单变更历史失败")
	}

	// 转换为响应格式
	list = make([]*v1.OrderHistoryInfo, 0, len(histories))
	for _, history := range histories {
		info := &v1.OrderHistoryInfo{
			HistoryUuid: history.HistoryUuid,
			OrderId:     history.OrderId,
			ChangeType:  consts.OrderChangeType(history.ChangeType),
			JdOrderId:   history.JdOrderId,
			WxPayUrl:    history.WxPayUrl,
			Remark:      history.Remark,
			CreatedAt:   history.CreatedAt,
		}
		list = append(list, info)
	}

	return
}

// GetJdOrderHistoryByJdOrderId 根据京东订单ID获取京东订单历史
func (s *sJdCookie) GetJdOrderHistoryByJdOrderId(ctx context.Context, jdOrderId string, page, size int) (list []*v1.JdOrderHistoryInfo, total int, err error) {
	if jdOrderId == "" {
		return nil, 0, gerror.New("京东订单号不能为空")
	}
	if page <= 0 {
		page = 1
	}
	if size <= 0 {
		size = 20
	}

	m := dao.V1JdCookieJdOrderChangeHistory.Ctx(ctx).DB(config.GetDatabaseV1())
	m = m.Where(dao.V1JdCookieJdOrderChangeHistory.Columns().JdOrderId, jdOrderId)

	// 查询总数
	total, err = m.Count()
	if err != nil {
		return nil, 0, gerror.Wrap(err, "查询总数失败")
	}

	// 查询列表数据
	var histories []*entity.V1JdCookieJdOrderChangeHistory
	err = m.Page(page, size).OrderDesc(dao.V1JdCookieJdOrderChangeHistory.Columns().CreatedAt).Scan(&histories)
	if err != nil {
		return nil, 0, gerror.Wrap(err, "查询京东订单历史失败")
	}

	// 转换为响应格式
	list = make([]*v1.JdOrderHistoryInfo, 0, len(histories))
	for _, history := range histories {
		info := &v1.JdOrderHistoryInfo{
			HistoryUuid: history.HistoryUuid,
			JdOrderId:   history.JdOrderId,
			ChangeType:  consts.JdOrderChangeType(history.ChangeType),
			OrderId:     history.OrderId,
			WxPayUrl:    history.WxPayUrl,
			Remark:      history.Remark,
			CreatedAt:   history.CreatedAt,
		}
		list = append(list, info)
	}

	return
}
