# Flyto2 Core - Source Available License
# Copyright (c) 2025 Flyto2. All Rights Reserved.
#
# This source code is licensed under the Flyto2 Source Available License v1.0.
# Commercial use requires a license. See LICENSE for details.

"""
Assert Greater Than Module

Assert that a value is greater than another.
"""

from typing import Any

from ...base import BaseModule
from ...registry import register_module


@register_module(
    module_id='test.assert_greater_than',
    version='1.0.0',
    category='testing',
    tags=['testing', 'assertion', 'validation'],
    label='Assert Greater Than',
    label_key='modules.test.assert_greater_than.label',
    description='Assert that a value is greater than another',
    description_key='modules.test.assert_greater_than.description',
    icon='CircleCheck',
    color='#22C55E',

    # Connection types
    input_types=['number'],
    output_types=['boolean'],


    can_receive_from=['*'],
    can_connect_to=['testing.*', 'test.*', 'flow.*', 'notify.*', 'data.*'],    params_schema={
        'actual': {
            'type': 'number',
            'required': True,
            'description': 'Actual value'
        ,
                'description_key': 'modules.test.assert_greater_than.params.actual.description',
            'placeholder': '0',
            'label': 'Actual',
        },
        'threshold': {
            'type': 'number',
            'required': True,
            'description': 'Threshold value'
        ,
                'description_key': 'modules.test.assert_greater_than.params.threshold.description',
            'placeholder': '0',
            'label': 'Threshold',
        },
        'message': {
            'type': 'string',
            'required': False,
            'description': 'Custom error message'
        ,
                'description_key': 'modules.test.assert_greater_than.params.message.description',
            'placeholder': 'Your message here',
            'label': 'Message',
        }
    },
    output_schema={
        'passed': {
            'type': 'boolean',
            'description': 'Whether assertion passed'
        ,
                'description_key': 'modules.test.assert_greater_than.output.passed.description'},
        'actual': {
            'type': 'number',
            'description': 'Actual value'
        ,
                'description_key': 'modules.test.assert_greater_than.output.actual.description'},
        'threshold': {
            'type': 'number',
            'description': 'Threshold value'
        ,
                'description_key': 'modules.test.assert_greater_than.output.threshold.description'},
        'message': {
            'type': 'string',
            'description': 'Result message'
        ,
                'description_key': 'modules.test.assert_greater_than.output.message.description'}
    },
    timeout_ms=5000,
)
class AssertGreaterThanModule(BaseModule):
    """Assert that a value is greater than another."""

    module_name = "Assert Greater Than"
    module_description = "Assert that a value is greater than another"

    def validate_params(self) -> None:
        if 'actual' not in self.params:
            raise ValueError("Parameter 'actual' is required")
        if 'threshold' not in self.params:
            raise ValueError("Parameter 'threshold' is required")

    async def execute(self) -> Any:
        actual = self.params.get('actual')
        threshold = self.params.get('threshold')
        custom_message = self.params.get('message')

        passed = actual > threshold

        if passed:
            message = custom_message or f"Assertion passed: {actual} > {threshold}"
        else:
            message = custom_message or f"Assertion failed: {actual} <= {threshold}"

        result = {
            'passed': passed,
            'actual': actual,
            'threshold': threshold,
            'message': message
        }

        if not passed:
            raise AssertionError(message)

        return result
