import { NextResponse } from "next/server";
import Stripe from "stripe";
// import { createClient } from "@supabase/supabase-js";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
// const supabase = createClient(process.env.SUPABASE_SERVICE_ROLE_KEY);

// export async function POST(request) {
//   try {
//     const { sessionId, userId } = await request.json();

//     Retrieve the Stripe session
//     const session = await stripe.checkout.sessions.retrieve(sessionId);

//     if (session.payment_status !== "paid") {
//       return NextResponse.json(
//         { error: "Payment not successful" },
//         { status: 400 }
//       );
//     }

//     Retrieve the subscription details
//     const subscription = await stripe.subscriptions.retrieve(
//       session.subscription
//     );

//     Update the user's subscription data in Supabase
//     const { error } = await supabase
//       .from("subscription_data")
//       .update({
//         active_subscription: true,
//         subscription_id: subscription.id,
//         status: subscription.status,
//         current_period_start: new Date(
//           subscription.current_period_start * 1000
//         ).toISOString(),
//         current_period_end: new Date(
//           subscription.current_period_end * 1000
//         ).toISOString(),
//       })
//       .eq("client_id", userId);

//     if (error) {
//       return NextResponse.json(
//         {
//           error:
//             "Failed to update subscription data when handling successful payment",
//         },
//         { status: 500 }
//       );
//     }

//     return NextResponse.json({ message: "Payment processed successfully" });
//   } catch (error) {
//     console.error("Error handling successful payment:", error);
//     return NextResponse.json({ error: error.message }, { status: 500 });
//   }
// }
