// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package database

import (
	"context"
	"time"

	"git.imall.cloud/openim/open-im-server-deploy/pkg/common/storage/model"
	"github.com/openimsdk/tools/db/pagination"
)

// RedPacket defines the operations for managing red packets in MongoDB.
type RedPacket interface {
	// Create creates a new red packet record.
	Create(ctx context.Context, redPacket *model.RedPacket) error
	// Take retrieves a red packet by ID. Returns an error if not found.
	Take(ctx context.Context, redPacketID string) (*model.RedPacket, error)
	// UpdateStatus updates the status of a red packet.
	UpdateStatus(ctx context.Context, redPacketID string, status int32) error
	// UpdateRemain updates the remain amount and count of a red packet.
	UpdateRemain(ctx context.Context, redPacketID string, remainAmount int64, remainCount int32) error
	// DecreaseRemainAtomic 原子性地减少红包剩余数量和金额（防止并发问题）
	// 只有在 remain_count > 0 时才会更新，返回更新后的红包信息
	DecreaseRemainAtomic(ctx context.Context, redPacketID string, amount int64) (*model.RedPacket, error)
	// FindExpiredRedPackets finds red packets that have expired.
	FindExpiredRedPackets(ctx context.Context, beforeTime time.Time) ([]*model.RedPacket, error)
	// FindRedPacketsByUser finds red packets sent by a user with pagination.
	FindRedPacketsByUser(ctx context.Context, userID string, pagination pagination.Pagination) (total int64, redPackets []*model.RedPacket, err error)
	// FindRedPacketsByGroup finds red packets in a group with pagination.
	FindRedPacketsByGroup(ctx context.Context, groupID string, pagination pagination.Pagination) (total int64, redPackets []*model.RedPacket, err error)
	// FindAllRedPackets finds all red packets with pagination.
	FindAllRedPackets(ctx context.Context, pagination pagination.Pagination) (total int64, redPackets []*model.RedPacket, err error)
}

// RedPacketReceive defines the operations for managing red packet receives in MongoDB.
type RedPacketReceive interface {
	// Create creates a new red packet receive record.
	Create(ctx context.Context, receive *model.RedPacketReceive) error
	// Take retrieves a receive record by ID. Returns an error if not found.
	Take(ctx context.Context, receiveID string) (*model.RedPacketReceive, error)
	// FindByRedPacketID finds all receive records for a red packet.
	FindByRedPacketID(ctx context.Context, redPacketID string) ([]*model.RedPacketReceive, error)
	// FindByUserAndRedPacketID finds if a user has received a specific red packet.
	FindByUserAndRedPacketID(ctx context.Context, userID, redPacketID string) (*model.RedPacketReceive, error)
	// FindByUser finds all red packets received by a user with pagination.
	FindByUser(ctx context.Context, userID string, pagination pagination.Pagination) (total int64, receives []*model.RedPacketReceive, err error)
	// DeleteByReceiveID deletes a receive record by receive ID (for cleanup on failure).
	DeleteByReceiveID(ctx context.Context, receiveID string) error
}
