#!/usr/bin/env python3
"""Synthetic ElevenLabs command for offline tests."""

import json
import math
import os
import shutil
import struct
import sys
import time
import wave
from pathlib import Path


def main():
    args = sys.argv[1:]
    body_text = sys.stdin.read()
    log = os.environ.get("FAKE_ELEVENLABS_LOG")
    if log:
        with open(log, "a", encoding="utf-8") as stream:
            stream.write(json.dumps({"argv": args, "stdin": body_text}) + "\n")
    command = " ".join(args[:1] if args[:1] in (["models"], ["text-to-speech"]) else args[:2])
    missing = json.loads(os.environ.get("FAKE_ELEVENLABS_MISSING_PERMISSIONS", "{}"))
    if command in missing:
        print(f"HTTP 401: The API key you used is missing the permission {missing[command]}",
              file=sys.stderr)
        return 4
    if args[:2] == ["auth", "status"]:
        code = int(os.environ.get("FAKE_ELEVENLABS_AUTH_EXIT", "0"))
        if code == 0:
            print('{"authenticated": true}')
        return code
    if args[:2] == ["user", "get-subscription-info"]:
        tier = os.environ.get("FAKE_ELEVENLABS_TIER", "creator")
        if tier == "missing_permissions":
            print("HTTP 401 missing_permissions", file=sys.stderr)
            return 4
        print(json.dumps({"tier": tier,
                          "character_count": int(os.environ.get("FAKE_ELEVENLABS_COUNT", "18766")),
                          "character_limit": int(os.environ.get("FAKE_ELEVENLABS_LIMIT", "100000")),
                          "voice_limit": 10}))
        return 0
    if args[:1] == ["models"]:
        if os.environ.get("FAKE_ELEVENLABS_MODELS_FAIL"):
            return 5
        print(json.dumps([{"model_id": "eleven_multilingual_v2", "model_rates": {
            "character_cost_multiplier": float(os.environ.get("FAKE_ELEVENLABS_MULTIPLIER", "1"))}}]))
        return 0
    voice_id = os.environ.get("FAKE_ELEVENLABS_VOICE_ID", "fake-personal-voice-secret")
    voice = {"voice_id": voice_id, "name": os.environ.get("FAKE_ELEVENLABS_VOICE_NAME", "My voice"),
             "category": os.environ.get("FAKE_ELEVENLABS_VOICE_CATEGORY", "cloned"),
             "samples": json.loads(os.environ.get("FAKE_ELEVENLABS_VOICE_SAMPLES", "[]"))}
    if args[:2] == ["voices", "get-user-v2"]:
        if "--all" in args:
            print("[]")
            return 0
        count = int(os.environ.get("FAKE_ELEVENLABS_VOICE_DUPLICATES", "1"))
        if os.environ.get("FAKE_ELEVENLABS_VOICE_APPEARS_AFTER_ADD"):
            count = int(bool(log) and any(
                json.loads(line)["argv"][:2] == ["voices", "add"]
                for line in Path(log).read_text().splitlines()
            ))
        rows = [voice] * count
        pages = json.loads(os.environ.get("FAKE_ELEVENLABS_VOICE_PAGES", "[]"))
        page = {"voices": rows, "has_more": False, "next_page_token": None,
                "total_count": count}
        if pages:
            token = args[args.index("--next-page-token") + 1] if "--next-page-token" in args else None
            index = next((i + 1 for i, row in enumerate(pages[:-1])
                          if token is not None and row.get("next_page_token") == token), 0)
            page = pages[index]
        print(json.dumps(rows if os.environ.get("FAKE_ELEVENLABS_RAW") else
                         {"meta": {}, "results": page}))
        return 0
    if args[:2] == ["voices", "get-by-id"]:
        print(json.dumps(voice))
        return 0
    if args[:2] == ["voices", "delete"]:
        print('{}')
        return 0
    if args[:2] == ["voices", "add"]:
        if os.environ.get("FAKE_ELEVENLABS_ADD_FILES_ERROR"):
            print("HTTP 422 validation error: files must be uploaded", file=sys.stderr)
            return 2
        print(json.dumps(voice))
        return 0
    if args[:1] == ["text-to-speech"]:
        if "--dry-run" in args:
            print(json.dumps({"dry_run": True}))
            return 0
        code = int(os.environ.get("FAKE_ELEVENLABS_GENERATE_EXIT", "0"))
        if code:
            print(os.environ.get("FAKE_ELEVENLABS_GENERATE_STDERR", "synthetic failure") +
                  " " + voice_id, file=sys.stderr)
            return code
        options = dict(zip(args[2::2], args[3::2]))
        target = Path(options["--deliver"].removeprefix("file:"))
        rate = 44100
        count = round(float(os.environ.get("FAKE_ELEVENLABS_SPEECH_SECONDS", "1.4"))*rate)
        with wave.open(str(target), "wb") as output:
            output.setnchannels(1)
            output.setsampwidth(2)
            output.setframerate(rate)
            samples = bytearray()
            for index in range(count):
                envelope = min(1, index/(rate*.02), (count-index)/(rate*.02))
                samples.extend(struct.pack("<h", round(1800*envelope*math.sin(2*math.pi*260*index/rate))))
            output.writeframes(samples)
        with target.open("rb") as audio:
            shutil.copyfileobj(audio, sys.stdout.buffer)
        return 0
    if args[:2] == ["history", "get-speech"]:
        rows = []
        if log and Path(log).is_file() and not os.environ.get("FAKE_ELEVENLABS_HISTORY_EMPTY"):
            for line in Path(log).read_text().splitlines():
                call = json.loads(line)["argv"]
                if call[:1] == ["text-to-speech"]:
                    rows.append({"text": call[call.index("--text")+1], "date_unix": int(time.time()),
                                 "character_count_change_from": 0,
                                 "character_count_change_to": int(os.environ.get("FAKE_ELEVENLABS_CHARGED", "11"))})
        print(json.dumps({"history": rows}))
        return 0
    if args[:2] == ["music", "compose-plan"]:
        body = json.loads(body_text)
        if "--dry-run" in args:
            print("POST https://api.elevenlabs.io/v1/music/compose-plan", file=sys.stderr)
            print(f"Body: {json.dumps(body)}", file=sys.stderr)
            print("  xi-api-key: ****abcd", file=sys.stderr)
            print(json.dumps({"action": "post", "data": {"dry_run": True},
                              "dry_run": True, "success": True}))
        else:
            print(json.dumps({"sections": [{"duration_ms": body["music_length_ms"],
                                             "prompt": body["prompt"]}]}))
        return 0
    if args[:2] == ["music", "generate"]:
        body = json.loads(body_text)
        deliver = next(value.removeprefix("file:") for flag, value in zip(args, args[1:],
                                                                           strict=False)
                       if flag == "--deliver")
        target = Path(deliver)
        if "--dry-run" in args:
            print("POST https://api.elevenlabs.io/v1/music", file=sys.stderr)
            print(f"Body: {json.dumps(body)}", file=sys.stderr)
            print("  xi-api-key: ****abcd", file=sys.stderr)
            print(json.dumps({"dry_run": True}))
            return 0
        code = int(os.environ.get("FAKE_ELEVENLABS_GENERATE_EXIT", "0"))
        if code:
            print(os.environ.get("FAKE_ELEVENLABS_GENERATE_STDERR", "synthetic failure"),
                  file=sys.stderr)
            print("  xi-api-key: ****abcd", file=sys.stderr)
            return code
        milliseconds = sum(item["duration_ms"] for item in
                           body["composition_plan"]["sections"])
        rate = 44_100
        with wave.open(str(target), "wb") as output:
            output.setnchannels(2)
            output.setsampwidth(2)
            output.setframerate(rate)
            chunk = 4096
            for first in range(0, round(milliseconds * rate / 1000), chunk):
                count = min(chunk, round(milliseconds * rate / 1000) - first)
                samples = bytearray()
                for index in range(first, first + count):
                    sample = round(4000 * math.sin(2 * math.pi * 220 * index / rate))
                    samples.extend(struct.pack("<hh", sample, sample))
                output.writeframesraw(samples)
        with target.open("rb") as audio:
            shutil.copyfileobj(audio, sys.stdout.buffer)
        return 0
    if args[:1] == ["sound-generation"]:
        options = dict(zip(args[1::2], args[2::2]))
        if "--dry-run" in args:
            print("POST https://api.elevenlabs.io/v1/sound-generation", file=sys.stderr)
            print(f"Body: {json.dumps({'text': options['--text']})}", file=sys.stderr)
            print("  xi-api-key: ****abcd", file=sys.stderr)
            print(json.dumps({"dry_run": True}))
            return 0
        code = int(os.environ.get("FAKE_ELEVENLABS_GENERATE_EXIT", "0"))
        if code:
            print(os.environ.get("FAKE_ELEVENLABS_GENERATE_STDERR", "synthetic failure"),
                  file=sys.stderr)
            print("  xi-api-key: ****abcd", file=sys.stderr)
            return code
        target = Path(options["--deliver"].removeprefix("file:"))
        rate = 44_100
        count = round(float(options["--duration-seconds"]) * rate)
        frequency = 330 + sum(map(ord, options["--text"])) % 300
        with wave.open(str(target), "wb") as output:
            output.setnchannels(2)
            output.setsampwidth(2)
            output.setframerate(rate)
            for first in range(0, count, 4096):
                samples = bytearray()
                for index in range(first, min(first + 4096, count)):
                    envelope = (1 - index / count) ** 2
                    sample = round(4000 * envelope * math.sin(
                        2 * math.pi * frequency * index / rate))
                    samples.extend(struct.pack("<hh", sample, sample))
                output.writeframesraw(samples)
        with target.open("rb") as audio:
            shutil.copyfileobj(audio, sys.stdout.buffer)
        return 0
    print("unknown synthetic command", file=sys.stderr)
    return 2


sys.exit(main())
