"""Replace the website placeholders with website domains from env_config
Generate the test data"""

import json
import os
from pathlib import Path
from tempfile import NamedTemporaryFile

base_out_path = "config_files/vwa_not_vague"

inp_paths = [
    # "config_files/vwa/test_classifieds.raw.json", "config_files/vwa/test_shopping.raw.json", "config_files/vwa/test_reddit.raw.json",
    # "config_files/wa/test_webarena.raw.json",
    "config_files/vwa_not_vague/test_classifieds.raw.json",
    "config_files/vwa_not_vague/test_shopping.raw.json",
    "config_files/vwa_not_vague/test_reddit.raw.json",
]


def main(inp_paths=inp_paths) -> None:
    for inp_path in inp_paths:
        output_dir = inp_path.replace(".raw.json", "")
        os.makedirs(output_dir, exist_ok=True)
        with open(inp_path, "r") as f:
            raw = f.read()
        data = json.loads(raw)
        for idx, item in enumerate(data):
            file_path = Path(output_dir) / f"{idx}.json"
            file_path.parent.mkdir(parents=True, exist_ok=True)

            # Write atomically
            with NamedTemporaryFile(mode="w", encoding="utf-8", dir=file_path.parent, delete=False) as temp_file:
                json.dump(item, temp_file, indent=2)
                temp_file.flush()
                os.fsync(temp_file.fileno())
                temp_name = temp_file.name

            os.replace(temp_name, file_path)


if __name__ == "__main__":
    main()
