import directus from "./directus";
import { readItems, type Query } from "@directus/sdk";
import type { DirectusSchema, Review } from "../types/codyops-reviews";

const isDev = import.meta.env.DEV; // Astro's way to check for development mode

export const reviews = await directus.request(
    readItems<DirectusSchema, 'codyops_reviews', Query<DirectusSchema, Review>>("codyops_reviews", {
        fields: [
            'stars',
            'date_created',
            'course_id',
            'feedback',
            {
              user_created: [
                  'first_name',
                  'last_name',
                  'avatar'
              ]
            },
        ],
        filter: {
            status: isDev ? { '_neq': 'archived' } : { '_eq': 'published' }
        },
      }
    )
);

export function getReviewsCourse(course: any) {
    return reviews.filter((review: any) => review.course_id === course);
};
