import 'dotenv/config'
import Stripe from 'stripe'

// Simple seed script to create Stripe products and prices (Stripe Plans API is deprecated).
// Uses Products + Prices APIs as per current Stripe docs.
// Note: Requires a Stripe SECRET key (sk_test_...) in your environment (loaded via dotenv).

async function createStripeProducts() {
  const secretKey = process.env.NUXT_STRIPE_SECRET_KEY
  if (!secretKey) {
    throw new Error('NUXT_STRIPE_SECRET_KEY is not set')
  }
  if (!secretKey.startsWith('sk_')) {
    throw new Error('NUXT_STRIPE_SECRET_KEY must be a secret key (starts with sk_). You appear to be using a restricted/public key.')
  }

  const stripe = new Stripe(secretKey, { apiVersion: '2025-11-17.clover' })

  console.log('Creating Stripe products and prices...')

  const baseProduct = await stripe.products.create({
    name: 'Base',
    description: 'Base subscription plan',
    active: true
  })

  await stripe.prices.create({
    product: baseProduct.id,
    unit_amount: 800, // €8.00 in cents
    currency: 'eur',
    nickname: 'Base Monthly (EUR)',
    recurring: {
      interval: 'month',
      trial_period_days: 7
    }
  })

  const plusProduct = await stripe.products.create({
    name: 'Plus',
    description: 'Plus subscription plan',
    active: true
  })

  await stripe.prices.create({
    product: plusProduct.id,
    unit_amount: 1200, // $12.00 in cents
    currency: 'usd',
    nickname: 'Plus Monthly (USD)',
    recurring: {
      interval: 'month',
      trial_period_days: 7
    }
  })

  console.log('Stripe products and prices created successfully.')
}

createStripeProducts()
  .catch((error) => {
    console.error('Seed process failed:', error)
    process.exit(1)
  })
  .finally(() => {
    console.log('Seed process finished. Exiting...')
    process.exit(0)
  })
