package activity

import (
	"context"
	"time"
)

// ModifyActivity 修改活动
// 参数: id 活动ID, in 修改输入
// 返回: 错误信息
func (s *service) ModifyActivity(ctx context.Context, id int64, in ModifyActivityInput) error {
    updates := map[string]any{s.writeDB.Activities.UpdatedAt.ColumnName().String(): time.Now()}
	if in.Name != "" {
		updates[s.writeDB.Activities.Name.ColumnName().String()] = in.Name
	}
	if in.Banner != "" {
		updates[s.writeDB.Activities.Banner.ColumnName().String()] = in.Banner
	}
	if in.Image != "" {
		updates[s.writeDB.Activities.Image.ColumnName().String()] = in.Image
	}
	if in.GameplayIntro != "" {
		updates[s.writeDB.Activities.GameplayIntro.ColumnName().String()] = sanitizeHTML(in.GameplayIntro)
	}
	if in.ActivityCategoryID != 0 {
		updates[s.writeDB.Activities.ActivityCategoryID.ColumnName().String()] = in.ActivityCategoryID
	}
	if in.Status != 0 {
		updates[s.writeDB.Activities.Status.ColumnName().String()] = in.Status
	}
	if in.PriceDraw != 0 {
		updates[s.writeDB.Activities.PriceDraw.ColumnName().String()] = in.PriceDraw
	}
	if in.IsBoss != 0 {
		updates[s.writeDB.Activities.IsBoss.ColumnName().String()] = in.IsBoss
	}
	if in.StartTime != nil {
		updates[s.writeDB.Activities.StartTime.ColumnName().String()] = *in.StartTime
	}
    if in.EndTime != nil {
        updates[s.writeDB.Activities.EndTime.ColumnName().String()] = *in.EndTime
    }
    if in.AllowItemCards != nil {
        updates["allow_item_cards"] = (*in.AllowItemCards != 0)
    }
    if in.AllowCoupons != nil {
        updates["allow_coupons"] = (*in.AllowCoupons != 0)
    }
    if len(updates) == 1 {
        return nil
    }
	_, err := s.writeDB.Activities.WithContext(ctx).Where(s.writeDB.Activities.ID.Eq(id)).Updates(updates)
	return err
}
