import { handleOAuthCallback } from "@/lib/oauth-flow";
import { exchangeHubSpotCodeForTokens, getHubSpotUserInfo } from "@/lib/hubspot-oauth";

export async function GET(request: Request) {
  return handleOAuthCallback(request, {
    expectedProvider: "hubspot",
    exchange: (code) => exchangeHubSpotCodeForTokens(code),
    getConnectionData: async (tokens) => {
      const userInfo = await getHubSpotUserInfo(tokens.access_token);
      return {
        accountEmail: `${userInfo.user} (${userInfo.hub_domain})`,
        expiresAt: new Date(Date.now() + tokens.expires_in! * 1000),
        refreshToken: tokens.refresh_token,
      };
    },
  });
}
