package user

import (
	"bytes"
	"context"
	"encoding/base64"
	"errors"
	"image/png"
	"time"

	"bindbox-game/internal/pkg/wechat"
	"bindbox-game/internal/repository/mysql/dao"
	"bindbox-game/internal/repository/mysql/model"
	"bindbox-game/internal/service/sysconfig"

	randomname "github.com/DanPlayer/randomname"
	identicon "github.com/issue9/identicon/v2"
	"go.uber.org/zap"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
)

type LoginWeixinInput struct {
	Code        string
	OpenID      string
	UnionID     string
	Nickname    string
	AvatarURL   string
	InviteCode  string
	DouyinID    string
	ChannelCode string
}

// LoginWeixinOutput 微信登录输出结果
type LoginWeixinOutput struct {
	User      *model.Users
	IsNewUser bool
	InviterID int64
}

// LoginWeixin 微信小程序登录
func (s *service) LoginWeixin(ctx context.Context, in LoginWeixinInput) (*LoginWeixinOutput, error) {
	// 1. 获取 OpenID (如果是小程序登录)
	if in.Code != "" {

		// 结合动态配置和静态配置
		wcfg := &wechat.WechatConfig{}
		wcfgVal := sysconfig.GetDynamicConfig().GetWechat(ctx)
		wcfg.AppID = wcfgVal.AppID
		wcfg.AppSecret = wcfgVal.AppSecret

		s.logger.Info("DEBUG: LoginWeixin Config", zap.String("AppID", wcfg.AppID), zap.String("AppSecret", wcfg.AppSecret))
		resp, err := wechat.Code2Session(ctx, wcfg, in.Code)
		if err != nil {
			s.logger.Error("code2session failed", zap.Error(err))
			return nil, err
		}
		in.OpenID = resp.OpenID
		if resp.UnionID != "" {
			in.UnionID = resp.UnionID
		}
	}

	var u *model.Users
	var isNewUser bool
	var inviterID int64

	// 事务处理：创建/更新用户 + 处理邀请
	err := s.writeDB.Transaction(func(tx *dao.Query) error {
		var err error
		// 2. 查找或创建用户
		if in.OpenID != "" {
			u, err = tx.Users.WithContext(ctx).Clauses(clause.Locking{Strength: "UPDATE"}).Where(tx.Users.Openid.Eq(in.OpenID)).First()
			if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
				return err
			}
		}
		if u == nil && in.UnionID != "" {
			u, err = tx.Users.WithContext(ctx).Clauses(clause.Locking{Strength: "UPDATE"}).Where(tx.Users.Unionid.Eq(in.UnionID)).First()
			if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
				return err
			}
		}

		// 查找渠道ID
		var channelID int64
		if in.ChannelCode != "" {
			ch, _ := s.readDB.Channels.WithContext(ctx).Where(s.readDB.Channels.Code.Eq(in.ChannelCode)).First()
			if ch != nil {
				channelID = ch.ID
			}
		}

		isNewUser = false
		if u == nil {
			isNewUser = true
			code := s.generateInviteCode(ctx)
			nickname := in.Nickname
			if nickname == "" {
				nickname = randomname.GenerateName()
			}
			avatar := in.AvatarURL
			if avatar == "" {
				seed := in.OpenID
				if seed == "" {
					seed = nickname
				}
				img := identicon.S2(128).Make([]byte(seed))
				var buf bytes.Buffer
				_ = png.Encode(&buf, img)
				avatar = "data:image/png;base64," + base64.StdEncoding.EncodeToString(buf.Bytes())
			}
			u = &model.Users{
				Nickname:   nickname,
				Avatar:     avatar,
				Openid:     in.OpenID,
				Unionid:    in.UnionID,
				InviteCode: code,
				Status:     1,
				ChannelID:  channelID, // 绑定渠道
			}
			if err := tx.Users.WithContext(ctx).Create(u); err != nil {
				return err
			}
		} else {
			set := map[string]any{}
			if in.Nickname != "" {
				set["nickname"] = in.Nickname
			}
			if in.AvatarURL != "" {
				set["avatar"] = in.AvatarURL
			}
			if in.DouyinID != "" {
				set["douyin_id"] = in.DouyinID
			}
			// 如果此用户是通过 UnionID 找到的，且原本没有 OpenID，则绑定 OpenID
			if u.Openid == "" && in.OpenID != "" {
				set["openid"] = in.OpenID
				u.Openid = in.OpenID
			}
			if channelID > 0 {
				set["channel_id"] = channelID
			}
			if len(set) > 0 {
				if _, err := tx.Users.WithContext(ctx).Where(tx.Users.ID.Eq(u.ID)).Updates(set); err != nil {
					return err
				}
				u, _ = tx.Users.WithContext(ctx).Where(tx.Users.ID.Eq(u.ID)).First()
			}
		}

		// 只有在真正创建新用户记录时才发放邀请奖励
		if in.InviteCode != "" && isNewUser {
			existed, _ := tx.UserInvites.WithContext(ctx).Where(tx.UserInvites.InviteeID.Eq(u.ID)).First()
			if existed == nil {
				inviter, _ := tx.Users.WithContext(ctx).Where(tx.Users.InviteCode.Eq(in.InviteCode)).First()
				if inviter != nil && inviter.ID != u.ID {
					inv := &model.UserInvites{
						InviterID:    inviter.ID,
						InviteeID:    u.ID,
						InviteCode:   in.InviteCode,
						RewardPoints: 0,
						RewardedAt:   time.Now(),
					}
					if err := tx.UserInvites.WithContext(ctx).Create(inv); err != nil {
						return err
					}
					if u.InviterID == 0 {
						if _, err := tx.Users.WithContext(ctx).Where(tx.Users.ID.Eq(u.ID)).Updates(map[string]any{"inviter_id": inviter.ID}); err != nil {
							return err
						}
					}
					// 返回邀请人ID，以便外层触发任务中心逻辑
					inviterID = inviter.ID
					s.logger.Info("微信登录邀请关系建立成功", zap.Int64("user_id", u.ID), zap.Int64("inviter_id", inviter.ID))
				}
			}
		}

		// 为新建用户绑定抖音ID（如果传入）
		if in.DouyinID != "" {
			_, _ = tx.Users.WithContext(ctx).Where(tx.Users.ID.Eq(u.ID)).Updates(map[string]any{"douyin_id": in.DouyinID})
		}

		return nil
	})

	if err != nil {
		return nil, err
	}

	return &LoginWeixinOutput{
		User:      u,
		IsNewUser: isNewUser,
		InviterID: inviterID,
	}, nil
}
