'use server';

import { IPostResponseData, IPostThumbResponseData } from '@/app/api/blog/data';
import api from '@/lib/axios';
import { IResponse } from '@/models/response';
import { cookies } from 'next/headers';

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

export async function getPost(postId: string): Promise<IPostResponseData> {
  const cookiesStore = await cookies();
  return api
    .get(`/blog/${postId}`, {
      headers: {
        Cookie: cookiesStore.toString(),
      },
    })
    .then(({ data }) => {
      return Promise.resolve(data.data);
    })
    .catch((error) => Promise.reject(error));
}
