package feedback

import (
	"context"
	"server/internal/dao"
	"server/internal/model"
	"server/internal/model/do"
	"server/internal/service"
	"server/utility/ecode"
)

type sFeedback struct{}

func New() service.IFeedback {
	return &sFeedback{}
}
func init() {
	service.RegisterFeedback(New())
}

// List retrieves a paginated list of feedbacks
func (s *sFeedback) List(ctx context.Context, in *model.FeedbackListIn) (out *model.FeedbackListOut, err error) {
	out = &model.FeedbackListOut{}
	m := dao.Feedbacks.Ctx(ctx)
	if in.UserId != 0 {
		m = m.Where(dao.Feedbacks.Columns().UserId, in.UserId)
	}
	if in.Status != 0 {
		m = m.Where(dao.Feedbacks.Columns().Status, in.Status)
	}
	if err = m.Page(in.Page, in.Size).WithAll().ScanAndCount(&out.List, &out.Total, false); err != nil {
		return
	}
	return
}

// Create adds a new feedback
func (s *sFeedback) Create(ctx context.Context, in *model.FeedbackAddIn) (out *model.FeedbackCRUDOut, err error) {
	if _, err := dao.Feedbacks.Ctx(ctx).Data(do.Feedbacks{
		UserId:  in.UserId,
		Content: in.Content,
		Status:  1, // 默认未处理
	}).Insert(); err != nil {
		return nil, ecode.Fail.Sub("feedback_create_failed")
	}
	return &model.FeedbackCRUDOut{Success: true}, nil
}
