"""sfx.py — Sound effects manager for Powernet."""
from __future__ import annotations
import pygame
from pathlib import Path

_SFX_DIR = Path(__file__).parent.parent / "assets" / "sounds"

class SFXManager:
    def __init__(self) -> None:
        self._sounds: dict[str, pygame.mixer.Sound] = {}
        self._volume: float = 0.7
        self._enabled: bool = True

    def load(self, name: str, filename: str) -> None:
        path = _SFX_DIR / filename
        if path.exists():
            try:
                self._sounds[name] = pygame.mixer.Sound(str(path))
                self._sounds[name].set_volume(self._volume)
            except Exception:
                pass

    def play(self, name: str) -> None:
        if self._enabled and name in self._sounds:
            self._sounds[name].play()

    def set_volume(self, vol: float) -> None:
        self._volume = max(0.0, min(1.0, vol))
        for s in self._sounds.values():
            s.set_volume(self._volume)

    def set_enabled(self, enabled: bool) -> None:
        self._enabled = enabled

sfx_manager = SFXManager()

def init_sfx() -> None:
    """Load all game sound effects. Call after pygame.mixer is ready."""
    sfx_manager.load("place",    "place.wav")       # building placed
    sfx_manager.load("wire",     "wire.wav")         # wire connected
    sfx_manager.load("demolish", "demolish.wav")     # building demolished
    sfx_manager.load("levelup",  "levelup.wav")      # level up
    sfx_manager.load("error",    "error.wav")        # can't afford / error
    sfx_manager.load("click",    "click.wav")        # UI click
