"use client"

import { useRef } from "react"
import { useFrame } from "@react-three/fiber"
import type { Group } from "three"

function Bee({ position, speed = 1 }: { position: [number, number, number]; speed?: number }) {
  const beeRef = useRef<Group>(null)

  useFrame((state) => {
    if (beeRef.current) {
      const time = state.clock.elapsedTime * speed
      beeRef.current.position.x = position[0] + Math.sin(time) * 2
      beeRef.current.position.y = position[1] + Math.cos(time * 0.7) * 1
      beeRef.current.position.z = position[2] + Math.sin(time * 0.5) * 1.5
      beeRef.current.rotation.y = Math.sin(time) * 0.3
    }
  })

  return (
    <group ref={beeRef}>
      {/* Bee body */}
      <mesh position={[0, 0, 0]}>
        <sphereGeometry args={[0.3, 16, 12]} />
        <meshStandardMaterial color="#FFD700" />
        <mesh scale={[1.5, 1, 1]} position={[0, 0, 0]}>
          <sphereGeometry args={[0.25, 12, 8]} />
          <meshStandardMaterial color="#FFA500" />
        </mesh>
      </mesh>

      {/* Bee stripes */}
      <mesh position={[0, 0.1, 0]} scale={[1.6, 0.8, 1.1]}>
        <sphereGeometry args={[0.28, 16, 8]} />
        <meshStandardMaterial color="#000000" />
      </mesh>
      <mesh position={[0, -0.1, 0]} scale={[1.6, 0.8, 1.1]}>
        <sphereGeometry args={[0.28, 16, 8]} />
        <meshStandardMaterial color="#000000" />
      </mesh>

      {/* Wings */}
      <mesh position={[0.08, 0.02, 0]} rotation={[0, 0, Math.PI / 4]}>
        <planeGeometry args={[0.15, 0.08]} />
        <meshStandardMaterial color="#E5E7EB" transparent opacity={0.7} />
      </mesh>
      <mesh position={[-0.08, 0.02, 0]} rotation={[0, 0, -Math.PI / 4]}>
        <planeGeometry args={[0.15, 0.08]} />
        <meshStandardMaterial color="#E5E7EB" transparent opacity={0.7} />
      </mesh>
    </group>
  )
}

export function FlyingBees() {
  return (
    <>
      <Bee position={[-3, 2, 1]} speed={0.8} />
      <Bee position={[4, -1, 2]} speed={1.2} />
      <Bee position={[-2, -2, -1]} speed={0.9} />
      <Bee position={[1, 3, -2]} speed={1.1} />
    </>
  )
}
