"use server";

import React from "react";
import Link from "next/link";

import styles from "@/app/(Admin)/dashboard/gallery/gallery.module.css";
import GalleryComponent from "@/components/administrator/Gallery/gallery";

import { BsArrowLeft } from "react-icons/bs";
import { getGallery } from "@/models/gallery.model";

const GalleryPage = async ({ searchParams }) => {
  const query = await searchParams;
  let page = query?.page || 1;
  const limit = 50;
  const sort = query?.sort ? JSON.parse(query.sort) : { createdAt: -1 };
  const search = query?.search || null;

  if (search) page = 1;

  const galleryData = await getGallery(
    { page: parseInt(page), limit },
    sort,
    search
  );

  return (
    <div className={styles.container}>
      <div className={styles.header}>
        <Link
          className={styles.back}
          href="/dashboard">
          <BsArrowLeft /> back
        </Link>
      </div>
      <div className={styles.body}>
        <GalleryComponent initialData={galleryData} />
      </div>
    </div>
  );
};

export default GalleryPage;
