#!/usr/bin/env node
/**
 * Create Additional Seat Product in Stripe
 *
 * This script creates a Stripe product and price for additional seats.
 * The price is set at £9/month per seat.
 *
 * Usage: node scripts/create-seat-product.js
 */

require('dotenv').config();

const Stripe = require('stripe');

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
  apiVersion: '2025-09-30.clover',
});

async function createSeatProduct() {
  console.log('Creating additional seat product in Stripe...\n');

  try {
    // Check if product already exists
    const existingProducts = await stripe.products.list({ limit: 100 });
    const existingSeatProduct = existingProducts.data.find(
      p => p.metadata?.type === 'additional_seat'
    );

    if (existingSeatProduct) {
      console.log('Seat product already exists:');
      console.log(`  Product ID: ${existingSeatProduct.id}`);
      console.log(`  Name: ${existingSeatProduct.name}`);

      // Check for existing price
      const prices = await stripe.prices.list({
        product: existingSeatProduct.id,
        limit: 10
      });

      if (prices.data.length > 0) {
        console.log('\nExisting prices:');
        prices.data.forEach(p => {
          console.log(`  - ${p.id}: £${p.unit_amount / 100}/${p.recurring?.interval}`);
        });
        return;
      }
    }

    // Create the product
    const product = existingSeatProduct || await stripe.products.create({
      name: 'Additional Team Seat',
      description: 'Add extra team members to your organization',
      metadata: {
        type: 'additional_seat',
      },
    });

    console.log(`Product created/found: ${product.id}`);

    // Create monthly price (£9/month)
    const monthlyPrice = await stripe.prices.create({
      product: product.id,
      currency: 'gbp',
      unit_amount: 900, // £9.00 in pence
      recurring: {
        interval: 'month',
      },
      metadata: {
        type: 'additional_seat',
      },
    });

    console.log(`\nMonthly price created: ${monthlyPrice.id}`);
    console.log(`  Amount: £${monthlyPrice.unit_amount / 100}/month`);

    // Create yearly price (£9 * 12 * 0.93 = £100.44/year with 7% discount)
    const yearlyPrice = await stripe.prices.create({
      product: product.id,
      currency: 'gbp',
      unit_amount: 10044, // £100.44 in pence (7% yearly discount)
      recurring: {
        interval: 'year',
      },
      metadata: {
        type: 'additional_seat',
      },
    });

    console.log(`\nYearly price created: ${yearlyPrice.id}`);
    console.log(`  Amount: £${yearlyPrice.unit_amount / 100}/year (7% discount)`);

    console.log('\n✅ Seat product setup complete!');
    console.log('\nProduct details:');
    console.log(`  Product ID: ${product.id}`);
    console.log(`  Monthly Price ID: ${monthlyPrice.id}`);
    console.log(`  Yearly Price ID: ${yearlyPrice.id}`);

  } catch (error) {
    console.error('Error creating seat product:', error.message);
    process.exit(1);
  }
}

createSeatProduct();
