package user

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

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

	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 != "" {
		cfg := configs.Get().Wechat
		wcfg := &wechat.WechatConfig{
			AppID:     cfg.AppID,
			AppSecret: cfg.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 {
					// reward := int64(10) // Removed hardcoded reward as per instruction
					inv := &model.UserInvites{InviterID: inviter.ID, InviteeID: u.ID, InviteCode: in.InviteCode, RewardPoints: 0, RewardedAt: time.Now()} // RewardPoints set to 0 as reward is removed
					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
						}
					}
					points, _ := tx.UserPoints.WithContext(ctx).Clauses(clause.Locking{Strength: "UPDATE"}).Where(tx.UserPoints.UserID.Eq(inviter.ID)).Where(tx.UserPoints.Kind.Eq("invite")).First()
					if points == nil {
						points = &model.UserPoints{UserID: inviter.ID, Kind: "invite", Points: 0, ValidStart: time.Now()} // Points set to 0 as reward is removed
						do := tx.UserPoints.WithContext(ctx)
						if points.ValidEnd.IsZero() {
							do = do.Omit(tx.UserPoints.ValidEnd)
						}
						if err := do.Create(points); err != nil {
							return err
						}
					} else {
						if _, err := tx.UserPoints.WithContext(ctx).Where(tx.UserPoints.ID.Eq(points.ID)).Updates(map[string]any{"points": points.Points + 0}); err != nil { // Points set to 0 as reward is removed
							return err
						}
					}
					ledger := &model.UserPointsLedger{UserID: inviter.ID, Action: "invite_reward", Points: 0, RefTable: "user_invites", RefID: strconv.FormatInt(inv.ID, 10), Remark: "invite_reward"} // Points set to 0 as reward is removed
					if err := tx.UserPointsLedger.WithContext(ctx).Create(ledger); 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
}
