"""장르별 음악 파라미터 랜덤 풀 — 음악적으로 어울리는 결과 유도."""
import random

POOLS = {
    "lo-fi": {
        "moods": ["chill", "relaxing", "dreamy", "melancholic", "mellow", "nostalgic", "peaceful"],
        "instruments_pool": ["piano", "synth", "drums", "vinyl", "rhodes", "soft bass", "ambient pads"],
        "instruments_count": (3, 4),
        "bpm": (70, 90),
        "keys": ["C", "D", "F", "G", "A"],
        "scales": ["minor", "major"],
        "prompt_modifiers": ["cozy bedroom vibes", "rainy night", "late night study", "cafe ambience"],
    },
    "phonk": {
        "moods": ["dark", "aggressive", "moody", "intense", "hypnotic"],
        "instruments_pool": ["808 bass", "hi-hat", "synth lead", "vocal chops", "bass drops", "trap drums"],
        "instruments_count": (3, 4),
        "bpm": (130, 160),
        "keys": ["C", "D", "F", "G"],
        "scales": ["minor"],
        "prompt_modifiers": ["drift atmosphere", "dark neon", "midnight drive"],
    },
    "ambient": {
        "moods": ["peaceful", "meditative", "ethereal", "spacious", "dreamy"],
        "instruments_pool": ["pad synths", "atmospheric guitar", "soft strings", "field recordings", "drone bass"],
        "instruments_count": (2, 3),
        "bpm": (50, 75),
        "keys": ["C", "D", "E", "G", "A"],
        "scales": ["major", "minor"],
        "prompt_modifiers": ["misty mountain morning", "deep space", "still water", "forest dawn"],
    },
    "pop": {
        "moods": ["uplifting", "happy", "energetic", "romantic", "catchy"],
        "instruments_pool": ["acoustic guitar", "piano", "drums", "bass", "synth", "vocals harmonies"],
        "instruments_count": (3, 5),
        "bpm": (95, 130),
        "keys": ["C", "D", "E", "F", "G", "A"],
        "scales": ["major"],
        "prompt_modifiers": ["radio-ready", "summer vibe", "feel-good"],
    },
    "synthwave": {
        "moods": ["retro", "nostalgic", "futuristic", "dreamy", "moody"],
        "instruments_pool": ["synth lead", "synth bass", "drum machine", "arp synth", "pad synth", "vocoder"],
        "instruments_count": (3, 4),
        "bpm": (90, 120),
        "keys": ["A", "D", "F", "G"],
        "scales": ["minor", "major"],
        "prompt_modifiers": ["80s neon city night", "retro arcade glow", "VHS aesthetic", "cyberpunk skyline"],
    },
    "chillhop": {
        "moods": ["chill", "groovy", "warm", "nostalgic", "head-nodding"],
        "instruments_pool": ["jazz piano", "bass guitar", "drum kit", "saxophone", "vinyl crackle", "rhodes", "muted trumpet"],
        "instruments_count": (3, 5),
        "bpm": (75, 95),
        "keys": ["C", "D", "F", "G", "A"],
        "scales": ["major", "minor"],
        "prompt_modifiers": ["jazz bar lounge", "chill summer afternoon", "vintage warm tape"],
    },
    "jazz": {
        "moods": ["smooth", "elegant", "moody", "warm", "sophisticated"],
        "instruments_pool": ["piano", "double bass", "jazz drums", "saxophone", "trumpet", "jazz guitar"],
        "instruments_count": (3, 5),
        "bpm": (75, 130),
        "keys": ["C", "D", "F", "G", "A"],
        "scales": ["major", "minor"],
        "prompt_modifiers": ["smoky jazz club", "fireplace evening", "elegant lounge", "rainy speakeasy"],
    },
    "hip-hop": {
        "moods": ["confident", "groovy", "head-nodding", "dark", "energetic"],
        "instruments_pool": ["808 bass", "trap drums", "synth lead", "vocal samples", "piano chords", "vinyl scratch", "boom bap drums"],
        "instruments_count": (3, 4),
        "bpm": (85, 100),
        "keys": ["C", "D", "F", "G"],
        "scales": ["minor"],
        "prompt_modifiers": ["urban night", "boom bap classic", "street vibe", "underground"],
    },
    "electronic": {
        "moods": ["energetic", "uplifting", "hypnotic", "futuristic", "driving"],
        "instruments_pool": ["synth lead", "synth bass", "drum machine", "fx pad", "arp", "kick", "snare claps"],
        "instruments_count": (3, 5),
        "bpm": (110, 140),
        "keys": ["A", "C", "D", "F", "G"],
        "scales": ["minor", "major"],
        "prompt_modifiers": ["club energy", "festival vibe", "neon dance floor"],
    },
    "classical": {
        "moods": ["serene", "elegant", "melancholic", "majestic", "tender"],
        "instruments_pool": ["piano", "strings", "cello", "violin", "harp", "flute", "oboe"],
        "instruments_count": (1, 3),
        "bpm": (60, 100),
        "keys": ["C", "D", "E", "F", "G", "A"],
        "scales": ["major", "minor"],
        "prompt_modifiers": ["orchestra hall", "candlelight evening", "morning piano study", "stately concert"],
    },
    "funk": {
        "moods": ["groovy", "funky", "energetic", "uplifting", "playful"],
        "instruments_pool": ["bass guitar", "wah guitar", "horn section", "drums", "clavinet", "rhodes"],
        "instruments_count": (3, 5),
        "bpm": (95, 120),
        "keys": ["C", "D", "E", "F", "G"],
        "scales": ["major", "minor"],
        "prompt_modifiers": ["70s groove", "disco funk", "soul party"],
    },
    "default": {
        "moods": ["chill", "relaxing", "uplifting", "mellow"],
        "instruments_pool": ["piano", "synth", "drums", "guitar", "bass", "strings"],
        "instruments_count": (3, 4),
        "bpm": (80, 110),
        "keys": ["C", "D", "F", "G", "A"],
        "scales": ["minor", "major"],
        "prompt_modifiers": [""],
    },
}


def list_genres() -> list[str]:
    """프론트에 노출할 장르 목록 — POOLS의 키 (default 제외)."""
    return [g for g in POOLS.keys() if g != "default"]


def randomize(genre: str, rng=None) -> dict:
    """장르 → 랜덤 음악 파라미터 1세트.

    반환: {moods, instruments, bpm, key, scale, prompt_modifier}
    """
    rng = rng or random.Random()
    pool = POOLS.get(genre.lower(), POOLS["default"])
    n_instr = rng.randint(*pool["instruments_count"])
    instruments = rng.sample(pool["instruments_pool"], min(n_instr, len(pool["instruments_pool"])))
    return {
        "moods": [rng.choice(pool["moods"])],
        "instruments": instruments,
        "bpm": rng.randint(*pool["bpm"]),
        "key": rng.choice(pool["keys"]),
        "scale": rng.choice(pool["scales"]),
        "prompt_modifier": rng.choice(pool["prompt_modifiers"]),
    }
