import * as THREE from 'three';
import { state, initState } from '../state.js';
import { EffectsManager } from '../effects/EffectsManager.js';
import { createSceneObjects } from '../scene/root.js';
import { animate, onWindowResize } from './animate.js';
import { initPostprocessing } from './postprocessing.js';

// --- Initialization ---
export function init() {
    initState();

    // 1. Scene Setup (Dark, Ambient)
    state.scene = new THREE.Scene();
    state.scene.background = new THREE.Color(0x000000); 

    // 2. Camera Setup
    const FOV = 95;
    state.camera = new THREE.PerspectiveCamera(FOV, window.innerWidth / window.innerHeight, 0.1, 1000);
    state.camera.position.set(0, 1.5, 4);
    
    // 3. Renderer Setup
    state.renderer = new THREE.WebGLRenderer({ antialias: true });
    state.renderer.setSize(window.innerWidth, window.innerHeight);
    state.renderer.setPixelRatio(window.devicePixelRatio);
    // Enable shadows on the renderer
    state.renderer.shadowMap.enabled = true;
    state.renderer.shadowMap.type = THREE.PCFSoftShadowMap; // Softer shadows
    
    state.container.appendChild(state.renderer.domElement);

    // 5. Build the entire scene with TV and surrounding objects
    createSceneObjects();

    // 6. Initialize all visual effects via the manager
    state.effectsManager = new EffectsManager(state.scene);
    
    // 9. Event Listeners
    window.addEventListener('resize', onWindowResize, false);

    initPostprocessing();

    // Start the animation loop
    animate();
}
