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

"""
Test Report Generator Module

Generate test execution reports.
"""

import logging
from typing import Any, Dict

from ...registry import register_module

logger = logging.getLogger(__name__)


@register_module(
    module_id='testing.report.generate',
    version='1.0.0',
    category='atomic',
    subcategory='testing',
    tags=['testing', 'report', 'documentation', 'atomic', 'path_restricted'],
    label='Generate Report',
    label_key='modules.testing.report.generate.label',
    description='Generate test execution report',
    description_key='modules.testing.report.generate.description',
    icon='FileText',
    color='#6366F1',

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

    timeout_ms=60000,
    retryable=False,

    params_schema={
        'results': {
            'type': 'object',
            'label': 'Test Results',
            'required': True,
            'description': 'Results data',
        },
        'format': {
            'type': 'string',
            'label': 'Report Format',
            'default': 'json',
            'options': ['json', 'html', 'markdown', 'junit'],
            'placeholder': 'format-string',
            'description': 'Format',
        },
        'title': {
            'type': 'string',
            'label': 'Report Title',
            'default': 'Test Report',
            'placeholder': 'Title',
            'description': 'Title',
        }
    },
    output_schema={
        'ok': {'type': 'boolean', 'description': 'Whether the operation succeeded',
                'description_key': 'modules.testing.report.generate.output.ok.description'},
        'report': {'type': 'string', 'description': 'The report',
                'description_key': 'modules.testing.report.generate.output.report.description'},
        'format': {'type': 'string', 'description': 'The format',
                'description_key': 'modules.testing.report.generate.output.format.description'},
        'summary': {'type': 'object', 'description': 'The summary',
                'description_key': 'modules.testing.report.generate.output.summary.description'}
    }
)
async def testing_report_generate(context: Dict[str, Any]) -> Dict[str, Any]:
    """Generate test report"""
    import json

    params = context['params']
    results = params.get('results', {})
    format_type = params.get('format', 'json')
    title = params.get('title', 'Test Report')

    summary = {
        'title': title,
        'total': results.get('total', 0),
        'passed': results.get('passed', 0),
        'failed': results.get('failed', 0)
    }

    if format_type == 'json':
        report = json.dumps({'title': title, 'results': results}, indent=2)
    elif format_type == 'markdown':
        report = f"# {title}\n\n"
        report += f"- Total: {summary['total']}\n"
        report += f"- Passed: {summary['passed']}\n"
        report += f"- Failed: {summary['failed']}\n"
    else:
        report = json.dumps(results)

    return {
        'ok': True,
        'report': report,
        'format': format_type,
        'summary': summary
    }
