# 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.

"""
Unit Test Runner Module

Execute unit tests.
"""

import logging
from typing import Any, Dict

from ...registry import register_module

logger = logging.getLogger(__name__)


@register_module(
    module_id='testing.unit.run',
    version='1.0.0',
    category='atomic',
    subcategory='testing',
    tags=['testing', 'unit', 'unittest', 'atomic', 'path_restricted'],
    label='Run Unit Tests',
    label_key='modules.testing.unit.run.label',
    description='Execute unit tests',
    description_key='modules.testing.unit.run.description',
    icon='TestTube',
    color='#22C55E',

    input_types=['string', 'array'],
    output_types=['object'],
    can_receive_from=['start', 'flow.*', 'file.*'],
    can_connect_to=['testing.*', 'notify.*', 'data.*', 'flow.*'],

    timeout_ms=300000,
    retryable=False,

    params_schema={
        'paths': {
            'type': 'array',
            'label': 'Test Paths',
            'required': True,
            'description': 'Paths to test files or directories'
        ,
                'description_key': 'modules.testing.unit.run.params.paths.description'},
        'pattern': {
            'type': 'string',
            'label': 'Pattern',
            'default': 'test_*.py',
            'placeholder': '^pattern$',
            'description': 'Pattern',
        },
        'verbose': {
            'type': 'boolean',
            'label': 'Verbose',
            'default': False,
            'description': 'Whether to verbose',
        }
    },
    output_schema={
        'ok': {'type': 'boolean', 'description': 'Whether the operation succeeded',
                'description_key': 'modules.testing.unit.run.output.ok.description'},
        'passed': {'type': 'number', 'description': 'Number of tests passed',
                'description_key': 'modules.testing.unit.run.output.passed.description'},
        'failed': {'type': 'number', 'description': 'Number of tests failed',
                'description_key': 'modules.testing.unit.run.output.failed.description'},
        'errors': {'type': 'number', 'description': 'Number of errors encountered',
                'description_key': 'modules.testing.unit.run.output.errors.description'},
        'results': {'type': 'array', 'description': 'List of results',
                'description_key': 'modules.testing.unit.run.output.results.description'}
    }
)
async def testing_unit_run(context: Dict[str, Any]) -> Dict[str, Any]:
    """Run unit tests"""
    params = context['params']
    paths = params.get('paths', [])

    # Placeholder implementation
    return {
        'ok': True,
        'passed': 0,
        'failed': 0,
        'errors': 0,
        'total': 0,
        'results': [],
        'paths': paths
    }
