import { eq } from "drizzle-orm";
import { db } from "../../../../database/dbclient.js";
import { invHistoricalData } from "../../../../database/schema/historicalINV.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { format } from "date-fns";

export const historicalInvByDate = async (date: string) => {
    const histDate = new Date(date);

    const { data, error } = (await tryCatch(
        db
            .select()
            .from(invHistoricalData)
            .where(
                eq(invHistoricalData.histDate, format(histDate, "yyyy-MM-dd"))
            )
    )) as any;

    if (error) {
        return {
            success: false,
            message: "There was an error with getting the inventory",
            data: error,
        };
    }

    return {
        success: true,
        message: `Historical inventory for ${date}`,
        data: data,
    };
};
