from pathlib import Path

from inventory_handler.InventoryGenerators import (
    CpuSerialNumberGenerator,
    UBootVersionGenerator,
    EcpVersionGenerator,
    ImageVersionGenerator,
    RescueOsVersionGenerator
)
from inventory_handler.InventoryRepository import InventoryRepository
from inventory_handler.InventoryService import InventoryService
from inventory_handler.InventoryUtils import logger
from inventory_handler.RaucSync import syncRaucData, restartRAUCService


class InventoryController:
    """Controller providing external access to inventory operations."""

    def __init__(self, inventoryPath: Path, schemaPath: Path, generators=None):
        """
        Initialize the inventory controller.

        :param inventoryPath: Path to the inventory JSON file.
        :param schemaPath: Path to the JSON schema file.
        :param generators: Optional list of generator instances to use.
        """
        logger.debug(f"Initializing InventoryController with inventoryPath={inventoryPath} and schemaPath={schemaPath}")
        if generators is None:
            generators = []
        repo = InventoryRepository(inventoryPath, schemaPath)
        self.inventoryService = InventoryService(
            repo,
            generators or [
                CpuSerialNumberGenerator(callToActions=[syncRaucData, restartRAUCService]),
                UBootVersionGenerator(),
                EcpVersionGenerator(),
                ImageVersionGenerator(),
                RescueOsVersionGenerator()
            ]
        )
        logger.info(f"InventoryController initialized successfully with {len(self.inventoryService.generators)} generators")

    async def generateInventory(self):
        """Trigger the generation of inventory data."""
        logger.info("Starting inventory generation")
        await self.inventoryService.generateInventory()
        logger.info("Inventory generation completed")

    async def getInventory(self):
        """Retrieve the current inventory data."""
        logger.info("Fetching current inventory data")
        inventory = await self.inventoryService.getInventory()
        logger.info("Inventory data retrieval completed")
        return inventory
