# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import logging

from pyrit.converter.converter import Converter, ConverterResult
from pyrit.models import ComponentIdentifier, PromptDataType

logger = logging.getLogger(__name__)


class RandomCapitalLettersConverter(Converter):
    """Takes a prompt and randomly capitalizes it by a percentage of the total characters."""

    SUPPORTED_INPUT_TYPES = ("text",)
    SUPPORTED_OUTPUT_TYPES = ("text",)

    def __init__(self, *, percentage: float = 100.0, seed: int | None = None) -> None:
        """
        Initialize the converter with the specified percentage of randomization.

        Args:
            percentage (float): The percentage of characters to capitalize in the prompt. Must be between 1 and 100.
                Defaults to 100.0. This includes decimal points in that range.
            seed (int | None): Optional seed for reproducible output. Defaults to None.
        """
        self.percentage = percentage
        self._seed = seed

    def _build_identifier(self) -> ComponentIdentifier:
        """
        Build identifier with random capital letters parameters.

        Returns:
            ComponentIdentifier: The identifier for this converter.
        """
        return self._create_identifier(
            params={
                "percentage": self.percentage,
                "seed": self._seed,
            }
        )

    def is_percentage(self, input_string: float) -> bool:
        """
        Check if the input string is a valid percentage between 1 and 100.

        Args:
            input_string (str): The input string to check.

        Returns:
            bool: True if the input string is a valid percentage, False otherwise.
        """
        try:
            number = float(input_string)
            return 1 <= number <= 100
        except ValueError:
            return False

    def generate_random_positions(self, total_length: int, set_number: int) -> list[int]:
        """
        Generate a list of unique random positions within the range of `total_length`.

        Args:
            total_length (int): The total length of the string.
            set_number (int): The number of unique random positions to generate.

        Returns:
            list: A list of unique random positions.

        Raises:
            ValueError: If `set_number` is greater than `total_length`.
        """
        # Ensure the set number is not greater than the total length
        if set_number > total_length:
            logger.error(f"Set number {set_number} cannot be greater than the total length which is {total_length}.")
            raise ValueError(
                f"Set number {set_number} cannot be greater than the total length which is {total_length}."
            )

        # Generate a list of unique random positions
        return self._get_random_generator(stream="capital-positions").sample(range(total_length), set_number)

    def string_to_upper_case_by_percentage(self, percentage: float, prompt: str) -> str:
        """
        Convert a string by randomly capitalizing a percentage of its characters.

        Args:
            percentage (float): The percentage of characters to capitalize.
            prompt (str): The input string to be converted.

        Returns:
            str: The converted string with randomly capitalized characters.

        Raises:
            ValueError: If the percentage is not between 1 and 100.
        """
        if not self.is_percentage(percentage):
            logger.error(f"Percentage number {percentage} cannot be higher than 100 and lower than 1.")
            raise ValueError(f"Percentage number {percentage} cannot be higher than 100 and lower than 1.")
        target_count = int(len(prompt) * (percentage / 100))
        random_positions = self.generate_random_positions(len(prompt), target_count)
        output = list(prompt)
        for pos in random_positions:
            if prompt[pos].islower():
                output[pos] = prompt[pos].upper()
        return "".join(output)

    async def convert_async(self, *, prompt: str, input_type: PromptDataType = "text") -> ConverterResult:
        """
        Convert the given prompt by randomly capitalizing a percentage of its characters.

        Args:
            prompt (str): The input text prompt to be converted.
            input_type (PromptDataType): The type of input data.

        Returns:
            ConverterResult: The result containing the converted text.

        Raises:
            ValueError: If the input type is not supported.
        """
        if not self.input_supported(input_type):
            raise ValueError("Input type not supported")

        output = self.string_to_upper_case_by_percentage(self.percentage, prompt)
        return ConverterResult(output_text=output, output_type="text")
