'use client';
import { useState } from "react";
import { useGraphQlBuildsList } from "./queries";
import { UsersBuildDataTable } from "./data-table";

const PageUsersBuildsTableSection = (
    {
        selectedBuildIDS,
        defaultSelection,
        appendBuildID,
        removeBuildID,
        additionButtons,
        setDefaultSelection
    }: {
        selectedBuildIDS: string[];
        defaultSelection: string;
        appendBuildID: (id: string) => void;
        removeBuildID: (id: string) => void;
        additionButtons?: React.ReactNode | null;
        setDefaultSelection: (id: string) => void;
    }
) => {

    const [page, setPage] = useState(1);
    const [limit, setLimit] = useState(10);
    const [sort, setSort] = useState({ createdAt: 'desc' });
    const [filters, setFilters] = useState({});
    const { data, isLoading, error, refetch } = useGraphQlBuildsList({ limit, skip: (page - 1) * limit, sort, filters });

    const handlePageChange = (newPage: number) => { setPage(newPage) };
    const handlePageSizeChange = (newSize: number) => { setLimit(newSize); setPage(1) };
    if (isLoading) { return <div className="flex items-center justify-center p-8">Loading...</div> }
    if (error) { return <div className="flex items-center justify-center p-8 text-red-500">Error loading users</div> }
    return <>
        <UsersBuildDataTable
            data={data?.data || []} totalCount={data?.totalCount || 0} currentPage={page} pageSize={limit} onPageChange={handlePageChange} onPageSizeChange={handlePageSizeChange}
            refetchTable={refetch} selectedBuildIDS={selectedBuildIDS} appendBuildID={appendBuildID} removeBuildID={removeBuildID} additionButtons={additionButtons} defaultSelection={defaultSelection} setDefaultSelection={setDefaultSelection}
        />
    </>;

}

export default PageUsersBuildsTableSection;
