package user

import (
	"context"
	"strconv"
	"time"

	"bindbox-game/internal/repository/mysql/dao"
	"bindbox-game/internal/repository/mysql/model"

	"gorm.io/gorm/clause"
)

func (s *service) AddPoints(ctx context.Context, userID int64, points int64, kind string, remark string, validStart *time.Time, validEnd *time.Time) error {
	if points == 0 {
		return nil
	}
	return s.writeDB.Transaction(func(tx *dao.Query) error {
		var existing *model.UserPoints
		if kind != "" {
			existing, _ = tx.UserPoints.WithContext(ctx).Clauses(clause.Locking{Strength: "UPDATE"}).Where(tx.UserPoints.UserID.Eq(userID)).Where(tx.UserPoints.Kind.Eq(kind)).First()
		} else {
			existing, _ = tx.UserPoints.WithContext(ctx).Clauses(clause.Locking{Strength: "UPDATE"}).Where(tx.UserPoints.UserID.Eq(userID)).First()
		}
		if existing == nil {
			item := &model.UserPoints{UserID: userID, Kind: kind, Points: points}
			if validStart != nil {
				item.ValidStart = *validStart
			}
			if validEnd != nil {
				item.ValidEnd = *validEnd
			}
			do := tx.UserPoints.WithContext(ctx)
			if validStart == nil || validStart.IsZero() {
				do = do.Omit(tx.UserPoints.ValidStart)
			}
			if validEnd == nil || validEnd.IsZero() {
				do = do.Omit(tx.UserPoints.ValidEnd)
			}
			// 若有效期未设置，使用 Select 仅写入必要列，避免零日期插入
			if (validStart == nil || validStart.IsZero()) && (validEnd == nil || validEnd.IsZero()) {
				if err := do.Select(tx.UserPoints.UserID, tx.UserPoints.Kind, tx.UserPoints.Points).Create(item); err != nil {
					return err
				}
			} else {
				if err := do.Create(item); err != nil {
					return err
				}
			}

		} else {
			set := map[string]any{"points": existing.Points + points}
			if validStart != nil {
				set["valid_start"] = *validStart
			}
			if validEnd != nil {
				set["valid_end"] = *validEnd
			}
			if _, err := tx.UserPoints.WithContext(ctx).Where(tx.UserPoints.ID.Eq(existing.ID)).Updates(set); err != nil {
				return err
			}
		}
		act := kind
		if act == "" {
			act = "manual_add"
		}
		ledger := &model.UserPointsLedger{UserID: userID, Action: act, Points: points, RefTable: "user_points", RefID: strconv.FormatInt(userID, 10), Remark: remark}
		if err := tx.UserPointsLedger.WithContext(ctx).Create(ledger); err != nil {
			return err
		}
		return nil
	})
}

func (s *service) AddPointsWithAction(ctx context.Context, userID int64, points int64, kind string, remark string, action string, validStart *time.Time, validEnd *time.Time) error {
	if points == 0 {
		return nil
	}
	return s.writeDB.Transaction(func(tx *dao.Query) error {
		var existing *model.UserPoints
		if kind != "" {
			existing, _ = tx.UserPoints.WithContext(ctx).Clauses(clause.Locking{Strength: "UPDATE"}).Where(tx.UserPoints.UserID.Eq(userID)).Where(tx.UserPoints.Kind.Eq(kind)).First()
		} else {
			existing, _ = tx.UserPoints.WithContext(ctx).Clauses(clause.Locking{Strength: "UPDATE"}).Where(tx.UserPoints.UserID.Eq(userID)).First()
		}
		if existing == nil {
			item := &model.UserPoints{UserID: userID, Kind: kind, Points: points}
			if validStart != nil {
				item.ValidStart = *validStart
			}
			if validEnd != nil {
				item.ValidEnd = *validEnd
			}
			do := tx.UserPoints.WithContext(ctx)
			if validStart == nil || validStart.IsZero() {
				do = do.Omit(tx.UserPoints.ValidStart)
			}
			if validEnd == nil || validEnd.IsZero() {
				do = do.Omit(tx.UserPoints.ValidEnd)
			}
			if (validStart == nil || validStart.IsZero()) && (validEnd == nil || validEnd.IsZero()) {
				if err := do.Select(tx.UserPoints.UserID, tx.UserPoints.Kind, tx.UserPoints.Points).Create(item); err != nil {
					return err
				}
			} else {
				if err := do.Create(item); err != nil {
					return err
				}
			}
		} else {
			set := map[string]any{"points": existing.Points + points}
			if validStart != nil {
				set["valid_start"] = *validStart
			}
			if validEnd != nil {
				set["valid_end"] = *validEnd
			}
			if _, err := tx.UserPoints.WithContext(ctx).Where(tx.UserPoints.ID.Eq(existing.ID)).Updates(set); err != nil {
				return err
			}
		}
		act := action
		if act == "" {
			act = "manual_add"
		}
		ledger := &model.UserPointsLedger{UserID: userID, Action: act, Points: points, RefTable: "user_points", RefID: strconv.FormatInt(userID, 10), Remark: remark}
		if err := tx.UserPointsLedger.WithContext(ctx).Create(ledger); err != nil {
			return err
		}
		return nil
	})
}
