import asyncio
import sys
from inventory_handler.InventoryController import InventoryController
from inventory_handler.InventoryUtils import INVENTORY_PATH, SCHEMA_PATH, logger


async def run():
    """
    Run the inventory update process.

    This function initializes the InventoryController and triggers the inventory generation,
    which involves reading the existing inventory, generating new values using generators,
    and writing the updated inventory back to the file.

    :return: Exit code 0 indicating success.
    :rtype: int
    """
    controller = InventoryController(INVENTORY_PATH, SCHEMA_PATH)
    await controller.generateInventory()
    return 0

def main():
    """
    Main entry point of the application.

    This function executes the asynchronous run() method and handles any unhandled exceptions.
    If an exception occurs, it logs the error to stderr and exits with code 1.

    Ensures deterministic exit codes for CI/CD or system process handling.
    """
    try:
        exit_code = asyncio.run(run())
        sys.exit(exit_code)
    except Exception as e:
        logger.error(f"Unhandled exception: {e}", file=sys.stderr)
        sys.exit(1)