let directionalLight;
let ambientLight;

function initSun(scene) {
    // Lighting
    ambientLight = new THREE.AmbientLight(0x90bbbd, 0.9);
    scene.add(ambientLight);

    directionalLight = new THREE.DirectionalLight(0xffffff);
    directionalLight.position.set(20, 30, 10);
    directionalLight.castShadow = true;
    directionalLight.shadow.mapSize.width = 4096;
    directionalLight.shadow.mapSize.height = 4096;
    directionalLight.shadow.camera.near = 25;
    directionalLight.shadow.camera.far = 250;
    directionalLight.shadow.bias = -0.005
    directionalLight.shadow.camera.left = -100;
    directionalLight.shadow.camera.right = 100;
    directionalLight.shadow.camera.top = 100;
    directionalLight.shadow.camera.bottom = -100;
    scene.add(directionalLight);
}

function sunPosition(){
        // --- Use SunCalc to get sun position ---
    // Set your latitude and longitude
    const latitude = 41.17873;
    const longitude = -8.60835; // Example: adjust as needed

    // Calculate the current date and time based on your simulation hour
    const now = new Date();
    now.setHours(Math.floor(currentHour));
    now.setMinutes(Math.floor((currentHour % 1) * 60));
    now.setSeconds(0);

    // Get sun position from SunCalc
    const sunPos = SunCalc.getPosition(now, latitude, longitude);

    // Convert to your scene's coordinates
    const sunRadius = 100;
    const azimuth = sunPos.azimuth; // Convert from south-based to x/z-plane (east = 0)
    const elevation = sunPos.altitude;
    console.log(azimuth, elevation)
    const sunX = sunRadius * Math.cos(elevation) * Math.cos(azimuth);
    const sunY = sunRadius * Math.sin(elevation);
    const sunZ = sunRadius * Math.cos(elevation) * Math.sin(azimuth);

    directionalLight.position.set(sunX, sunY, sunZ);
    directionalLight.target.position.set(0, 0, 0);
    directionalLight.target.updateMatrixWorld();
}

function updateSun(scene) {
    sunPosition();
    // --- Dynamic Sky Color ---
    const dayColor = new THREE.Color(0xa9d3fd);   // Light blue
    const nightColor = new THREE.Color(0x0a0a23); // Dark blue/black

    const sunriseSunColor = new THREE.Color(0xa3d1ff);      // Bright white/yellow
    const daySunColor = new THREE.Color(0xffffa9);      // Bright white/yellow
    const sunsetSunColor = new THREE.Color(0xffcc25);   // Warm orange
    const nightSunColor = new THREE.Color(0x222244);    // Dim blue

    const dayAmbientColor = new THREE.Color(0xa9d3fd); // Light blue
    const nightAmbientColor = new THREE.Color(0x222244); // Dim blue

    let sunColor;
    if (currentHour < 5 || currentHour >= 21){
        sunColor = nightSunColor;
        ambientLight.color.copy(nightAmbientColor);
        directionalLight.castShadow = false;
        directionalLight.intensity = 0
        scene.background = nightColor;
    } else if (5 < currentHour && currentHour <= 8) {
        // Night to sunrise
        const t = (currentHour - 5) / 3;
        sunColor = nightSunColor.clone().lerp(sunriseSunColor, t);
        ambientLight.color.copy(nightAmbientColor.clone().lerp(dayAmbientColor, t));
        directionalLight.castShadow = true;
        directionalLight.intensity = t *0.5;
        scene.background = nightColor.clone().lerp(dayColor, t);
    } else if (currentHour < 11) {
        // Sunrise to day
        const t = (currentHour - 8) / 3;
        sunColor = sunriseSunColor.clone().lerp(daySunColor, t);
        directionalLight.castShadow = true;
        scene.background = dayColor;
    } else if (currentHour < 16) {
        // Day
        sunColor = daySunColor;
        scene.background = dayColor;
    } else if (currentHour < 20) {
        // Day to sunset
        const t = (currentHour - 16) / 2;
        directionalLight.intensity = (1-t) * 0.5
        sunColor = daySunColor.clone().lerp(sunsetSunColor, t);
        scene.background = dayColor;
    } else if (currentHour < 21) {
        // Sunset to night
        const t = (currentHour - 20);
        sunColor = sunsetSunColor.clone().lerp(nightSunColor, t);
        ambientLight.color.copy(dayAmbientColor.clone().lerp(nightAmbientColor, t));
        scene.background = dayColor.clone().lerp(nightColor, t);
    }
    directionalLight.color.copy(sunColor);
}