package category

import (
	"context"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"wechat-api/internal/utils/dberrorhandler"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetApiCategoryListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetApiCategoryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApiCategoryListLogic {
	return &GetApiCategoryListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx}
}

func (l *GetApiCategoryListLogic) GetApiCategoryList() (*types.CategoryListResp, error) {
	data, err := l.svcCtx.DB.Category.Query().All(l.ctx)

	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, nil)
	}

	resp := &types.CategoryListResp{}
	resp.Msg = errormsg.Success
	resp.Data.Total = uint64(len(data))

	for _, v := range data {
		resp.Data.Data = append(resp.Data.Data,
			types.CategoryInfo{
				BaseIDInfo: types.BaseIDInfo{
					Id:        &v.ID,
					CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
					UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
				},
				Name: &v.Name,
			})
	}

	return resp, nil
}
