package user

import (
	"bytes"
	"context"
	"encoding/base64"
	"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/clause"
)

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

func (s *service) LoginWeixin(ctx context.Context, in LoginWeixinInput) (*model.Users, 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
	// 事务处理：创建/更新用户 + 处理邀请
	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 {
				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 {
				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
			}
		}

		if u == nil {
			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
			}
			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 != "" {
			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)
					inv := &model.UserInvites{InviterID: inviter.ID, InviteeID: u.ID, InviteCode: in.InviteCode, RewardPoints: reward, 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
						}
					}
					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: reward, ValidStart: time.Now()}
						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 + reward}); err != nil {
							return err
						}
					}
					ledger := &model.UserPointsLedger{UserID: inviter.ID, Action: "invite_reward", Points: reward, RefTable: "user_invites", RefID: strconv.FormatInt(inv.ID, 10), Remark: "invite_reward"}
					if err := tx.UserPointsLedger.WithContext(ctx).Create(ledger); err != nil {
						return err
					}
				}
			}
		}

		// 为新建用户绑定抖音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 u, nil
}
