'use server';

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

export async function getProjects(page = 1 as number) {
  const cookiesStore = await cookies();

  return api
    .get('/projects', {
      headers: {
        Cookie: cookiesStore.toString(),
      },
      params: { page },
    })
    .then(({ data }) => Promise.resolve(data))
    .catch((error) => Promise.reject(error));
}

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