//
// Copyright (C) 2024 veypi <i@veypi.com>
// 2025-03-04 16:08:06
// Distributed under terms of the MIT license.
//

package settings

import (
	"github.com/veypi/vbase/cfg"
	"github.com/veypi/vbase/models"
	"github.com/veypi/vigo"
)

// ListRequest 列表请求
type ListRequest struct {
	Category *string `src:"query" desc:"分类过滤"`
}

// ListResponse 列表响应
type ListResponse struct {
	Items []models.Setting `json:"items"`
}

// list 获取设置列表
func list(x *vigo.X, req *ListRequest) (*ListResponse, error) {
	db := cfg.DB()

	var settings []models.Setting
	query := db.Order("category, `key`")

	if req.Category != nil && *req.Category != "" {
		query = query.Where("category = ?", req.Category)
	}

	if err := query.Find(&settings).Error; err != nil {
		return nil, vigo.ErrInternalServer.WithError(err)
	}

	return &ListResponse{
		Items: settings,
	}, nil
}
