import axios from 'axios';
import Database from 'cr-db';
import { Message } from 'eris';
import type { Stripe } from 'stripe';
import { Client, CmdContext, Command } from '../class';

export default class Billing_T3 extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 't3';
    this.description = 'Subscription to CS Tier 3.';
    this.usage = `${this.client.config.prefix}billing t3 [promoCode]`;
    this.permissions = 0;
    this.guildOnly = false;
    this.enabled = true;
  }

  public async run(ctx: CmdContext) {
    await ctx.message.delete();
    const response = <{
      found: boolean,
      emailAddress?: string,
      tier?: number,
      supportKey?: string,
    }>(await axios.get(`https://api.cloud.libraryofcode.org/wh/info?id=${ctx.message.author.id}&authorization=${this.client.config.internalKey}`)).data;
    if (!response.found) return this.error(ctx.message.channel, 'CS Account not found.');

    const customer = await this.client.db.mongo.Customer.findOne({ userID: ctx.message.author.id });
    if (!customer) return this.error(ctx.message.channel, `You do not have a Customer Account. Please run \`${this.client.config.prefix}billing\`, once you visit the Billing Portal via the URL given to you, please try again.`);

    let promoCode;
    if (ctx.args[0]) {
      promoCode = await this.client.db.mongo.Promo.findOne({ code: ctx.args[0].toUpperCase() });
    }

    let subscription: Stripe.Response<Stripe.Subscription>;
    try {
      subscription = await this.client.stripe.subscriptions.create({
        customer: customer.cusID,
        payment_behavior: 'allow_incomplete',
        items: [{ price: 'price_1H8e6ODatwI1hQ4WFVvX6Nda' }],
        days_until_due: 1,
        collection_method: 'send_invoice',
        default_tax_rates: ['txr_1HlAadDatwI1hQ4WRHu14S2I'],
        promotion_code: promoCode ? promoCode.id : undefined,
      });
    } catch (err) {
      return this.error(ctx.message.channel, `Error creating subscription.\n\n${err}`);
    }

    await this.client.stripe.invoices.finalizeInvoice(subscription.latest_invoice.toString());
    const invoice = await this.client.stripe.invoices.retrieve(subscription.latest_invoice.toString());

    const chan = await this.client.getDMChannel(ctx.message.author.id);
    await chan.createMessage(`__**Invoice for New Subscription**__\n${invoice.hosted_invoice_url}\n\n*Please click on the link above to pay for your subscription.*`);
    return this.success(ctx.message.channel, 'Transaction processed.');
  }
}
