"use client";

import { getCookiesDecrypt } from "@/lib/utils";
import { listDataAll } from "@/service/content/content";
import {
  ColumnFiltersState,
  PaginationState,
  SortingState,
  VisibilityState,
} from "@tanstack/react-table";
import { DockIcon, ImageIcon, MicIcon, YoutubeIcon } from "lucide-react";
import { useRouter, useSearchParams } from "next/navigation";
import React from "react";
import search from "../../../app/chat/components/search";

type StatusFilter = string[];

interface Counts {
  images: number;
  audiovisual: number;
  text: number;
  audio: number;
}

const RecentActivity: React.FC = () => {
  const router = useRouter();
  const searchParams = useSearchParams();

  const [dataTable, setDataTable] = React.useState<any[]>([]);
  const [totalData, setTotalData] = React.useState<number>(1);
  const [sorting, setSorting] = React.useState<SortingState>([]);
  const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>(
    []
  );
  const [columnVisibility, setColumnVisibility] =
    React.useState<VisibilityState>({});
  const [rowSelection, setRowSelection] = React.useState({});
  const [pagination, setPagination] = React.useState<PaginationState>({
    pageIndex: 0,
    pageSize: 10,
  });
  const [page, setPage] = React.useState(1);
  const [totalPage, setTotalPage] = React.useState(1);
  const [limit, setLimit] = React.useState(10);
  const [search, setSearch] = React.useState<string>("");
  const userId = getCookiesDecrypt("uie");
  const userLevelId = getCookiesDecrypt("ulie");

  const [categories, setCategories] = React.useState<string[]>();
  const [categoryFilter, setCategoryFilter] = React.useState<string[]>([]);
  const [statusFilter, setStatusFilter] = React.useState<StatusFilter>([]);
  const [startDateString, setStartDateString] = React.useState<string>("");
  const [endDateString, setEndDateString] = React.useState<string>("");
  const [filterByCreator, setFilterByCreator] = React.useState<string>("");
  const [fileTypeFilter, setFileTypeFilter] = React.useState<string[]>([]);
  const [filterBySource, setFilterBySource] = React.useState<string>("");

  const [counts, setCounts] = React.useState<Counts>({
    images: 0,
    audiovisual: 0,
    text: 0,
    audio: 0,
  });

  const roleId = getCookiesDecrypt("urie");

  React.useEffect(() => {
    const pageFromUrl = searchParams?.get("page");
    if (pageFromUrl) {
      setPage(Number(pageFromUrl));
    }
  }, [searchParams]);

  React.useEffect(() => {
    fetchData();
  }, [page, limit]);

  async function fetchData() {
    try {
      const isForSelf = Number(roleId) === 4;
      const res = await listDataAll(
        isForSelf,
        !isForSelf,
        page - 1,
        limit,
        search,
        fileTypeFilter.sort().join(","),
        categoryFilter.sort().join(","),
        statusFilter.sort().join(","),
        statusFilter.sort().join(",").includes("1") ? userLevelId : "",
        filterByCreator,
        filterBySource,
        startDateString,
        endDateString
      );
      const data = res?.data?.data;
      const { content } = data || [];

      // Calculate counts for each typeId
      const newCounts: Counts = {
        images: content.filter((item: any) => item.typeId === 1).length,
        audiovisual: content.filter((item: any) => item.typeId === 2).length,
        text: content.filter((item: any) => item.typeId === 3).length,
        audio: content.filter((item: any) => item.typeId === 4).length,
      };

      setDataTable(content);
      setCounts(newCounts);
    } catch (error) {
      console.error("Error fetching tasks:", error);
    }
  }

  return (
    <div className="flex flex-col gap-5">
      <div className="flex flex-row items-center gap-3">
        <ImageIcon size={40} className="text-blue-700" />
        <p className="text-xl">{counts.images} FOTO</p>
      </div>
      <div className="flex flex-row items-center gap-3">
        <YoutubeIcon size={40} className="text-blue-700" />
        <p className="text-xl">{counts.audiovisual} AUDIO VISUAL</p>
      </div>
      <div className="flex flex-row items-center gap-3">
        <DockIcon size={40} className="text-blue-700" />
        <p className="text-xl">{counts.text} TEXT</p>
      </div>
      <div className="flex flex-row items-center gap-3">
        <MicIcon size={40} className="text-blue-700" />
        <p className="text-xl">{counts.audio} AUDIO</p>
      </div>
    </div>
  );
};

export default RecentActivity;
