package setting

import (
	"context"

	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

type ListSettingsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewListSettingsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListSettingsLogic {
	return &ListSettingsLogic{ctx: ctx, svcCtx: svcCtx}
}

func (l *ListSettingsLogic) ListSettings(req *types.SettingPath) (*types.SettingListData, error) {
	items, total, page, pageSize, err := l.svcCtx.Setting.List(l.ctx, req.Scope, req.ScopeID, req.Page, req.PageSize)
	if err != nil {
		return nil, err
	}
	data := make([]types.SettingData, 0, len(items))
	for _, item := range items {
		data = append(data, toSettingData(item))
	}
	totalPages := int64(0)
	if pageSize > 0 {
		totalPages = (total + pageSize - 1) / pageSize
	}
	return &types.SettingListData{
		Pagination: types.PaginationData{
			Total:      total,
			Page:       page,
			PageSize:   pageSize,
			TotalPages: totalPages,
		},
		List: data,
	}, nil
}
