package main

import (
	"bindbox-game/internal/repository/mysql/model"
	"fmt"
	"log"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func main() {
	dsn := "root:bindbox2025kdy@tcp(150.158.78.154:3306)/bindbox_game?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatalf("failed to connect database: %v", err)
	}

	userID := int64(9082) // User from the report

	// 1. Check Orders (ALL Status)
	var orders []model.Orders
	if err := db.Where("user_id = ?", userID).Find(&orders).Error; err != nil {
		log.Printf("Error querying orders: %v", err)
	}

	var totalAmount int64
	var discountAmount int64
	var pointsAmount int64

	fmt.Printf("--- ALL Orders for User %d ---\n", userID)
	for _, o := range orders {
		fmt.Printf("ID: %d, OrderNo: %s, Status: %d, Total: %d, Actual: %d, Discount: %d, Points: %d, Source: %d\n",
			o.ID, o.OrderNo, o.Status, o.TotalAmount, o.ActualAmount, o.DiscountAmount, o.PointsAmount, o.SourceType)
		if o.Status == 2 { // Only count Paid for Spending simulation (if that's the logic)
			totalAmount += o.TotalAmount
			discountAmount += o.DiscountAmount
			pointsAmount += o.PointsAmount
		}
	}
	fmt.Printf("Total Points (Status 2): %d\n", pointsAmount)

	// 1.5 Check Points Ledger (Redemptions)
	var ledgers []model.UserPointsLedger
	if err := db.Where("user_id = ? AND action = ?", userID, "redeem_reward").Find(&ledgers).Error; err != nil {
		log.Printf("Error querying ledgers: %v", err)
	}
	var totalRedeemedPoints int64
	fmt.Printf("\n--- Points Redemption (Decomposition) ---\n")
	for _, l := range ledgers {
		fmt.Printf("ID: %d, Points: %d, Remark: %s, CreatedAt: %v\n", l.ID, l.Points, l.Remark, l.CreatedAt)
		totalRedeemedPoints += l.Points
	}
	fmt.Printf("Total Redeemed Points: %d\n", totalRedeemedPoints)

	// 2. Check Inventory (Output)
	type InvItem struct {
		ID        int64
		ProductID int64
		Status    int32
		Price     int64
		Name      string
		Remark    string // Added Remark field
	}
	var invItems []InvItem

	// Show ALL status
	err = db.Table("user_inventory").
		Select("user_inventory.id, user_inventory.product_id, user_inventory.status, user_inventory.remark, products.price, products.name").
		Joins("JOIN products ON products.id = user_inventory.product_id").
		Where("user_inventory.user_id = ?", userID).
		Where("user_inventory.remark NOT LIKE ? AND user_inventory.remark NOT LIKE ?", "%redeemed%", "%void%").
		Scan(&invItems).Error
	if err != nil {
		log.Printf("Error querying inventory: %v", err)
	}

	var totalPrizeValue int64
	var status1Value int64
	var status2Value int64
	var status3Value int64

	fmt.Printf("\n--- Inventory (ALL Status) for User %d ---\n", userID)
	for _, item := range invItems {
		fmt.Printf("InvID: %d, ProductID: %d, Name: %s, Price: %d, Status: %d, Remark: %s\n",
			item.ID, item.ProductID, item.Name, item.Price, item.Status, item.Remark)

		if item.Status == 1 || item.Status == 3 {
			totalPrizeValue += item.Price
		}
		if item.Status == 1 {
			status1Value += item.Price
		}
		if item.Status == 2 {
			status2Value += item.Price
		}
		if item.Status == 3 {
			status3Value += item.Price
		}
	}
	fmt.Printf("Status 1 (Holding) Value: %d\n", status1Value)
	fmt.Printf("Status 2 (Void/Decomposed) Value: %d\n", status2Value)
	fmt.Printf("Status 3 (Shipped/Used) Value: %d\n", status3Value)
	fmt.Printf("Total Effective Prize Value (1+3): %d\n", totalPrizeValue)

	// 3. Calculate Profit
	profit := totalAmount - totalPrizeValue - discountAmount
	fmt.Printf("\n--- Calculation ---\n")
	fmt.Printf("Profit = Spending (%d) - PrizeValue (%d) - Discount (%d) = %d\n",
		totalAmount, totalPrizeValue, discountAmount, profit)

	fmt.Printf("Formatted:\nSpending: %.2f\nOutput: %.2f\nProfit: %.2f\n", float64(totalAmount)/100, float64(totalPrizeValue)/100, float64(profit)/100)
}
