// import { Redis } from "@upstash/redis"
import { POST as hubspotHandler } from "../hubspot/route";
import { NextRequest } from 'next/server';
import axios from "axios";
// const redis = new Redis({
//   url: process.env.REDIS_URL,
//   token: process.env.REDIS_TOKEN,
// })

export async function POST(req: any) {
  try {
    const body = await req.json();
    console.log(body,"recved from zapier");
    const urldata = body.urldata;
    // const userId = body.userId;
    const email = body.email;
    const entryId=body.entryId;
    // await redis.set(`slide_url:${userId}`, urldata, {
    //   ex: 3600 * 24
    // })
    if(entryId!=null){
      await axios.patch(`${process.env.NEXT_PUBLIC_IDEA_API_URL}/data/mypitchsavedata`, { url:urldata , entryId:entryId });
      }
       
    const hubspotReq = new NextRequest(`${process.env.BASE_URL}/api/hubspot`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        email: email,
        url: urldata,
        entryId:entryId,
      }),
    });
    
    await hubspotHandler(hubspotReq);
    return new Response(
      JSON.stringify({
        success: true,
        slideUrl: urldata,
      }),
      {
        status: 200,
        headers: { "Content-Type": "application/json" },
      }
    );
  } catch (error: any) {
    console.error("Error processing request:", error);
    return new Response(
      JSON.stringify({
        success: false,
        error: error.message,
      }),
      {
        status: 500,
        headers: { "Content-Type": "application/json" },
      }
    );
  }
}

// export async function GET(req: any) {
//   const url = new URL(req.url);
//   const userId = url.searchParams.get("userId");
//   const slideUrl = await redis.get(`slide_url:${userId}`)
//   return new Response(
//     JSON.stringify({
//       slideUrl: slideUrl,
//     }),
//     {
//       status: 200,
//       headers: { "Content-Type": "application/json" },
//     }
//   );
// }
