// app/projects/design/page.tsx

'use client';

import { Center, Environment, Loader, OrbitControls, useGLTF } from '@react-three/drei';
import { Canvas, useThree } from '@react-three/fiber';
import { Suspense, useEffect, useState, useTransition } from 'react';
import { TbCube3dSphere } from 'react-icons/tb';
import * as THREE from 'three';
import Image from 'next/image';

const models = [
    { name: 'Wallet', src: 'Wallet' },
    { name: 'Car', src: 'Car' },
    { name: 'Magnet holder', src: 'Magnet holder' },
    { name: 'PC', src: 'PC' },
];

function Model({ url, onReady }: { url: string; onReady: () => void }) {
    const { scene } = useGLTF(url);

    useEffect(() => {
        if (scene) {
            const timer = setTimeout(onReady, 100);
            return () => clearTimeout(timer);
        }
    }, [scene, onReady]);

    return <primitive object={scene} />;
}

function DynamicCamera({ radius }: { radius: number }) {
    const { camera } = useThree();
    useEffect(() => {
        camera.position.set(0, radius * 0.5, radius * 2.5);
        camera.lookAt(0, 0, 0);
        camera.updateProjectionMatrix();
    }, [radius, camera]);
    return null;
}

export default function DesignPage() {
    const [activeModel, setActiveModel] = useState(models[0].src);
    const [modelRadius, setModelRadius] = useState(1);
    const [isPending, startTransition] = useTransition();
    const [isSceneReady, setIsSceneReady] = useState(false);
    const [isCentering, setIsCentering] = useState(true);

    const handleModelChange = (newModel: string) => {
        if (newModel === activeModel) return;

        setIsCentering(true);
        setIsSceneReady(false);

        startTransition(() => {
            setActiveModel(newModel);
        });
    };

    const showOverlay = isPending || !isSceneReady;

    return (
        <main className="flex flex-col flex-1 relative bg-slate-50 dark:bg-[#0d1117] overflow-hidden transition-colors duration-300">
            {/* Background Glow */}
            <div className="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-200 h-200 bg-[#84c0a0]/10 blur-[150px] rounded-full pointer-events-none z-0"></div>
            <div className="h-25 shrink-0"></div>

            {/* Changed from flex-col to lg:flex-row for side-by-side layout on desktop */}
            <div className="relative z-10 w-full max-w-450 mx-auto px-4 lg:px-12 pb-20 flex flex-col lg:flex-row gap-10 lg:gap-16 items-center lg:items-start pt-10">

                {/* 1. Title Section (Now on the left and sticky!) */}
                <div className="w-full lg:w-1/3 flex flex-col justify-center items-center lg:items-start text-center lg:text-left pt-5 lg:sticky lg:top-40">
                    <TbCube3dSphere className="text-slate-900 dark:text-white text-[80px] drop-shadow-md dark:drop-shadow-[0_0_15px_rgba(255,255,255,0.2)] transition-colors" />
                    <h2 className="text-slate-900 dark:text-white text-[32px] md:text-[45px] uppercase mt-4 font-bold tracking-wider transition-colors leading-tight">CAD Library</h2>
                    <p className="text-slate-500 dark:text-gray-400 mt-4 text-base md:text-lg leading-relaxed transition-colors">
                        Explore my 3D modeling and design work. Interact with the models using your mouse or touch screen.
                    </p>
                </div>

                {/* 2. Viewer Section (Now taking up the right side) */}
                <section className="w-full lg:w-2/3 flex justify-center">
                    <div className="w-full relative bg-white/80 dark:bg-white/5 backdrop-blur-xl rounded-4xl border border-slate-200 dark:border-white/10 shadow-xl dark:shadow-[0_8px_32px_rgba(0,0,0,0.5)] p-4 md:p-8 flex flex-col transition-colors duration-300">
                        <div className="w-full h-[50vh] md:h-[60vh] rounded-[20px] overflow-hidden relative bg-slate-200 dark:bg-linear-to-br dark:from-[#1a1a1a] dark:to-[#0f0f0f] shadow-inner border border-slate-300 dark:border-white/5 cursor-grab active:cursor-grabbing transition-colors duration-300">

                            {showOverlay && (
                                <div className="absolute inset-0 z-20 flex flex-col items-center justify-center bg-slate-100/80 dark:bg-[#0d1117]/80 backdrop-blur-md transition-all duration-500">
                                    <div className="w-12 h-12 border-4 border-[#84c0a0]/20 border-t-[#84c0a0] rounded-full animate-spin mb-4"></div>
                                    <p className="text-[#84c0a0] font-mono text-xs uppercase tracking-[0.2em] font-bold">
                                        {isPending ? "Fetching Data..." : "Compiling Materials..."}
                                    </p>
                                </div>
                            )}

                            <Canvas shadows={{ type: THREE.PCFShadowMap }} dpr={[1, 2]} camera={{ near: 0.001, far: 1000, fov: 50 }}>
                                <ambientLight intensity={0.5} />
                                <directionalLight position={[10, 10, 10]} intensity={1} castShadow />
                                <Environment preset="city" />
                                <DynamicCamera radius={modelRadius} />

                                <Suspense fallback={null}>
                                    <Center
                                        key={activeModel}
                                        onCentered={({ boundingSphere }) => {
                                            setModelRadius(boundingSphere.radius);
                                            requestAnimationFrame(() => setIsCentering(false));
                                        }}
                                    >
                                        <group visible={!isCentering && isSceneReady}>
                                            <Model
                                                url={`/models/${activeModel}.glb`}
                                                onReady={() => setIsSceneReady(true)}
                                            />
                                        </group>
                                    </Center>
                                </Suspense>

                                <OrbitControls
                                    makeDefault
                                    autoRotate
                                    autoRotateSpeed={0.5}
                                    enablePan={true}
                                    enableZoom={true}
                                    minDistance={modelRadius * 0.5}
                                    maxDistance={modelRadius * 6}
                                />
                            </Canvas>

                            <Loader
                                containerStyles={{ background: 'transparent' }}
                                innerStyles={{ display: 'none' }}
                            />

                            <a href={`/models/${activeModel}.usdz`} rel="ar" className="absolute top-4 right-4 bg-white/80 dark:bg-black/50 text-slate-900 dark:text-white backdrop-blur-md text-sm px-4 py-2 rounded-full border border-slate-300 dark:border-white/10 hover:bg-slate-100 dark:hover:bg-white/20 transition-colors md:hidden z-10 flex items-center gap-2">
                                <Image
                                    src={`/models/${activeModel}.png`}
                                    alt="AR Preview"
                                    width={20}
                                    height={20}
                                    className="w-5 h-5 rounded-full object-cover"
                                />
                                View in AR
                            </a>
                        </div>

                        <div className="w-full mt-8 flex justify-center">
                            <div className="flex flex-wrap justify-center gap-4 md:gap-8 w-full px-2">
                                {models.map((model) => (
                                    <button
                                        key={model.name}
                                        className={`shrink-0 w-20 h-20 md:w-25 md:h-25 bg-contain bg-no-repeat bg-center rounded-2xl transition-all duration-300 focus:outline-none focus-visible:ring-2 focus-visible:ring-[#84c0a0] relative group ${activeModel === model.src
                                            ? 'bg-white dark:bg-[#2a2a2a] border-2 border-[#84c0a0] scale-110 shadow-[0_0_20px_rgba(132,192,160,0.3)]'
                                            : 'bg-slate-100 dark:bg-[#1a1a1a] border border-slate-200 dark:border-white/10 hover:bg-slate-200 dark:hover:bg-[#252525] hover:-translate-y-1'
                                            }`}
                                        style={{ backgroundImage: `url('/models/${model.src}.png')` }}
                                        onClick={() => handleModelChange(model.src)}
                                        onMouseEnter={() => useGLTF.preload(`/models/${model.src}.glb`)}
                                        aria-label={`View ${model.name} model`}
                                    >
                                        <span className="absolute -bottom-8 left-1/2 -translate-x-1/2 text-xs font-semibold text-white opacity-0 group-hover:opacity-100 transition-opacity whitespace-nowrap bg-slate-800/90 dark:bg-black/80 px-2 py-1 rounded">
                                            {model.name}
                                        </span>
                                    </button>
                                ))}
                            </div>
                        </div>
                    </div>
                </section>
            </div>
        </main>
    );
}