package main

import (
	"fmt"
	"time"

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

type Orders struct {
	ID          int64
	OrderNo     string
	SourceType  int32
	Status      int32
	UserID      int64
	TotalAmount int64
	CreatedAt   time.Time
}
type ActivityDrawLogs struct {
	ID      int64
	OrderID int64
	IssueID int64
}
type ActivityIssues struct {
	ID         int64
	ActivityID int64
}
type Activities struct {
	ID       int64
	PlayType string
	Name     string
}

func (Orders) TableName() string           { return "orders" }
func (ActivityDrawLogs) TableName() string { return "activity_draw_logs" }
func (ActivityIssues) TableName() string   { return "activity_issues" }
func (Activities) TableName() string       { return "activities" }

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

	var count int64
	db.Model(&Orders{}).Count(&count)
	fmt.Printf("Total Orders in DB: %d\n", count)

	var orders []Orders
	if err := db.Order("id DESC").Limit(5).Find(&orders).Error; err != nil {
		fmt.Printf("Error finding orders: %v\n", err)
		return
	}

	fmt.Printf("========== Latest 5 Orders ==========\n")
	for _, o := range orders {
		fmt.Printf("Order %s (ID: %d): Status=%d, SourceType=%d, Amount=%d, Time=%s\n", o.OrderNo, o.ID, o.Status, o.SourceType, o.TotalAmount, o.CreatedAt)
	}
	fmt.Printf("=====================================\n\n")

	checkSourceType(db, 3, "Matching") // SourceType 3 = Matching
	checkSourceType(db, 2, "Ichiban")  // SourceType 2 = Ichiban
	checkPlayType(db, "default", "Default PlayType")
}

func checkPlayType(db *gorm.DB, playType string, label string) {
	fmt.Printf("========== Checking %s (PlayType='%s') ==========\n", label, playType)
	var acts []Activities
	if err := db.Where("play_type = ?", playType).Limit(5).Find(&acts).Error; err != nil {
		fmt.Printf("Error finding activities: %v\n", err)
		return
	}
	for _, a := range acts {
		fmt.Printf("Activity ID=%d Name='%s' PlayType='%s'\n", a.ID, a.Name, a.PlayType)
	}
	fmt.Printf("============================================\n\n")
}

func checkSourceType(db *gorm.DB, sourceType int, label string) {
	fmt.Printf("========== Checking %s (SourceType=%d) ==========\n", label, sourceType)
	var orders []Orders
	// Get last 5 paid orders
	if err := db.Where("source_type = ? AND status = 2", sourceType).Order("id DESC").Limit(5).Find(&orders).Error; err != nil {
		fmt.Printf("Error finding orders: %v\n", err)
		return
	}

	if len(orders) == 0 {
		fmt.Printf("No paid orders found for %s\n", label)
		return
	}

	for _, o := range orders {
		fmt.Printf("Order %s (ID: %d): ", o.OrderNo, o.ID)

		// Find DrawLog
		var log ActivityDrawLogs
		if err := db.Where("order_id = ?", o.ID).First(&log).Error; err != nil {
			fmt.Printf("DrawLog MISSING (%v)\n", err)
			continue
		}

		// Find Issue
		var issue ActivityIssues
		if err := db.Where("id = ?", log.IssueID).First(&issue).Error; err != nil {
			fmt.Printf("Issue MISSING (ID: %d, Err: %v)\n", log.IssueID, err)
			continue
		}

		// Find Activity
		var act Activities
		if err := db.Where("id = ?", issue.ActivityID).First(&act).Error; err != nil {
			fmt.Printf("Activity MISSING (ID: %d, Err: %v)\n", issue.ActivityID, err)
			continue
		}

		fmt.Printf("PlayType='%s' Name='%s' (ActivityID: %d)\n", act.PlayType, act.Name, act.ID)
	}
	fmt.Printf("============================================\n\n")
}
