import { NextRequest, NextResponse } from 'next/server';

import oriyoClient from '@/app/api-utlities/utilities/oriyo.client';

import { validationErrorHandler } from '../../middlewares/error-handler.middleware';

const routeHandler = async (req: NextRequest) => {
  const bonusTokenData = req.cookies.get('bonus__token');

  if (!bonusTokenData) {
    return NextResponse.json(
      { error: 'User does not have access' },
      { status: 401 },
    );
  }

  const { card_id, token } = JSON.parse(bonusTokenData.value);

  const oriyoResponse = await oriyoClient.get('/client/info', {
    params: {
      card_id,
      token,
    },
  });

  return new Response(oriyoResponse.data, {
    headers: { 'Content-Type': 'application/json' },
  });
};

export const GET = validationErrorHandler(routeHandler);
