import {where} from "firebase/firestore";
import FirebaseQuery from "../../../firebase/firebaseQuery";
import {useState} from "react";
import { UserData } from "../../../typeDefinitions";

export function useDowntime({user}: {user: UserData}){
    const [downtime, setDowntime] = useState<{title: string, body: string}|false>();
    const firebaseQuery = new FirebaseQuery();

    const fetchDownTime = async () => {
        try {
            const docs = await firebaseQuery.getDocsWhere("downtime", [where("products", "array-contains", user?.product), where("active", "==", true)])
            const downtimeDocs = { ...docs as {[key: string]: {title: string, body: string}}};
            setDowntime(downtimeDocs[Object.keys(downtimeDocs)[0]] || false);
            
        } catch(error) {
            console.log(error)
        }
      
    }

    return ({...{downtime, setDowntime, fetchDownTime}})
    
}