"""vector_gen.py - Base class for vector generators.

Each module provides a gen_vectors.py that subclasses VectorGenerator.
The generate_one() method is abstract and must be overridden.
The write_hex_file() method writes vectors as hex-formatted files.
"""

import os
from abc import ABC, abstractmethod


class VectorGenerator(ABC):
    """Base class for test vector generators."""

    @abstractmethod
    def generate_one(self, params: dict) -> dict:
        """Generate a single test vector.

        Args:
            params: Module-specific parameters for vector generation.

        Returns:
            dict with 'input' and 'expected' keys, each containing a dict
            of signal_name -> value.
        """
        ...

    def write_hex_file(self, vectors: list[dict], filepath: str) -> None:
        """Write vectors to a hex file.

        Subclasses should override this to match their module's input format.
        Default: writes each vector on one line, hex values separated by spaces.

        Args:
            vectors: List of result dicts from generate_one().
            filepath: Path to write the hex file.
        """
        os.makedirs(os.path.dirname(filepath), exist_ok=True)
        with open(filepath, 'w') as f:
            for v in vectors:
                # Default: write input values as space-separated hex
                inputs = v.get('input', {})
                hex_vals = [format(val, 'X') for val in inputs.values()]
                f.write(' '.join(hex_vals) + '\n')

    def write_expected_file(self, vectors: list[dict], filepath: str) -> None:
        """Write expected results to a hex file.

        Subclasses should override this to match their module's expected format.
        Default: writes each expected value on one line.

        Args:
            vectors: List of result dicts from generate_one().
            filepath: Path to write the expected hex file.
        """
        os.makedirs(os.path.dirname(filepath), exist_ok=True)
        with open(filepath, 'w') as f:
            for v in vectors:
                expected = v.get('expected', {})
                hex_vals = [format(val, 'X') for val in expected.values()]
                f.write(' '.join(hex_vals) + '\n')
