import { stripe } from "./client"

export async function createRentPaymentLink({
  tenantName,
  propertyName,
  unitNumber,
  amount,
  tenantId,
  paymentId,
}: {
  tenantName: string
  propertyName: string
  unitNumber: string
  amount: number
  tenantId: string
  paymentId: string
}) {
  const paymentLink = await stripe.paymentLinks.create({
    line_items: [
      {
        price_data: {
          currency: "usd",
          unit_amount: Math.round(amount * 100),
          product_data: {
            name: `Rent Payment — ${propertyName} Unit ${unitNumber}`,
            description: `Tenant: ${tenantName}`,
          },
        },
        quantity: 1,
      },
    ],
    metadata: {
      tenant_id: tenantId,
      payment_id: paymentId,
      type: "rent_payment",
    },
    after_completion: {
      type: "redirect",
      redirect: {
        url: `${process.env.NEXT_PUBLIC_APP_URL}/tenant-portal/payment-success`,
      },
    },
  })

  return paymentLink
}
