package apple

import (
	"context"
	"errors"

	commonLogic "github.com/perfect-panel/server/internal/logic/common"
	"github.com/perfect-panel/server/internal/model/user"
	"github.com/perfect-panel/server/internal/svc"
	"gorm.io/gorm"
)

func findSingleModeMergeTarget(ctx context.Context, svcCtx *svc.ServiceContext, userId int64, subscribeId int64) (*user.Subscribe, error) {
	anchorSub, err := svcCtx.UserModel.FindSingleModeAnchorSubscribe(ctx, userId)
	switch {
	case err == nil && anchorSub != nil && anchorSub.Id > 0:
		if subscribeId > 0 && anchorSub.SubscribeId != subscribeId {
			return nil, commonLogic.ErrSingleModePlanMismatch
		}
		return anchorSub, nil
	case err != nil && !errors.Is(err, gorm.ErrRecordNotFound):
		return nil, err
	}

	userSubs, queryErr := svcCtx.UserModel.QueryUserSubscribe(ctx, userId, 0, 1, 2, 3, 4, 5)
	if queryErr != nil {
		return nil, queryErr
	}
	var candidate *user.SubscribeDetails
	for _, item := range userSubs {
		if item == nil {
			continue
		}
		if subscribeId > 0 && item.SubscribeId != subscribeId {
			continue
		}
		if candidate == nil ||
			item.ExpireTime.After(candidate.ExpireTime) ||
			(item.ExpireTime.Equal(candidate.ExpireTime) && item.UpdatedAt.After(candidate.UpdatedAt)) ||
			(item.ExpireTime.Equal(candidate.ExpireTime) && item.UpdatedAt.Equal(candidate.UpdatedAt) && item.Id > candidate.Id) {
			candidate = item
		}
	}
	if candidate == nil {
		return nil, gorm.ErrRecordNotFound
	}
	return &user.Subscribe{
		Id:          candidate.Id,
		UserId:      candidate.UserId,
		OrderId:     candidate.OrderId,
		SubscribeId: candidate.SubscribeId,
		StartTime:   candidate.StartTime,
		ExpireTime:  candidate.ExpireTime,
		FinishedAt:  candidate.FinishedAt,
		Traffic:     candidate.Traffic,
		Download:    candidate.Download,
		Upload:      candidate.Upload,
		Token:       candidate.Token,
		UUID:        candidate.UUID,
		Status:      candidate.Status,
		Note:        candidate.Note,
		CreatedAt:   candidate.CreatedAt,
		UpdatedAt:   candidate.UpdatedAt,
	}, nil
}
