package user

import (
    "context"
    "time"

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

type CreateUserInput struct {
    Nickname string
    OpenID   string
    Avatar   string
}

func (s *service) CreateUser(ctx context.Context, in CreateUserInput) (*model.Users, error) {
    now := time.Now()
    u := &model.Users{
        Nickname:  in.Nickname,
        Openid:    in.OpenID,
        Avatar:    in.Avatar,
        Status:    1,
        CreatedAt: now,
        UpdatedAt: now,
    }
    if err := s.writeDB.Users.WithContext(ctx).Create(u); err != nil {
        return nil, err
    }
    return u, nil
}

func (s *service) DeleteUser(ctx context.Context, userID int64) error {
    return s.writeDB.Transaction(func(tx *dao.Query) error {
        orders, err := tx.Orders.WithContext(ctx).Where(tx.Orders.UserID.Eq(userID)).Find()
        if err != nil {
            return err
        }
        var orderIDs []int64
        for _, od := range orders {
            orderIDs = append(orderIDs, od.ID)
        }

        if len(orderIDs) > 0 {
            if _, err = tx.OrderItems.WithContext(ctx).Where(tx.OrderItems.OrderID.In(orderIDs...)).Delete(); err != nil {
                return err
            }
            if _, err = tx.PaymentPreorders.WithContext(ctx).Where(tx.PaymentPreorders.OrderID.In(orderIDs...)).Delete(); err != nil {
                return err
            }
            if _, err = tx.PaymentTransactions.WithContext(ctx).Where(tx.PaymentTransactions.OrderID.In(orderIDs...)).Delete(); err != nil {
                return err
            }
            if _, err = tx.PaymentRefunds.WithContext(ctx).Where(tx.PaymentRefunds.OrderID.In(orderIDs...)).Delete(); err != nil {
                return err
            }
        }

        invs, err := tx.UserInventory.WithContext(ctx).Where(tx.UserInventory.UserID.Eq(userID)).Find()
        if err != nil {
            return err
        }
        var invIDs []int64
        for _, iv := range invs {
            invIDs = append(invIDs, iv.ID)
        }

        if _, err = tx.UserInvites.WithContext(ctx).Where(tx.UserInvites.InviterID.Eq(userID)).Or(tx.UserInvites.InviteeID.Eq(userID)).Delete(); err != nil {
            return err
        }
        if _, err = tx.UserInventoryTransfers.WithContext(ctx).Where(tx.UserInventoryTransfers.FromUserID.Eq(userID)).Or(tx.UserInventoryTransfers.ToUserID.Eq(userID)).Delete(); err != nil {
            return err
        }
        if len(invIDs) > 0 {
            if _, err = tx.UserInventoryTransfers.WithContext(ctx).Where(tx.UserInventoryTransfers.InventoryID.In(invIDs...)).Delete(); err != nil {
                return err
            }
        }

        effects := tx.ActivityDrawEffects.WithContext(ctx).Where(tx.ActivityDrawEffects.UserID.Eq(userID))
        if _, err := effects.Delete(); err != nil {
            return err
        }

        logs, err := tx.ActivityDrawLogs.WithContext(ctx).Where(tx.ActivityDrawLogs.UserID.Eq(userID)).Find()
        if err != nil {
            return err
        }
        var drawLogIDs []int64
        for _, lg := range logs {
            drawLogIDs = append(drawLogIDs, lg.ID)
        }
        if len(drawLogIDs) > 0 {
            if _, err = tx.ActivityDrawReceipts.WithContext(ctx).Where(tx.ActivityDrawReceipts.DrawLogID.In(drawLogIDs...)).Delete(); err != nil {
                return err
            }
            if _, err = tx.ActivityDrawEffects.WithContext(ctx).Where(tx.ActivityDrawEffects.DrawLogID.In(drawLogIDs...)).Delete(); err != nil {
                return err
            }
            if _, err = tx.UserItemCards.WithContext(ctx).Where(tx.UserItemCards.UsedDrawLogID.In(drawLogIDs...)).Delete(); err != nil {
                return err
            }
            if _, err = tx.ActivityDrawLogs.WithContext(ctx).Where(tx.ActivityDrawLogs.ID.In(drawLogIDs...)).Delete(); err != nil {
                return err
            }
        }

        if _, err = tx.UserInventory.WithContext(ctx).Where(tx.UserInventory.UserID.Eq(userID)).Delete(); err != nil {
            return err
        }
        if _, err = tx.UserItemCards.WithContext(ctx).Where(tx.UserItemCards.UserID.Eq(userID)).Delete(); err != nil {
            return err
        }
        if _, err = tx.UserCoupons.WithContext(ctx).Where(tx.UserCoupons.UserID.Eq(userID)).Delete(); err != nil {
            return err
        }
        if _, err = tx.Orders.WithContext(ctx).Where(tx.Orders.UserID.Eq(userID)).Delete(); err != nil {
            return err
        }
        if _, err = tx.UserPointsLedger.WithContext(ctx).Where(tx.UserPointsLedger.UserID.Eq(userID)).Delete(); err != nil {
            return err
        }
        if _, err = tx.UserPoints.WithContext(ctx).Where(tx.UserPoints.UserID.Eq(userID)).Delete(); err != nil {
            return err
        }
        
        if _, err = tx.ShippingRecords.WithContext(ctx).Where(tx.ShippingRecords.UserID.Eq(userID)).Delete(); err != nil {
            return err
        }
        if _, err = tx.OpsShippingStats.WithContext(ctx).Where(tx.OpsShippingStats.UserID.Eq(userID)).Delete(); err != nil {
            return err
        }
        if _, err = tx.UserAddresses.WithContext(ctx).Where(tx.UserAddresses.UserID.Eq(userID)).Delete(); err != nil {
            return err
        }

        if _, err = tx.Users.WithContext(ctx).Where(tx.Users.ID.Eq(userID)).Delete(); err != nil {
            return err
        }
        return nil
    })
}
