"use client";

import { getEvents, getCategories } from "./utils";
import { calendarEvents, Category } from "./data";
import CalendarView from "./calender-view";
import { getCookiesDecrypt } from "@/lib/utils";
import { useEffect, useState } from "react";
import { CalendarCategory } from "./data";

const CalenderPage = () => {
  const [categories, setCategories] = useState<CalendarCategory[]>([]);
  const userLevelNumber = Number(getCookiesDecrypt("ulne")) || 0;
  const userLevelId = Number(getCookiesDecrypt("ulie")) || 0;
  const userParentLevelId = Number(getCookiesDecrypt("uplie")) || 0;

  useEffect(() => {
    initData();

    async function initData() {
      const events = await getEvents();
      const categories = await getCategories();
      let valueShowed: string[] = [];
      if (userLevelNumber == 1) {
        valueShowed = ["1", "2", "3", "4", "5"];
      } else if (userLevelNumber == 2 && userLevelId != 761) {
        valueShowed = ["2", "3"];
      } else if (
        (userLevelNumber == 2 && userLevelId == 761) ||
        (userLevelNumber == 3 && userParentLevelId == 761)
      ) {
        valueShowed = ["4"];
      } else if (userLevelNumber == 3 && userParentLevelId != 761) {
        valueShowed = ["3"];
      }

      const formattedCategories = categories
        .filter((category: Category) => valueShowed.includes(category.value))
        .map((category: Category) => ({
          ...category,
          activeClass: "",
        }));
      console.log(formattedCategories);
      setCategories(formattedCategories);
    }
  }, []);

  return <div>{categories && <CalendarView categories={categories} />}</div>;
};

export default CalenderPage;
