# Copyright (c) CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com
#

"""ASCII art utilities for generating visual patterns."""

import random
import shutil


def generate_wave_pattern(width: int | None = None, height: int = 5) -> str:
    """
    Generate a random undulating shaded ASCII pattern.

    Args:
        width: Width of the pattern. If None, uses terminal width.
        height: Height of the pattern (default: 5)

    Returns:
        Multi-line string containing the ASCII art pattern
    """
    # Get terminal width if not specified
    if width is None:
        width = shutil.get_terminal_size().columns

    # ASCII characters for shading (from light to dark)
    shading_chars = [" ", "·", ":", "-", "=", "+", "*", "#", "@"]

    # Generate random wave parameters
    amplitude = random.uniform(0.3, 0.7) * (height - 1)
    frequency = random.uniform(0.05, 0.15)
    phase = random.uniform(0, 6.28)  # 0 to 2π

    # Secondary wave for complexity
    amplitude2 = random.uniform(0.2, 0.4) * (height - 1)
    frequency2 = random.uniform(0.1, 0.2)
    phase2 = random.uniform(0, 6.28)

    # Generate the pattern
    lines = []
    for y in range(height):
        line = []
        for x in range(width):
            # Calculate wave values
            wave1 = amplitude * (0.5 + 0.5 * math.sin(frequency * x + phase))
            wave2 = amplitude2 * (0.5 + 0.5 * math.sin(frequency2 * x + phase2))

            # Combine waves and add vertical gradient
            value = (wave1 + wave2) / 2 + (y / height) * 2

            # Add some noise
            value += random.uniform(-0.3, 0.3)

            # Map to shading character
            char_index = int(max(0, min(len(shading_chars) - 1, value * len(shading_chars) / 3)))
            line.append(shading_chars[char_index])

        lines.append("".join(line))

    return "\n".join(lines)


def generate_error_divider(width: int | None = None) -> str:
    """
    Generate a decorative divider for error messages.

    Args:
        width: Width of the divider. If None, uses terminal width.

    Returns:
        Single-line decorative divider string
    """
    if width is None:
        width = shutil.get_terminal_size().columns

    # Pattern components
    patterns = [
        "═" * width,
        "─" * width,
        "━" * width,
        "╌" * width,
        "┅" * width,
    ]

    # Decorative patterns
    decorative = [
        f"{'╔' + '═' * (width - 2) + '╗'}",
        f"{'╚' + '═' * (width - 2) + '╝'}",
        f"{'┌' + '─' * (width - 2) + '┐'}",
        f"{'└' + '─' * (width - 2) + '┘'}",
        f"{'▓' * width}",
        f"{'░' * width}",
        f"{'▒' * width}",
    ]

    # Mix of simple and decorative
    all_patterns = patterns + decorative

    return random.choice(all_patterns)


# Import math for the wave calculations
import math
