// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
        e.preventDefault();

        document.querySelector(this.getAttribute('href')).scrollIntoView({
            behavior: 'smooth'
        });
    });
});

// 3D Logo
function init3DLogo() {
    const canvas = document.getElementById('3d-logo-canvas');
    if (canvas && typeof THREE !== 'undefined') {
        let scene, camera, renderer, plane;

        function init() {
            // Scene
            scene = new THREE.Scene();

            // Camera
            camera = new THREE.PerspectiveCamera(75, canvas.clientWidth / canvas.clientHeight, 0.1, 1000);
            camera.position.z = 5;

            // Renderer
            renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, canvas: canvas });
            renderer.setSize(canvas.clientWidth, canvas.clientHeight);

            // Texture
            const textureLoader = new THREE.TextureLoader();
            textureLoader.load('assets/pasted-20251022-215019-f7191fbf.png', 
                function(texture) {
                    // Plane
                    const geometry = new THREE.PlaneGeometry(5, 1.5);
                    const material = new THREE.MeshStandardMaterial({ map: texture, transparent: true, roughness: 0.4, metalness: 0.2 });
                    plane = new THREE.Mesh(geometry, material);
                    scene.add(plane);
                },
                undefined, // onProgress callback not needed
                function(err) {
                    console.error('An error happened while loading the texture.', err);
                }
            );

            // Lights
            const ambientLight = new THREE.AmbientLight(0xffffff, 0.7);
            scene.add(ambientLight);

            const pointLight = new THREE.PointLight(0xDB7093, 1.2); // Purple-red light
            pointLight.position.set(0, 0, 5);
            scene.add(pointLight);

            // Animation
            animate();
        }

        let mouseX = 0, mouseY = 0;
        let targetX = 0, targetY = 0;
        const windowHalfX = window.innerWidth / 2;
        const windowHalfY = window.innerHeight / 2;

        function onDocumentMouseMove(event) {
            mouseX = (event.clientX - windowHalfX) / 2;
            mouseY = (event.clientY - windowHalfY) / 2;
        }
        document.addEventListener('mousemove', onDocumentMouseMove);


        function animate() {
            requestAnimationFrame(animate);
            
            targetX = mouseX * .001;
            targetY = mouseY * .001;

            if (plane) {
                plane.rotation.y += .05 * (targetX - plane.rotation.y);
                plane.rotation.x += .05 * (targetY - plane.rotation.x);
            }

            renderer.render(scene, camera);
        }

        function onWindowResize() {
            if (canvas.clientWidth > 0 && canvas.clientHeight > 0) {
                camera.aspect = canvas.clientWidth / canvas.clientHeight;
                camera.updateProjectionMatrix();
                renderer.setSize(canvas.clientWidth, canvas.clientHeight);
            }
        }

        window.addEventListener('resize', onWindowResize, false);

        init();
    }
}

window.addEventListener('load', init3DLogo);
