import { r as redirect, e as error } from './index-wpIsICWW.js';
import { type } from 'arktype';
import { eq } from 'drizzle-orm';
import { s as stripe } from './index4-BWaXq8De.js';
import { d as db, f as payment, u as userCard } from './index3-Cipag7JS.js';
import 'stripe';
import 'drizzle-orm/libsql';
import 'drizzle-orm/sqlite-core';
import 'crypto';
import 'dotenv/config.js';

const load = async (event) => {
  const sessionId = event.url.searchParams.get("sessionId");
  if (!sessionId) {
    redirect(303, "/dashboard");
  }
  const session = await stripe.checkout.sessions.retrieve(sessionId);
  const metadataSchema = type({
    userId: "string > 0",
    cardId: "string > 0"
  });
  const validation = metadataSchema(session.metadata);
  if (validation instanceof type.errors) {
    error(400, validation.summary);
  }
  const [paymentItem] = await db.insert(payment).values({
    stripeCheckoutId: session.id,
    userId: validation.userId,
    amount: session.amount_total ?? 0
  }).returning();
  await db.update(userCard).set({
    paymentId: paymentItem.id
  }).where(eq(userCard.id, validation.cardId));
  return paymentItem;
};

var _page_server = /*#__PURE__*/Object.freeze({
  __proto__: null,
  load: load
});

const index = 7;
let component_cache;
const component = async () => component_cache ??= (await import('./_page.svelte-Bck7P4sr.js')).default;
const server_id = "src/routes/thank-you/+page.server.js";
const imports = ["_app/immutable/nodes/7.yOJsEqM_.js","_app/immutable/chunks/cL9VaPgy.js","_app/immutable/chunks/C75YQU_W.js","_app/immutable/chunks/Dj1UN0wv.js","_app/immutable/chunks/Cz-4yBSw.js"];
const stylesheets = [];
const fonts = [];

export { component, fonts, imports, index, _page_server as server, server_id, stylesheets };
//# sourceMappingURL=7-D4gOUAYy.js.map
