import { useMemo } from 'react';
import { Environment, ContactShadows } from '@react-three/drei';
import { CameraControls } from '@react-three/drei';
import type { Floor3DData, Room3D } from '../../lib/layoutApi';
import { RoomObject } from './RoomObject';
import { Beacon } from './Beacon';
import { PlantPosition, VisMode } from './types';
import { CameraPresets, CameraPreset } from './CameraPresets';

// Convert pixel coordinates to world units
const SCALE = 0.1;

interface FacilitySceneProps {
    data: Floor3DData;
    visMode: VisMode;
    cameraPreset: CameraPreset;
    focusTarget?: { x: number; z: number } | null;
    onPlantClick: (plant: PlantPosition) => void;
    onControlsReady?: (controls: CameraControls) => void;
    highlightedTags?: string[];
    dimMode?: boolean;
    beaconPosition?: [number, number, number] | null;
    timelineDate?: Date;
}

export function FacilityScene({
    data,
    visMode,
    cameraPreset,
    focusTarget,
    onPlantClick,
    onControlsReady,
    highlightedTags = [],
    dimMode = false,
    beaconPosition = null,
    timelineDate,
}: FacilitySceneProps) {
    // Use floor dimensions for centering - must match the calculation in Facility3DViewerPage
    const floorCenterX = (data.floor.width * SCALE) / 2;
    const floorCenterZ = (data.floor.height * SCALE) / 2;

    // Calculate floor bounds for camera
    const floorWidth = data.floor.width * SCALE;
    const floorHeight = data.floor.height * SCALE;

    return (
        <>
            <Environment preset="city" background={false} />
            <ambientLight intensity={0.6} />
            <directionalLight
                position={[-15, 60, -15]}
                intensity={1.2}
                castShadow
                shadow-mapSize={[2048, 2048]}
            />

            {/* Offset to center the content - uses floor dimensions to match page calculations */}
            <group position={[-floorCenterX, 0, -floorCenterZ]}>
                {data.rooms.map((room: Room3D) => (
                    <RoomObject
                        key={room.id}
                        room={room}
                        visMode={visMode}
                        onPlantClick={onPlantClick}
                        highlightedTags={highlightedTags}
                        dimMode={dimMode}
                        hierarchy={{
                            facility: data.floor.property,
                            building: data.floor.building,
                            floor: data.floor.name,
                        }}
                        timelineDate={timelineDate}
                    />
                ))}
            </group>

            {/* Beacon rendered OUTSIDE offset group - coords already include centering */}
            {beaconPosition && <Beacon position={beaconPosition} />}

            <ContactShadows
                position={[0, -0.02, 0]}
                opacity={0.35}
                scale={floorWidth * 1.5}
                blur={2}
                far={12}
                resolution={512}
                color="#000"
            />

            {/* Preset-based camera system */}
            <CameraPresets
                preset={cameraPreset}
                floorWidth={floorWidth}
                floorHeight={floorHeight}
                focusTarget={focusTarget}
                onReady={onControlsReady}
            />
        </>
    );
}
