package order

import (
	"context"
	"strings"
	"testing"

	"github.com/perfect-panel/server/internal/model/user"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func TestPaidSubscriptionQueryIncludesOrderBackedSubscriptionWithoutToken(t *testing.T) {
	db, err := gorm.Open(mysql.New(mysql.Config{
		DSN:                       "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local",
		SkipInitializeWithVersion: true,
	}), &gorm.Config{DryRun: true, DisableAutomaticPing: true})
	if err != nil {
		t.Fatalf("open dry-run db: %v", err)
	}

	var sub user.Subscribe
	tx := paidSubscriptionQuery(context.Background(), db, 510).First(&sub)
	sql := tx.Statement.SQL.String()

	if strings.Contains(sql, "token != ''") {
		t.Fatalf("paid subscription query should not require non-empty token: %s", sql)
	}
	if !strings.Contains(sql, "order_id > 0 OR token LIKE 'iap:%'") {
		t.Fatalf("paid subscription query should include order-backed or iap-backed subscriptions: %s", sql)
	}
}
