#!/usr/bin/env python3
"""Companion CLI to set ambience for the TUI.

Usage:
    python3 tools/ambience.py <name>
    python3 tools/ambience.py silence
"""

import sys
from pathlib import Path

SESSION = Path(__file__).resolve().parent.parent / 'session'
AMBIENCE_PATH = SESSION / 'ambience.md'


def main():
    if len(sys.argv) < 2:
        names = _available()
        print(f"Usage: python3 tools/ambience.py <name>")
        print(f"Available: {', '.join(names)}")
        sys.exit(1)

    name = sys.argv[1].lower()
    AMBIENCE_PATH.write_text(name + '\n')
    print(f"♫ ambience set to: {name}")


def _available():
    path = SESSION / 'ambience_options.md'
    if not path.exists():
        return ['silence']
    names = ['silence']
    for line in path.read_text().splitlines():
        s = line.strip()
        if s.startswith('|') and s.endswith('|'):
            parts = [p.strip() for p in s.split('|')]
            parts = [p for p in parts if p]
            if parts and parts[0].lower() not in ('ambience', '---'):
                names.append(parts[0])
    return names


if __name__ == '__main__':
    main()
