package activity

import (
	"context"
	"errors"

	"bindbox-game/internal/repository/mysql/model"
)

// CreateIssue 创建活动期
// 参数: activityID 活动ID, in 创建输入
// 返回: 新建期记录与错误
func (s *service) CreateIssue(ctx context.Context, activityID int64, in CreateIssueInput) (*model.ActivityIssues, error) {
	exists, err := s.readDB.ActivityIssues.WithContext(ctx).Where(s.readDB.ActivityIssues.ActivityID.Eq(activityID), s.readDB.ActivityIssues.IssueNumber.Eq(in.IssueNumber)).Count()
	if err != nil {
		return nil, err
	}
	if exists > 0 {
		return nil, errors.New("issue_number already exists")
	}
	item := &model.ActivityIssues{
		ActivityID:  activityID,
		IssueNumber: in.IssueNumber,
		Status:      in.Status,
		Sort:        in.Sort,
	}
	err = s.writeDB.ActivityIssues.WithContext(ctx).Create(item)
	if err != nil {
		return nil, err
	}
	return item, nil
}
