#!/usr/bin/env python3
"""
This script converts all JSON schema files from the 'schemas/json' folder
into Pydantic model files in the 'src/geodexes_schemas' directory using
datamodel-codegen and the following options:

    --snake-case-field
    --use-double-quotes
    --wrap-string-literal
    --use-standard-collections
    --target-python-version 3.12
    --default-null
    --default-factory
    --reuse-model
    --force-optional
    --use-pendulum
    --use-title-as-name
    --use-subclass-enum

These options ensure that your generated code adheres to modern Python
coding standards and best practices.
"""

import os
import glob
import subprocess
from pathlib import Path
def generate_models():
    # Define the input directory containing JSON schema files
    input_dir = Path("schemas/json")
    # Define the output directory where the generated code will be placed
    output_dir = Path("src/geodexes_schemas")
    
    # Ensure the output directory exists
    os.makedirs(output_dir, exist_ok=True)
    
    # Base command options for datamodel-codegen
    base_command = [
        "uv", "run", "datamodel-codegen",
        "--snake-case-field",
        "--use-double-quotes",
        "--wrap-string-literal",
        "--use-standard-collections",
        "--target-python-version", "3.12",
        # "--default-null",
        # "--default-factory",
        "--reuse-model",
        "--force-optional",
        "--use-pendulum",
        "--use-title-as-name",
        "--use-subclass-enum"
    ]
    
    # Use glob to list all JSON files in the input directory
    json_files = glob.glob(os.path.join(input_dir, "*.json"))
    
    if not json_files:
        print(f"No JSON files found in {input_dir}.")
        return

    for json_file in json_files:
        # Determine the base filename (e.g., company.json -> company.py)
        basename = os.path.basename(json_file)
        module_name, _ = os.path.splitext(basename)
        output_file = os.path.join(output_dir, f"{module_name}.py")
        
        # Build the complete command by adding input and output file arguments
        command = base_command + [
            "--input", json_file,
            "--output", output_file
        ]
        
        print(f"Processing {json_file} -> {output_file}")
        try:
            subprocess.check_call(command)
        except subprocess.CalledProcessError as e:
            print(f"Error processing {json_file}: {e}")

def main():
    generate_models()

if __name__ == "__main__":
    main() 