'use server';

import { IPostCardProps } from '@/app/api/blog/data';
import api from '@/lib/axios';
import { cookies } from 'next/headers';

export interface IResponse<T> {
  data: T;
  pagination: {
    currentPage: number;
    totalPages: number;
    totalItems: number;
    limit: number;
  };
}

export async function getPosts(
  page = 1,
  limit = 6,
): Promise<IResponse<IPostCardProps[]>> {
  const cookiesStore = await cookies();
  return api
    .get('/blog', {
      headers: {
        Cookie: cookiesStore.toString(),
      },
      params: { page, limit },
    })
    .then(({ data }) => {
      return Promise.resolve(data);
    })
    .catch((error) => Promise.reject(error));
}
