#!/usr/bin/env tsx

import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

async function testSubscriptionTable() {
  try {
    console.log('🔍 Testing subscription table...')
    
    // 测试查询所有订阅
    const subscriptions = await prisma.subscription.findMany({
      include: {
        user: {
          select: {
            id: true,
            email: true,
            stripeCustomerId: true
          }
        },
        subscriptionPlan: {
          select: {
            id: true,
            name: true,
            price: true
          }
        }
      }
    })
    
    console.log(`📊 Found ${subscriptions.length} subscriptions:`)
    subscriptions.forEach((sub, index) => {
      console.log(`${index + 1}. Subscription ${sub.id}:`)
      console.log(`   - User: ${sub.user.email} (${sub.user.id})`)
      console.log(`   - Plan: ${sub.subscriptionPlan.name} ($${sub.subscriptionPlan.price})`)
      console.log(`   - Status: ${sub.status}`)
      console.log(`   - Active: ${sub.isActive}`)
      console.log(`   - Stripe ID: ${sub.stripeSubscriptionId}`)
      console.log(`   - Start: ${sub.startDate}`)
      console.log(`   - End: ${sub.endDate}`)
      console.log('')
    })
    
    // 测试查询用户
    const users = await prisma.user.findMany({
      where: {
        stripeCustomerId: {
          not: null
        }
      },
      select: {
        id: true,
        email: true,
        stripeCustomerId: true,
        subscriptionPlanId: true,
        subscribePlan: true
      }
    })
    
    console.log(`👥 Found ${users.length} users with Stripe customer IDs:`)
    users.forEach((user, index) => {
      console.log(`${index + 1}. ${user.email}:`)
      console.log(`   - User ID: ${user.id}`)
      console.log(`   - Stripe Customer ID: ${user.stripeCustomerId}`)
      console.log(`   - Subscription Plan ID: ${user.subscriptionPlanId}`)
      console.log(`   - Subscribe Plan: ${user.subscribePlan}`)
      console.log('')
    })
    
    // 测试查询订阅套餐
    const plans = await prisma.subscriptionPlan.findMany()
    console.log(`📦 Found ${plans.length} subscription plans:`)
    plans.forEach((plan, index) => {
      console.log(`${index + 1}. ${plan.name} (${plan.id}):`)
      console.log(`   - Price: $${plan.price}`)
      console.log(`   - Stripe Price ID: ${plan.stripePriceId}`)
      console.log(`   - Active: ${plan.isActive}`)
      console.log('')
    })
    
  } catch (error) {
    console.error('❌ Error testing subscription table:', error)
  } finally {
    await prisma.$disconnect()
  }
}

testSubscriptionTable()
