package logic

import (
	"context"
	"errors"
	"time"

	plantModel "sundynix-micro-go/app/plant/model"
	"sundynix-micro-go/app/plant/rpc/internal/svc"
	"sundynix-micro-go/app/plant/rpc/plant"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type CreateExchangeOrderLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateExchangeOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateExchangeOrderLogic {
	return &CreateExchangeOrderLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 创建兑换订单（阳光值扣减+库存扣减事务）
func (l *CreateExchangeOrderLogic) CreateExchangeOrder(in *plant.CreateExchangeOrderReq) (*plant.CommonResp, error) {
	txErr := l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
		// 1. 查询并锁定商品
		var item plantModel.ExchangeItem
		if err := tx.Set("gorm:query_option", "FOR UPDATE").
			Where("id = ? AND status = 1", in.ItemId).First(&item).Error; err != nil {
			if errors.Is(err, gorm.ErrRecordNotFound) {
				return errors.New("商品不存在或已下架")
			}
			return err
		}

		quantity := int(in.Quantity)
		if quantity <= 0 {
			quantity = 1
		}
		cost := item.CostSunlight
		if cost == 0 {
			cost = item.Cost
		}
		totalCost := cost * int64(quantity)
		now := time.Now()
		if item.StartTime != nil && now.Before(*item.StartTime) {
			return errors.New("兑换尚未开始")
		}
		if item.EndTime != nil && now.After(*item.EndTime) {
			return errors.New("兑换已结束")
		}
		if item.LimitPerUser > 0 {
			var count int64
			tx.Model(&plantModel.ExchangeOrder{}).
				Where("user_id = ? AND item_id = ? AND status <> ?", in.UserId, in.ItemId, 5).
				Count(&count)
			if int(count)+quantity > item.LimitPerUser {
				return errors.New("已达到兑换上限")
			}
		}

		// 2. 检查库存（-1 表示无限库存）
		if item.Stock >= 0 && item.Stock < quantity {
			return errors.New("库存不足")
		}

		// 3. 扣减阳光值（原子操作，RowsAffected==0 说明阳光不足）
		result := tx.Model(&plantModel.UserProfile{}).
			Where("user_id = ? AND current_sunlight >= ?", in.UserId, totalCost).
			Update("current_sunlight", gorm.Expr("current_sunlight - ?", totalCost))
		if result.Error != nil {
			return result.Error
		}
		if result.RowsAffected == 0 {
			return errors.New("阳光值不足")
		}

		// 4. 扣减库存（-1无限库存时跳过）
		if item.Stock >= 0 {
			if err := tx.Model(&plantModel.ExchangeItem{}).
				Where("id = ? AND stock >= ?", in.ItemId, quantity).
				Update("stock", gorm.Expr("stock - ?", quantity)).Error; err != nil {
				return err
			}
		}

		// 5. 创建订单
		order := plantModel.ExchangeOrder{
			UserID:        in.UserId,
			ItemID:        in.ItemId,
			ItemName:      item.Name,
			Cost:          totalCost,
			CostSunlight:  totalCost,
			Quantity:      quantity,
			ItemType:      item.Type,
			RecipientName: in.RecipientName,
			Phone:         in.Phone,
			Address:       in.Address,
			Status:        1,
		}
		if item.Type == "VIRTUAL" {
			order.Status = 4
			order.CompletedAt = &now
		}
		return tx.Create(&order).Error
	})
	if txErr != nil {
		return nil, txErr
	}
	return &plant.CommonResp{Code: 0, Msg: "ok"}, nil
}
