'use client';
import { useState, useEffect } from "react";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { FormValues, createForm } from "./schema";
import { FormAddNewLivingSpace } from "./form";
import { useAddLivingSpaceMutation } from "./queries";
import { Button } from "@/components/ui/button";
import { IconArrowLeftToArc } from "@tabler/icons-react";
import { useRouter } from "next/navigation";

import PageLivingSpaceBuildsTableSection from "../tables/builds/page";
import PageLivingSpaceUserTypesTableSection from "../tables/userType/page";
import PageLivingSpacePartsTableSection from "../tables/part/page";
import PageLivingSpacePersonTableSection from "../tables/person/page";
import PageLivingSpaceCompanyTableSection from "../tables/company/page";

const PageLivingSpaceAdd = () => {

    const router = useRouter();
    const [buildID, setBuildID] = useState<string | null>(null);
    const [userTypeID, setUserTypeID] = useState<string | null>(null);
    const [partID, setPartID] = useState<string | null>(null);
    const [companyID, setCompanyID] = useState<string | null>(null);
    const [personID, setPersonID] = useState<string | null>(null);
    const [isProperty, setIsProperty] = useState<boolean | null>(null);

    const form = createForm({ buildID, userTypeID, partID, companyID, personID });

    useEffect(() => {
        form.setValue("buildID", buildID || ""); form.setValue("userTypeID", userTypeID || ""); form.setValue("partID", partID || ""); form.setValue("companyID", companyID || ""); form.setValue("personID", personID || "");
    }, [buildID, userTypeID, partID, companyID, personID, form]);

    const mutation = useAddLivingSpaceMutation();
    function onSubmit(values: FormValues) { mutation.mutate({ data: values }) }
    console.dir({ input: form.getValues() })

    const [isUserTypeEnabled, setIsUserTypeEnabled] = useState(false);
    const [isPartsEnabled, setIsPartsEnabled] = useState(false);
    const [isHandleCompanyAndPersonEnable, setIsHandleCompanyAndPersonEnable] = useState(false);

    const tabsClassName = "border border-gray-300 rounded-sm h-10"
    const deleteAllSelections = () => {
        setBuildID(null); setUserTypeID(null); setPartID(null); setCompanyID(null); setPersonID(null); setIsUserTypeEnabled(false); setIsPartsEnabled(false); setIsHandleCompanyAndPersonEnable(false)
    }

    return <>
        <div className="flex flex-col m-7">
            <Tabs defaultValue="builds" className="w-full">
                <Button className="my-3 h-10" variant="outline" size="sm" onClick={() => { router.push("/living-spaces") }}>
                    <IconArrowLeftToArc /><span className="hidden lg:inline">Back to Living Space List</span>
                </Button>
                <TabsList className="grid w-full grid-flow-col auto-cols-fr gap-1.5">
                    <TabsTrigger className={tabsClassName} value="builds">Builds</TabsTrigger>
                    {isUserTypeEnabled && <TabsTrigger className={tabsClassName} value="usertype">User Type</TabsTrigger>}
                    {isPartsEnabled && <TabsTrigger className={tabsClassName} value="parts">Parts</TabsTrigger>}
                    {isHandleCompanyAndPersonEnable && <TabsTrigger className={tabsClassName} value="company">Company</TabsTrigger>}
                    {isHandleCompanyAndPersonEnable && <TabsTrigger className={tabsClassName} value="person">Person</TabsTrigger>}
                </TabsList>
                <div className="mt-6">
                    <TabsContent value="builds">
                        <PageLivingSpaceBuildsTableSection buildID={buildID} setBuildID={setBuildID} setIsUserTypeEnabled={setIsUserTypeEnabled} />
                    </TabsContent>
                    {isUserTypeEnabled && <TabsContent value="usertype">
                        <PageLivingSpaceUserTypesTableSection userTypeID={userTypeID} setUserTypeID={setUserTypeID} setIsPartsEnabled={setIsPartsEnabled} setIsHandleCompanyAndPersonEnable={setIsHandleCompanyAndPersonEnable} setIsProperty={setIsProperty} />
                    </TabsContent>}
                    {isPartsEnabled && buildID && <TabsContent value="parts">
                        <PageLivingSpacePartsTableSection buildId={buildID} partID={partID} setPartID={setPartID} setIsPartsEnabled={setIsPartsEnabled} />
                    </TabsContent>}
                    {isHandleCompanyAndPersonEnable && <TabsContent value="company"><PageLivingSpaceCompanyTableSection companyID={companyID} setCompanyID={setCompanyID} /></TabsContent>}
                    {isHandleCompanyAndPersonEnable && <TabsContent value="person"><PageLivingSpacePersonTableSection personID={personID} setPersonID={setPersonID} /></TabsContent>}
                </div>
            </Tabs>
        </div>
        <div><FormAddNewLivingSpace form={form} onSubmit={onSubmit} deleteAllSelections={deleteAllSelections} /></div>
    </>
}

export { PageLivingSpaceAdd };