'use client';
import { useState, useEffect, useRef } from "react";
import { Card, CardContent } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { XCircle } from "lucide-react";
import PageLivingSpaceBuildsTableSection from "./builds/page";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import PageLivingSpaceUserTypesTableSection from "./userType/page";


const PageLivingSpace = () => {

    const [collectionToken, setCollectionToken] = useState<string | null>(null);
    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 [isUserTypeEnabled, setIsUserTypeEnabled] = useState(false);
    const [isPartsEnabled, setIsPartsEnabled] = useState(false);
    const [isCompanyEnabled, setIsCompanyEnabled] = useState(false);
    const [isPersonEnabled, setIsPersonEnabled] = useState(false);


    const tabsClassName = "border border-gray-300 rounded-sm h-10"
    const deleteAllSelections = () => {
        setBuildID(null); setCollectionToken(null); setUserTypeID(null); setPartID(null); setCompanyID(null); setPersonID(null);
        setIsUserTypeEnabled(false); setIsPartsEnabled(false); setIsCompanyEnabled(false); setIsPersonEnabled(false);
    }
    return <>
        <Card className="mb-6">
            <CardContent className="pt-6 flex justify-between items-start">
                <div className="grid grid-cols-6 gap-x-12 gap-y-2">
                    <div className="flex gap-2">
                        <span className="font-medium text-gray-500">Build ID:</span>
                        <span>{buildID || '-'}</span>
                    </div>
                    <div className="flex gap-2">
                        <span className="font-medium text-gray-500">Collection Token:</span>
                        <span>{collectionToken || '-'}</span>
                    </div>
                    <div className="flex gap-2">
                        <span className="font-medium text-gray-500">User Type ID:</span>
                        <span>{userTypeID || '-'}</span>
                    </div>
                    <div className="flex gap-2">
                        <span className="font-medium text-gray-500">Part ID:</span>
                        <span>{partID || '-'}</span>
                    </div>
                    <div className="flex gap-2">
                        <span className="font-medium text-gray-500">Company ID:</span>
                        <span>{companyID || '-'}</span>
                    </div>
                    <div className="flex gap-2">
                        <span className="font-medium text-gray-500">Person ID:</span>
                        <span>{personID || '-'}</span>
                    </div>
                </div>
                <Button variant="outline" className="ml-4 text-destructive hover:text-destructive" onClick={deleteAllSelections}>
                    <XCircle className="h-4 w-4 mr-2" />Clear All
                </Button>
            </CardContent>
        </Card>
        <div className="flex flex-col m-7">
            <Tabs defaultValue="builds" className="w-full">
                <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>}
                    {isCompanyEnabled && <TabsTrigger className={tabsClassName} value="company">Company</TabsTrigger>}
                    {isPersonEnabled && <TabsTrigger className={tabsClassName} value="person">Person</TabsTrigger>}
                </TabsList>
                <div className="mt-6">
                    <TabsContent value="builds">
                        <PageLivingSpaceBuildsTableSection buildID={buildID} setBuildID={setBuildID} collectionToken={collectionToken} setCollectionToken={setCollectionToken} setIsUserTypeEnabled={setIsUserTypeEnabled} />
                    </TabsContent>
                    {isUserTypeEnabled && <TabsContent value="usertype">
                        <PageLivingSpaceUserTypesTableSection userTypeID={userTypeID} setUserTypeID={setUserTypeID} setIsPartsEnabled={setIsPartsEnabled} setIsCompanyEnabled={setIsCompanyEnabled} setIsPersonEnabled={setIsPersonEnabled} />
                    </TabsContent>}
                    {isPartsEnabled && <TabsContent value="parts">{/* Add Parts section component here */}</TabsContent>}
                    {isCompanyEnabled && <TabsContent value="company">{/* Add Company section component here */}</TabsContent>}
                    {isPersonEnabled && <TabsContent value="person"> {/* Add Person section component here */}</TabsContent>}
                </div>
            </Tabs>
        </div>
    </>
}

export { PageLivingSpace };