'use server';

import { auth, currentUser } from '@clerk/nextjs/server';
import { revalidatePath } from 'next/cache';

import { db } from '@/lib/db';
import { createSafeAction } from '@/lib/create-safe-action';
import { absoluteUrl } from '@/lib/utils';
import { stripe } from '@/lib/stripe';

import { InputType, ReturnType } from './types';
import { StripeRedirect } from './schema';

const handler = async (data: InputType): Promise<ReturnType> => {
  const { userId, orgId } = await auth();
  const user = await currentUser();

  if (!userId || !orgId || !user) return { error: 'Unauthorized' };

  const settingsUrl = absoluteUrl(`/organization/${orgId}`);

  let url = '';

  try {
    const orgSubscription = await db.orgSubscription.findUnique({
      where: { orgId },
      select: {
        stripeCustomerId: true,
      },
    });

    if (orgSubscription?.stripeCustomerId) {
      const stripeSession = await stripe.billingPortal.sessions.create({
        customer: orgSubscription.stripeCustomerId,
        return_url: settingsUrl,
      });

      url = stripeSession.url;
    } else {
      /* @ts-ignore */
      const stripeSession = await stripe.checkout.sessions.create({
        success_url: settingsUrl,
        cancel_url: settingsUrl,
        payment_method_types: ['card', 'paypal'],
        mode: 'subscription',
        billing_address_collection: 'auto',
        customer_email: user.emailAddresses[0].emailAddress,
        line_items: [
          {
            price_data: {
              currency: 'usd',
              product_data: {
                name: 'Tasko Pro',
                description: 'Unlimited boards for your organization',
              },
              unit_amount_decimal: 99,
              recurring: { interval: 'month' },
            },
            quantity: 1,
          },
        ],
        metadata: {
          orgId,
        },
      });

      url = stripeSession.url ?? '';
    }
  } catch (error) {
    return {
      error: 'Something went wrong',
    };
  }

  revalidatePath(`/organization/${orgId}`);
  return { data: url };
};

export const stripeRedirect = createSafeAction(StripeRedirect, handler);