#!/usr/bin/env tsx

import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

async function createTestSubscription() {
  try {
    console.log('🧪 Creating test subscription...')
    
    // 使用真实的数据
    const subscriptionData = {
      userId: '9975b2c5-7955-48cf-9594-74b8d9beab25',
      subscriptionPlanId: 'pro',
      stripeSubscriptionId: 'sub_1Rt8nRLW0cChKPJ0Osn5UBcV',
      stripeCustomerId: 'cus_SojwymqWZ4EXlZ',
      isActive: true,
      status: 'active',
      startDate: new Date(1754492307 * 1000), // 2025-08-06T14:58:27.000Z
      endDate: new Date(1757170707 * 1000),   // 2025-09-06T14:58:27.000Z
      metadata: {
        test: true,
        created_by: 'manual_script'
      }
    }
    
    console.log('📊 Creating subscription with data:', subscriptionData)
    
    // 检查是否已存在
    const existing = await prisma.subscription.findFirst({
      where: {
        stripeSubscriptionId: subscriptionData.stripeSubscriptionId
      }
    })
    
    if (existing) {
      console.log('⚠️ Subscription already exists:', existing.id)
      console.log('🗑️ Deleting existing subscription...')
      await prisma.subscription.delete({
        where: { id: existing.id }
      })
    }
    
    // 创建新订阅
    const newSubscription = await prisma.subscription.create({
      data: subscriptionData
    })
    
    console.log('✅ Created subscription:', newSubscription.id)
    
    // 更新用户的订阅计划
    await prisma.user.update({
      where: { id: subscriptionData.userId },
      data: {
        subscriptionPlanId: 'pro',
        subscribePlan: 'pro'
      }
    })
    
    console.log('✅ Updated user subscription plan')
    
    // 验证创建结果
    const createdSubscription = await prisma.subscription.findUnique({
      where: { id: newSubscription.id },
      include: {
        user: {
          select: {
            email: true,
            subscriptionPlanId: true,
            subscribePlan: true
          }
        },
        subscriptionPlan: {
          select: {
            name: true,
            price: true
          }
        }
      }
    })
    
    console.log('📋 Subscription details:')
    console.log('  ID:', createdSubscription?.id)
    console.log('  User:', createdSubscription?.user.email)
    console.log('  Plan:', createdSubscription?.subscriptionPlan.name)
    console.log('  Price:', `$${createdSubscription?.subscriptionPlan.price}`)
    console.log('  Status:', createdSubscription?.status)
    console.log('  Active:', createdSubscription?.isActive)
    console.log('  Start:', createdSubscription?.startDate)
    console.log('  End:', createdSubscription?.endDate)
    console.log('  User Plan ID:', createdSubscription?.user.subscriptionPlanId)
    console.log('  User Plan:', createdSubscription?.user.subscribePlan)
    
    console.log('🎉 Test subscription created successfully!')
    
  } catch (error) {
    console.error('❌ Error creating test subscription:', error)
  } finally {
    await prisma.$disconnect()
  }
}

createTestSubscription()
