import { Command, Option } from 'commander'
import { graphql } from '../util/client.ts'
import { requireRemoteMode } from '../util/config.ts'
import { output, outputError } from '../util/format.ts'
import { CREATE_CHECKOUT } from '../util/operations.ts'

const checkoutCommand = new Command('checkout')
  .description('Create a checkout session for a subscription tier')
  .addOption(
    new Option('--tier <tier>', 'Subscription tier')
      .choices(['explorer', 'pro', 'team'])
      .makeOptionMandatory(),
  )
  .action(async (opts: { tier: string }) => {
    try {
      requireRemoteMode('billing checkout')
      const data = await graphql<{ createCheckout: { url: string } }>(
        CREATE_CHECKOUT,
        { tier: opts.tier },
      )
      output(data.createCheckout)
    } catch (error) {
      outputError(error)
    }
  })

export const billingCommand = new Command('billing')
  .description('Billing and subscription management')
  .addCommand(checkoutCommand)
