import { getOcpLogs } from "@/utils/querys/production/ocpLogs";
import { ocpColumns } from "@/utils/tableData/production/ocpLogs/ocpLogColumns";
import { OcpLogTable } from "@/utils/tableData/production/ocpLogs/ocpLogData";
import { useQuery } from "@tanstack/react-query";
import axios from "axios";

import { useMemo } from "react";
import { toast } from "sonner";

export default function OcpLogs() {
    const { data, isError, isLoading } = useQuery(getOcpLogs("4"));

    const clearLog = async (log: any) => {
        try {
            const res = await axios.patch(`/api/logger/logs/${log.log_id}`);

            if (res.data.success) {
                toast.success(`Log message: ${log.message}, was just cleared`);
            } else {
                console.log(res);
                toast.error(`There was an error clearing the message.`);
            }
        } catch (error) {
            toast.error(`There was an error trying to clearing the message.`);
        }
    };
    const columns = useMemo(() => ocpColumns(clearLog), [clearLog]);
    const logData = data ? data : [];
    if (isError) {
        return <div>Error</div>;
    }

    if (isLoading) {
        return <div>Loading</div>;
    }

    return (
        <div className="m-2">
            <OcpLogTable
                columns={columns}
                data={logData}
                //style={style}
            />
        </div>
    );
}
